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

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-02-28 13:32:26 +03:00
const { logger, file, xml } = 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')
.parse(process.argv)
const options = program.opts()
async function main() {
const config = require(path.resolve(options.config))
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
2021-11-15 21:58:52 +03:00
let channels = config.channels(args)
2021-11-12 18:08:51 +03:00
if (isPromise(channels)) {
channels = await channels
}
2022-03-04 20:35:36 +03:00
channels = channels.map(c => {
c.lang = c.lang || 'en'
return c
})
channels = _.sortBy(channels, ['lang', 'xmltv_id'])
2022-01-20 20:18:49 +03:00
2022-02-28 13:32:26 +03:00
const dir = file.dirname(options.config)
const outputFilepath = options.output || `${dir}/${config.site}.channels.xml`
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'
}