From 114ff0823b973a209bfaa3124b971f7025cfa897 Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Fri, 30 Jun 2023 14:41:28 +0300 Subject: [PATCH] Update parse.js --- scripts/commands/channels/parse.js | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/scripts/commands/channels/parse.js b/scripts/commands/channels/parse.js index 0200de32..84e8f175 100644 --- a/scripts/commands/channels/parse.js +++ b/scripts/commands/channels/parse.js @@ -1,4 +1,4 @@ -const { logger, file, xml } = require('../../core') +const { logger, file, xml, parser } = require('../../core') const { Command } = require('commander') const path = require('path') const _ = require('lodash') @@ -8,31 +8,44 @@ program .requiredOption('-c, --config ', 'Config file') .option('-s, --set [args...]', 'Set custom arguments', []) .option('-o, --output ', 'Output file') + .option('--clean', 'Delete the previous *.channels.xml if exists') .parse(process.argv) const options = program.opts() async function main() { const config = require(path.resolve(options.config)) + 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 + } + const args = {} options.set.forEach(arg => { const [key, value] = arg.split(':') args[key] = value }) - let channels = config.channels(args) - if (isPromise(channels)) { - channels = await channels + let parsedChannels = config.channels(args) + if (isPromise(parsedChannels)) { + parsedChannels = await parsedChannels } - channels = channels.map(c => { + parsedChannels = parsedChannels.map(c => { c.lang = c.lang || 'en' return c }) - channels = _.sortBy(channels, ['lang', 'xmltv_id']) - const dir = file.dirname(options.config) - const outputFilepath = options.output || `${dir}/${config.site}.channels.xml` + channels = channels.concat(parsedChannels) + + channels = _.uniqBy(channels, c => c.site_id + c.lang) + + channels = _.sortBy(channels, ['lang', 'xmltv_id']) const output = xml.create(channels, config.site)