2022-02-02 06:08:48 +03:00
|
|
|
const { parser, logger, api } = require('../core')
|
|
|
|
|
const { program } = require('commander')
|
|
|
|
|
const chalk = require('chalk')
|
|
|
|
|
|
|
|
|
|
program.argument('<filepath>', 'Path to file to validate').parse(process.argv)
|
|
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
|
await api.channels.load()
|
|
|
|
|
|
|
|
|
|
const stats = {
|
2022-02-26 12:50:29 +03:00
|
|
|
files: 0,
|
|
|
|
|
errors: 0
|
2022-02-02 06:08:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!program.args.length) {
|
|
|
|
|
logger.error('required argument "filepath" not specified')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const filepath of program.args) {
|
2022-02-03 04:44:56 +03:00
|
|
|
if (!filepath.endsWith('.xml')) continue
|
|
|
|
|
|
2022-02-02 06:08:48 +03:00
|
|
|
const { site, channels } = await parser.parseChannels(filepath)
|
|
|
|
|
|
2022-02-26 12:50:29 +03:00
|
|
|
const buffer = {}
|
|
|
|
|
const errors = []
|
2022-02-02 06:08:48 +03:00
|
|
|
for (const channel of channels) {
|
2022-02-26 12:50:29 +03:00
|
|
|
if (!buffer[channel.xmltv_id]) {
|
|
|
|
|
buffer[channel.xmltv_id] = channel
|
|
|
|
|
} else {
|
|
|
|
|
errors.push({ type: 'duplicate', ...channel })
|
|
|
|
|
stats.errors++
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 06:08:48 +03:00
|
|
|
if (!api.channels.find({ id: channel.xmltv_id })) {
|
2022-02-26 12:50:29 +03:00
|
|
|
errors.push({ type: 'wrong_xmltv_id', ...channel })
|
|
|
|
|
stats.errors++
|
2022-02-02 06:08:48 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-26 12:50:29 +03:00
|
|
|
if (errors.length) {
|
2022-02-02 06:08:48 +03:00
|
|
|
logger.info(chalk.underline(filepath))
|
2022-02-26 12:50:29 +03:00
|
|
|
console.table(errors, ['type', 'lang', 'xmltv_id', 'site_id', 'name'])
|
2022-02-02 06:08:48 +03:00
|
|
|
console.log()
|
|
|
|
|
stats.files++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-26 12:50:29 +03:00
|
|
|
if (stats.errors > 0) {
|
|
|
|
|
logger.error(chalk.red(`${stats.errors} error(s) in ${stats.files} file(s)`))
|
2022-02-02 06:08:48 +03:00
|
|
|
process.exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main()
|