iptv/scripts/commands/playlist/validate.js

68 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-02-11 19:07:16 +01:00
const { file, logger, api, parser, blocklist } = 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')
2021-12-12 05:10:03 +01:00
2022-02-11 08:09:31 +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-02-11 08:09:31 +01:00
await api.channels.load()
let errors = []
let warnings = []
for (const filepath of program.args) {
if (!filepath.endsWith('.m3u')) continue
2021-12-12 05:10:03 +01:00
const basename = file.basename(filepath)
2022-02-11 08:09:31 +01:00
const [_, countryCode] = basename.match(/([a-z]{2})(|_.*)\.m3u/i) || [null, null]
const fileLog = []
const streams = await parser.parsePlaylist(filepath)
for (const stream of streams) {
const found = blocklist.find(stream.name, countryCode.toUpperCase())
if (found) {
fileLog.push({
type: 'error',
line: stream.line,
message: `"${found.name}" is on the blocklist due to claims of copyright holders (${found.reference})`
})
}
if (stream.tvg.id && !api.channels.find({ id: stream.tvg.id })) {
fileLog.push({
type: 'warning',
line: stream.line,
message: `"${stream.tvg.id}" is not in the database`
})
}
}
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()