Files
epg/scripts/commands/channels/validate.js

69 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-02-27 09:18:53 +03:00
const { parser, logger, api } = require('../../core')
2022-02-02 06:08:48 +03:00
const { program } = require('commander')
const chalk = require('chalk')
2022-11-12 01:22:15 +03:00
const langs = require('langs')
2022-02-02 06:08:48 +03:00
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-11-02 04:07:20 +03:00
const bufferById = {}
const bufferBySiteId = {}
2022-02-26 12:50:29 +03:00
const errors = []
2022-02-02 06:08:48 +03:00
for (const channel of channels) {
2022-11-05 01:46:07 +03:00
if (!bufferById[channel.id + channel.lang]) {
bufferById[channel.id + channel.lang] = channel
2022-11-02 04:07:20 +03:00
} else {
errors.push({ type: 'duplicate', xmltv_id: channel.id, ...channel })
stats.errors++
}
2022-11-05 01:46:07 +03:00
if (!bufferBySiteId[channel.site_id + channel.lang]) {
bufferBySiteId[channel.site_id + channel.lang] = channel
2022-02-26 12:50:29 +03:00
} else {
2022-06-16 21:58:15 +03:00
errors.push({ type: 'duplicate', xmltv_id: channel.id, ...channel })
2022-02-26 12:50:29 +03:00
stats.errors++
}
2022-06-16 21:58:15 +03:00
if (!api.channels.find({ id: channel.id })) {
errors.push({ type: 'wrong_xmltv_id', xmltv_id: channel.id, ...channel })
2022-02-26 12:50:29 +03:00
stats.errors++
2022-02-02 06:08:48 +03:00
}
2022-11-12 01:22:15 +03:00
if (!langs.where('1', channel.lang)) {
errors.push({ type: 'wrong_lang', xmltv_id: channel.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()