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

93 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-07-18 22:51:01 +03:00
import { ChannelsParser, DataLoader, DataProcessor } from '../../core'
import { DataProcessorData } from '../../types/dataProcessor'
import { Storage, Dictionary, File } from '@freearhey/core'
import { DataLoaderData } from '../../types/dataLoader'
import { ChannelList } from '../../models'
import { DATA_DIR } from '../../constants'
import epgGrabber from 'epg-grabber'
2023-10-15 14:08:23 +03:00
import { program } from 'commander'
import chalk from 'chalk'
import langs from 'langs'
2025-01-13 17:56:54 +03:00
program.argument('[filepath]', 'Path to *.channels.xml files to validate').parse(process.argv)
2023-10-15 14:08:23 +03:00
type ValidationError = {
2025-07-17 17:43:00 +03:00
type: 'duplicate' | 'wrong_channel_id' | 'wrong_feed_id' | 'wrong_lang'
2023-10-15 14:08:23 +03:00
name: string
lang?: string
xmltv_id?: string
site_id?: string
logo?: string
}
async function main() {
2025-07-18 22:51:01 +03:00
const processor = new DataProcessor()
2023-10-15 14:08:23 +03:00
const dataStorage = new Storage(DATA_DIR)
2025-07-18 22:51:01 +03:00
const loader = new DataLoader({ storage: dataStorage })
const data: DataLoaderData = await loader.load()
const { channelsKeyById, feedsKeyByStreamId }: DataProcessorData = processor.process(data)
const parser = new ChannelsParser({
storage: new Storage()
})
2023-10-15 14:08:23 +03:00
let totalFiles = 0
let totalErrors = 0
2025-01-13 17:56:54 +03:00
2023-10-15 14:08:23 +03:00
const storage = new Storage()
2025-01-13 17:56:54 +03:00
const files = program.args.length ? program.args : await storage.list('sites/**/*.channels.xml')
2023-10-15 14:08:23 +03:00
for (const filepath of files) {
const file = new File(filepath)
if (file.extension() !== 'xml') continue
2025-07-18 22:51:01 +03:00
const channelList: ChannelList = await parser.parse(filepath)
2023-10-15 14:08:23 +03:00
const bufferBySiteId = new Dictionary()
const errors: ValidationError[] = []
2025-07-18 22:51:01 +03:00
channelList.channels.forEach((channel: epgGrabber.Channel) => {
2025-01-13 16:32:32 +03:00
const bufferId: string = channel.site_id
if (bufferBySiteId.missing(bufferId)) {
bufferBySiteId.set(bufferId, true)
2023-10-15 14:08:23 +03:00
} else {
errors.push({ type: 'duplicate', ...channel })
totalErrors++
}
if (!langs.where('1', channel.lang ?? '')) {
2023-12-03 20:52:08 +03:00
errors.push({ type: 'wrong_lang', ...channel })
totalErrors++
}
if (!channel.xmltv_id) return
2025-07-17 17:43:00 +03:00
const [channelId, feedId] = channel.xmltv_id.split('@')
const foundChannel = channelsKeyById.get(channelId)
2023-11-20 08:56:11 +03:00
if (!foundChannel) {
2025-07-17 17:43:00 +03:00
errors.push({ type: 'wrong_channel_id', ...channel })
2023-10-15 14:08:23 +03:00
totalErrors++
}
2025-07-17 17:43:00 +03:00
if (feedId) {
const foundFeed = feedsKeyByStreamId.get(channel.xmltv_id)
if (!foundFeed) {
errors.push({ type: 'wrong_feed_id', ...channel })
totalErrors++
}
}
2023-10-15 14:08:23 +03:00
})
if (errors.length) {
console.log(chalk.underline(filepath))
console.table(errors, ['type', 'lang', 'xmltv_id', 'site_id', 'name'])
console.log()
totalFiles++
}
}
if (totalErrors > 0) {
console.log(chalk.red(`${totalErrors} error(s) in ${totalFiles} file(s)`))
process.exit(1)
}
}
main()