Merge branch 'master' into update-dishtv.in

This commit is contained in:
freearhey 2023-06-30 14:53:31 +03:00
commit 16061d7507
8 changed files with 75 additions and 17 deletions

View file

@ -5,10 +5,11 @@ const { program } = require('commander')
const inquirer = require('inquirer')
program
.requiredOption('-i, --input <file>', 'Load channels from the file')
.argument('<filepath>', 'Path to *.channels.xml file to edit')
.option('-c, --country <name>', '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(() => {

View file

@ -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>', 'Config file')
.option('-s, --set [args...]', 'Set custom arguments', [])
.option('-o, --output <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)

View file

@ -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)
}

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<site site="parse-channels.com">
<channels>
<channel lang="en" xmltv_id="" site_id="140">CNN International</channel>
<channel lang="en" xmltv_id="" site_id="240">BBC World News</channel>
</channels>
</site>

View file

@ -2,5 +2,6 @@
<site site="parse-channels.com">
<channels>
<channel lang="en" xmltv_id="CNNInternational.us" site_id="140">CNN International</channel>
<channel lang="en" xmltv_id="" site_id="240">BBC World News</channel>
</channels>
</site>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<site site="parse-channels.com">
<channels>
<channel lang="en" xmltv_id="CNNInternational.us" site_id="140">CNN International</channel>
</channels>
</site>

View file

@ -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'
}
]
}

View file

@ -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'