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

62 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-06-30 14:41:28 +03:00
const { logger, file, xml, parser } = require('../../core')
2021-11-12 18:08:51 +03:00
const { Command } = require('commander')
const path = require('path')
2022-01-20 19:22:53 +03:00
const _ = require('lodash')
2021-11-12 18:08:51 +03:00
const program = new Command()
program
.requiredOption('-c, --config <config>', 'Config file')
2021-11-17 16:09:24 +03:00
.option('-s, --set [args...]', 'Set custom arguments', [])
2021-11-12 18:08:51 +03:00
.option('-o, --output <output>', 'Output file')
2023-06-30 14:41:28 +03:00
.option('--clean', 'Delete the previous *.channels.xml if exists')
2021-11-12 18:08:51 +03:00
.parse(process.argv)
const options = program.opts()
async function main() {
const config = require(path.resolve(options.config))
2023-06-30 14:41:28 +03:00
const dir = file.dirname(options.config)
const outputFilepath = options.output || `${dir}/${config.site}.channels.xml`
let channels = []
if (!options.clean && (await file.exists(outputFilepath))) {
let result = await parser.parseChannels(outputFilepath)
channels = result.channels
}
2021-11-15 21:58:52 +03:00
const args = {}
options.set.forEach(arg => {
const [key, value] = arg.split(':')
args[key] = value
})
2022-02-28 13:32:26 +03:00
2023-06-30 14:41:28 +03:00
let parsedChannels = config.channels(args)
if (isPromise(parsedChannels)) {
parsedChannels = await parsedChannels
2021-11-12 18:08:51 +03:00
}
2023-06-30 14:41:28 +03:00
parsedChannels = parsedChannels.map(c => {
2022-03-04 20:35:36 +03:00
c.lang = c.lang || 'en'
return c
})
2022-01-20 20:18:49 +03:00
2023-06-30 14:41:28 +03:00
channels = channels.concat(parsedChannels)
channels = _.uniqBy(channels, c => c.site_id + c.lang)
2023-06-30 19:55:17 +03:00
channels = _.sortBy(channels, ['lang', c => c.xmltv_id || '_', 'site_id'])
2021-11-12 18:08:51 +03:00
2022-02-28 13:32:26 +03:00
const output = xml.create(channels, config.site)
2021-11-12 18:08:51 +03:00
2022-02-28 13:32:26 +03:00
await file.write(outputFilepath, output)
2021-11-12 18:08:51 +03:00
2022-03-03 22:01:41 +03:00
logger.info(`File '${outputFilepath}' successfully saved`)
2021-11-12 18:08:51 +03:00
}
main()
function isPromise(promise) {
return !!promise && typeof promise.then === 'function'
}