Update validate.js

This commit is contained in:
Aleksandr Statciuk 2023-01-08 09:59:21 +03:00
parent f21ae7f646
commit 1dd2a55cfd
1 changed files with 33 additions and 31 deletions

View File

@ -1,40 +1,42 @@
const { db, logger, api } = require('../../core')
const { db, logger, api, parser } = require('../../core')
const chalk = require('chalk')
const _ = require('lodash')
const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs'
async function main() {
logger.info('loading data/channels.json...')
await api.channels.load()
await api.guides.load()
const logPath = `${LOGS_DIR}/guides/update.log`
logger.info(`loading ${logPath}...`)
const guides = await parser.parseLogs(logPath)
logger.info('loading database/programs.db...')
await db.programs.load()
let db_programs = await db.programs.find({})
logger.info(`found ${db_programs.length} programs`)
const stats = {
files: 0,
errors: 0
}
const errors = []
let api_channels = await api.channels.all()
api_channels.forEach(channel => {
let programs = db_programs.filter(p => p.channel == channel.id)
if (programs.length > 0) {
if (!api.guides.find({ channel: channel.id })) {
errors.push({ type: 'no_guide', xmltv_id: channel.id, ...channel })
stats.errors++
let programs = db_programs.map(p => ({
site: p.site,
xmltv_id: p.channel,
lang: p.titles[0].lang
}))
programs = _.uniqBy(programs, p => p.site + p.xmltv_id)
for (let program of programs) {
if (!guides.find(g => g.channel === program.xmltv_id)) {
const channel = await api.channels.find({ id: program.xmltv_id })
errors.push({ type: 'no_guide', ...program, ...channel })
}
}
})
if (errors.length) {
console.table(errors, ['type', 'xmltv_id', 'name', 'country'])
console.table(errors, ['type', 'site', 'lang', 'xmltv_id', 'broadcast_area', 'languages'])
console.log()
stats.files++
}
if (stats.errors > 0) {
logger.error(chalk.red(`${stats.errors} error(s)`))
process.exit(1)
logger.error(chalk.red(`${errors.length} error(s)`))
}
}