From feddd9500b410d733d0634a9d73741c132d529ea Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Fri, 30 Jun 2023 14:41:15 +0300 Subject: [PATCH 1/5] Update tests/__data__ --- .../expected/sites/parse-channels-clean.channels.xml | 7 +++++++ tests/__data__/expected/sites/parse-channels.channels.xml | 1 + tests/__data__/input/sites/parse-channels.channels.xml | 6 ++++++ tests/__data__/input/sites/parse-channels.config.js | 6 +++++- 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/__data__/expected/sites/parse-channels-clean.channels.xml create mode 100644 tests/__data__/input/sites/parse-channels.channels.xml diff --git a/tests/__data__/expected/sites/parse-channels-clean.channels.xml b/tests/__data__/expected/sites/parse-channels-clean.channels.xml new file mode 100644 index 00000000..2b8e092f --- /dev/null +++ b/tests/__data__/expected/sites/parse-channels-clean.channels.xml @@ -0,0 +1,7 @@ + + + + CNN International + BBC World News + + diff --git a/tests/__data__/expected/sites/parse-channels.channels.xml b/tests/__data__/expected/sites/parse-channels.channels.xml index 82cbe2a7..33163fe9 100644 --- a/tests/__data__/expected/sites/parse-channels.channels.xml +++ b/tests/__data__/expected/sites/parse-channels.channels.xml @@ -2,5 +2,6 @@ CNN International + BBC World News diff --git a/tests/__data__/input/sites/parse-channels.channels.xml b/tests/__data__/input/sites/parse-channels.channels.xml new file mode 100644 index 00000000..29f16d4b --- /dev/null +++ b/tests/__data__/input/sites/parse-channels.channels.xml @@ -0,0 +1,6 @@ + + + + CNN International + + diff --git a/tests/__data__/input/sites/parse-channels.config.js b/tests/__data__/input/sites/parse-channels.config.js index 9db10dc2..e79ffa4e 100644 --- a/tests/__data__/input/sites/parse-channels.config.js +++ b/tests/__data__/input/sites/parse-channels.config.js @@ -10,9 +10,13 @@ module.exports = { return [ { lang: 'en', - xmltv_id: 'CNNInternational.us', site_id: 140, name: 'CNN International' + }, + { + lang: 'en', + site_id: 240, + name: 'BBC World News' } ] } From f2057dd0157a81c001063a669b577c2f0c74f8ce Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Fri, 30 Jun 2023 14:41:19 +0300 Subject: [PATCH 2/5] Update parse.test.js --- tests/commands/channels/parse.test.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/tests/commands/channels/parse.test.js b/tests/commands/channels/parse.test.js index bc82251c..a9242a4f 100644 --- a/tests/commands/channels/parse.test.js +++ b/tests/commands/channels/parse.test.js @@ -4,19 +4,34 @@ const path = require('path') beforeEach(() => { fs.emptyDirSync('tests/__data__/output') - - const stdout = execSync( - 'npm run channels:parse -- --config=tests/__data__/input/sites/parse-channels.config.js --output=tests/__data__/output/channels.xml', - { encoding: 'utf8' } + fs.copySync( + 'tests/__data__/input/sites/parse-channels.channels.xml', + 'tests/__data__/output/channels.xml' ) }) it('can parse channels', () => { + const stdout = execSync( + 'npm run channels:parse -- --config=tests/__data__/input/sites/parse-channels.config.js --output=tests/__data__/output/channels.xml', + { encoding: 'utf8' } + ) + expect(content('tests/__data__/output/channels.xml')).toEqual( content('tests/__data__/expected/sites/parse-channels.channels.xml') ) }) +it('can parse channels with clean flag', () => { + const stdout = execSync( + 'npm run channels:parse -- --config=tests/__data__/input/sites/parse-channels.config.js --output=tests/__data__/output/channels.xml --clean', + { encoding: 'utf8' } + ) + + expect(content('tests/__data__/output/channels.xml')).toEqual( + content('tests/__data__/expected/sites/parse-channels-clean.channels.xml') + ) +}) + function content(filepath) { return fs.readFileSync(path.resolve(filepath), { encoding: 'utf8' 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 3/5] 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) From a3f67947f1443a3041f2fff8b02a54f8d9da861f Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Fri, 30 Jun 2023 14:52:26 +0300 Subject: [PATCH 4/5] Update file.js --- scripts/core/file.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/core/file.js b/scripts/core/file.js index d3c7b384..55300e7e 100644 --- a/scripts/core/file.js +++ b/scripts/core/file.js @@ -26,6 +26,10 @@ file.exists = function (filepath) { return fs.exists(path.resolve(filepath)) } +file.existsSync = function (filepath) { + return fs.existsSync(path.resolve(filepath)) +} + file.read = function (filepath) { return fs.readFile(path.resolve(filepath), { encoding: 'utf8' }).catch(console.error) } From 95134277fe5958f058ee79c76619e2eb9101fc4d Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Fri, 30 Jun 2023 14:53:10 +0300 Subject: [PATCH 5/5] Update editor.js --- scripts/commands/channels/editor.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/scripts/commands/channels/editor.js b/scripts/commands/channels/editor.js index 8df89f04..33f46702 100644 --- a/scripts/commands/channels/editor.js +++ b/scripts/commands/channels/editor.js @@ -5,10 +5,11 @@ const { program } = require('commander') const inquirer = require('inquirer') program - .requiredOption('-i, --input ', 'Load channels from the file') + .argument('', 'Path to *.channels.xml file to edit') .option('-c, --country ', 'Source country', 'us') .parse(process.argv) +const filepath = program.args[0] const options = program.opts() const defaultCountry = options.country const newLabel = ` [new]` @@ -17,7 +18,12 @@ let site let channels = [] async function main() { - let result = await parser.parseChannels(options.input) + if (!(await file.exists(filepath))) { + throw new Error(`File "${filepath}" does not exists`) + return + } + + let result = await parser.parseChannels(filepath) site = result.site channels = result.channels channels = channels.map(c => { @@ -59,11 +65,13 @@ async function main() { main() function save() { + if (!file.existsSync(filepath)) return + const output = xml.create(channels, site) - file.writeSync(options.input, output) + file.writeSync(filepath, output) - logger.info(`\nFile '${options.input}' successfully saved`) + logger.info(`\nFile '${filepath}' successfully saved`) } nodeCleanup(() => {