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') const inquirer = require('inquirer')
program 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') .option('-c, --country <name>', 'Source country', 'us')
.parse(process.argv) .parse(process.argv)
const filepath = program.args[0]
const options = program.opts() const options = program.opts()
const defaultCountry = options.country const defaultCountry = options.country
const newLabel = ` [new]` const newLabel = ` [new]`
@ -17,7 +18,12 @@ let site
let channels = [] let channels = []
async function main() { 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 site = result.site
channels = result.channels channels = result.channels
channels = channels.map(c => { channels = channels.map(c => {
@ -59,11 +65,13 @@ async function main() {
main() main()
function save() { function save() {
if (!file.existsSync(filepath)) return
const output = xml.create(channels, site) 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(() => { 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 { Command } = require('commander')
const path = require('path') const path = require('path')
const _ = require('lodash') const _ = require('lodash')
@ -8,31 +8,44 @@ program
.requiredOption('-c, --config <config>', 'Config file') .requiredOption('-c, --config <config>', 'Config file')
.option('-s, --set [args...]', 'Set custom arguments', []) .option('-s, --set [args...]', 'Set custom arguments', [])
.option('-o, --output <output>', 'Output file') .option('-o, --output <output>', 'Output file')
.option('--clean', 'Delete the previous *.channels.xml if exists')
.parse(process.argv) .parse(process.argv)
const options = program.opts() const options = program.opts()
async function main() { async function main() {
const config = require(path.resolve(options.config)) 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 = {} const args = {}
options.set.forEach(arg => { options.set.forEach(arg => {
const [key, value] = arg.split(':') const [key, value] = arg.split(':')
args[key] = value args[key] = value
}) })
let channels = config.channels(args) let parsedChannels = config.channels(args)
if (isPromise(channels)) { if (isPromise(parsedChannels)) {
channels = await channels parsedChannels = await parsedChannels
} }
channels = channels.map(c => { parsedChannels = parsedChannels.map(c => {
c.lang = c.lang || 'en' c.lang = c.lang || 'en'
return c return c
}) })
channels = _.sortBy(channels, ['lang', 'xmltv_id'])
const dir = file.dirname(options.config) channels = channels.concat(parsedChannels)
const outputFilepath = options.output || `${dir}/${config.site}.channels.xml`
channels = _.uniqBy(channels, c => c.site_id + c.lang)
channels = _.sortBy(channels, ['lang', 'xmltv_id'])
const output = xml.create(channels, config.site) const output = xml.create(channels, config.site)

View file

@ -26,6 +26,10 @@ file.exists = function (filepath) {
return fs.exists(path.resolve(filepath)) return fs.exists(path.resolve(filepath))
} }
file.existsSync = function (filepath) {
return fs.existsSync(path.resolve(filepath))
}
file.read = function (filepath) { file.read = function (filepath) {
return fs.readFile(path.resolve(filepath), { encoding: 'utf8' }).catch(console.error) 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"> <site site="parse-channels.com">
<channels> <channels>
<channel lang="en" xmltv_id="CNNInternational.us" site_id="140">CNN International</channel> <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> </channels>
</site> </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 [ return [
{ {
lang: 'en', lang: 'en',
xmltv_id: 'CNNInternational.us',
site_id: 140, site_id: 140,
name: 'CNN International' name: 'CNN International'
},
{
lang: 'en',
site_id: 240,
name: 'BBC World News'
} }
] ]
} }

View file

@ -4,19 +4,34 @@ const path = require('path')
beforeEach(() => { beforeEach(() => {
fs.emptyDirSync('tests/__data__/output') fs.emptyDirSync('tests/__data__/output')
fs.copySync(
const stdout = execSync( 'tests/__data__/input/sites/parse-channels.channels.xml',
'npm run channels:parse -- --config=tests/__data__/input/sites/parse-channels.config.js --output=tests/__data__/output/channels.xml', 'tests/__data__/output/channels.xml'
{ encoding: 'utf8' }
) )
}) })
it('can parse channels', () => { 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( expect(content('tests/__data__/output/channels.xml')).toEqual(
content('tests/__data__/expected/sites/parse-channels.channels.xml') 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) { function content(filepath) {
return fs.readFileSync(path.resolve(filepath), { return fs.readFileSync(path.resolve(filepath), {
encoding: 'utf8' encoding: 'utf8'