iptv/scripts/commands/playlist/validate.js

88 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-02-14 10:00:05 +01:00
const { file, logger, api, parser, id } = require('../../core')
2021-12-12 05:10:03 +01:00
const { program } = require('commander')
2022-02-11 08:09:31 +01:00
const chalk = require('chalk')
2022-02-14 10:00:05 +01:00
const _ = require('lodash')
2021-12-12 05:10:03 +01:00
2022-02-14 10:00:05 +01:00
program.argument('[filepath]', 'Path to file to validate').parse(process.argv)
2021-12-12 05:10:03 +01:00
async function main() {
2022-03-11 15:36:58 +01:00
const files = program.args.length ? program.args : await file.list('streams/*.m3u')
2022-02-14 10:00:05 +01:00
logger.info(`loading blocklist...`)
2022-02-11 08:09:31 +01:00
await api.channels.load()
2022-02-14 10:00:05 +01:00
await api.blocklist.load()
let blocklist = await api.blocklist.all()
blocklist = blocklist
.map(blocked => {
const channel = api.channels.find({ id: blocked.channel })
if (!channel) return null
return { ...blocked, name: channel.name }
})
.filter(i => i)
logger.info(`found ${blocklist.length} records`)
2022-02-11 08:09:31 +01:00
let errors = []
let warnings = []
2022-02-14 10:00:05 +01:00
for (const filepath of files) {
2022-02-11 08:09:31 +01:00
if (!filepath.endsWith('.m3u')) continue
2021-12-12 05:10:03 +01:00
const basename = file.basename(filepath)
2022-02-14 10:00:05 +01:00
const [__, country] = basename.match(/([a-z]{2})(|_.*)\.m3u/i) || [null, null]
2022-02-11 08:09:31 +01:00
const fileLog = []
2022-03-11 16:01:45 +01:00
const playlist = await parser.parsePlaylist(filepath)
for (const item of playlist.items) {
2022-02-14 10:00:05 +01:00
if (item.tvg.id && !api.channels.find({ id: item.tvg.id })) {
2022-02-11 08:09:31 +01:00
fileLog.push({
2022-02-14 10:00:05 +01:00
type: 'warning',
line: item.line,
message: `"${item.tvg.id}" is not in the database`
2022-02-11 08:09:31 +01:00
})
}
2022-02-14 10:00:05 +01:00
const channel_id = id.generate(item.name, country)
const found = blocklist.find(
blocked =>
item.tvg.id.toLowerCase() === blocked.channel.toLowerCase() ||
channel_id.toLowerCase() === blocked.channel.toLowerCase()
)
if (found) {
2022-02-11 08:09:31 +01:00
fileLog.push({
2022-02-14 10:00:05 +01:00
type: 'error',
line: item.line,
message: `"${found.name}" is on the blocklist due to claims of copyright holders (${found.ref})`
2022-02-11 08:09:31 +01:00
})
}
}
2021-12-12 05:10:03 +01:00
2022-02-11 08:09:31 +01:00
if (fileLog.length) {
logger.info(`\n${chalk.underline(filepath)}`)
2021-12-12 05:10:03 +01:00
2022-02-11 08:09:31 +01:00
fileLog.forEach(err => {
const position = err.line.toString().padEnd(6, ' ')
const type = err.type.padEnd(9, ' ')
const status = err.type === 'error' ? chalk.red(type) : chalk.yellow(type)
logger.info(` ${chalk.gray(position)}${status}${err.message}`)
2021-12-12 05:10:03 +01:00
})
2022-02-11 08:09:31 +01:00
errors = errors.concat(fileLog.filter(e => e.type === 'error'))
warnings = warnings.concat(fileLog.filter(e => e.type === 'warning'))
}
2021-12-12 05:10:03 +01:00
}
2022-02-11 08:09:31 +01:00
logger.error(
chalk.red(
`\n${errors.length + warnings.length} problems (${errors.length} errors, ${
warnings.length
} warnings)`
)
)
2021-12-12 05:10:03 +01:00
if (errors.length) {
process.exit(1)
}
}
main()