mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-09 16:40:07 -04:00
Create channels/parse.test.js
This commit is contained in:
parent
4cfbdaac3e
commit
c86d32c98d
4 changed files with 55 additions and 67 deletions
|
@ -1,8 +1,7 @@
|
||||||
const { db, logger } = require('../../core')
|
const { logger, file, xml } = require('../../core')
|
||||||
const { Command } = require('commander')
|
const { Command } = require('commander')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
const fs = require('fs')
|
|
||||||
|
|
||||||
const program = new Command()
|
const program = new Command()
|
||||||
program
|
program
|
||||||
|
@ -14,37 +13,25 @@ program
|
||||||
const options = program.opts()
|
const options = program.opts()
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
await db.channels.load()
|
|
||||||
const config = require(path.resolve(options.config))
|
const config = require(path.resolve(options.config))
|
||||||
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 channels = config.channels(args)
|
||||||
if (isPromise(channels)) {
|
if (isPromise(channels)) {
|
||||||
channels = await channels
|
channels = await channels
|
||||||
}
|
}
|
||||||
channels = _.uniqBy(channels, 'site_id')
|
|
||||||
|
|
||||||
const siteChannels = await db.channels.find({ site: config.site })
|
|
||||||
for (const channel of channels) {
|
|
||||||
if (channel.xmltv_id) continue
|
|
||||||
const data = siteChannels.find(c => c.site_id === channel.site_id.toString())
|
|
||||||
if (data) {
|
|
||||||
channel.xmltv_id = data.xmltv_id
|
|
||||||
channel.name = data.name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
channels = _.sortBy(channels, 'xmltv_id')
|
channels = _.sortBy(channels, 'xmltv_id')
|
||||||
|
|
||||||
const xml = json2xml(channels, config.site)
|
const dir = file.dirname(options.config)
|
||||||
|
const outputFilepath = options.output || `${dir}/${config.site}.channels.xml`
|
||||||
|
|
||||||
const dir = path.parse(options.config).dir
|
const output = xml.create(channels, config.site)
|
||||||
const output = options.output || `${dir}/${config.site}.channels.xml`
|
|
||||||
|
|
||||||
fs.writeFileSync(path.resolve(output), xml)
|
await file.write(outputFilepath, output)
|
||||||
|
|
||||||
logger.info(`File '${output}' successfully saved`)
|
logger.info(`File '${output}' successfully saved`)
|
||||||
}
|
}
|
||||||
|
@ -54,51 +41,3 @@ main()
|
||||||
function isPromise(promise) {
|
function isPromise(promise) {
|
||||||
return !!promise && typeof promise.then === 'function'
|
return !!promise && typeof promise.then === 'function'
|
||||||
}
|
}
|
||||||
|
|
||||||
function json2xml(items, site) {
|
|
||||||
let output = `<?xml version="1.0" encoding="UTF-8"?>\r\n<site site="${site}">\r\n <channels>\r\n`
|
|
||||||
|
|
||||||
items.forEach(channel => {
|
|
||||||
const logo = channel.logo ? ` logo="${channel.logo}"` : ''
|
|
||||||
const xmltv_id = channel.xmltv_id || ''
|
|
||||||
const lang = channel.lang || ''
|
|
||||||
const site_id = channel.site_id || ''
|
|
||||||
output += ` <channel lang="${lang}" xmltv_id="${escapeString(
|
|
||||||
xmltv_id
|
|
||||||
)}" site_id="${site_id}"${logo}>${escapeString(channel.name)}</channel>\r\n`
|
|
||||||
})
|
|
||||||
|
|
||||||
output += ` </channels>\r\n</site>\r\n`
|
|
||||||
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
|
|
||||||
function escapeString(string, defaultValue = '') {
|
|
||||||
if (!string) return defaultValue
|
|
||||||
|
|
||||||
const regex = new RegExp(
|
|
||||||
'((?:[\0-\x08\x0B\f\x0E-\x1F\uFFFD\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))|([\\x7F-\\x84]|[\\x86-\\x9F]|[\\uFDD0-\\uFDEF]|(?:\\uD83F[\\uDFFE\\uDFFF])|(?:\\uD87F[\\uDF' +
|
|
||||||
'FE\\uDFFF])|(?:\\uD8BF[\\uDFFE\\uDFFF])|(?:\\uD8FF[\\uDFFE\\uDFFF])|(?:\\uD93F[\\uDFFE\\uD' +
|
|
||||||
'FFF])|(?:\\uD97F[\\uDFFE\\uDFFF])|(?:\\uD9BF[\\uDFFE\\uDFFF])|(?:\\uD9FF[\\uDFFE\\uDFFF])' +
|
|
||||||
'|(?:\\uDA3F[\\uDFFE\\uDFFF])|(?:\\uDA7F[\\uDFFE\\uDFFF])|(?:\\uDABF[\\uDFFE\\uDFFF])|(?:\\' +
|
|
||||||
'uDAFF[\\uDFFE\\uDFFF])|(?:\\uDB3F[\\uDFFE\\uDFFF])|(?:\\uDB7F[\\uDFFE\\uDFFF])|(?:\\uDBBF' +
|
|
||||||
'[\\uDFFE\\uDFFF])|(?:\\uDBFF[\\uDFFE\\uDFFF])(?:[\\0-\\t\\x0B\\f\\x0E-\\u2027\\u202A-\\uD7FF\\' +
|
|
||||||
'uE000-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|' +
|
|
||||||
'(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF]))',
|
|
||||||
'g'
|
|
||||||
)
|
|
||||||
|
|
||||||
string = String(string || '').replace(regex, '')
|
|
||||||
|
|
||||||
return string
|
|
||||||
.replace(/&/g, '&')
|
|
||||||
.replace(/</g, '<')
|
|
||||||
.replace(/>/g, '>')
|
|
||||||
.replace(/"/g, '"')
|
|
||||||
.replace(/'/g, ''')
|
|
||||||
.replace(/\n|\r/g, ' ')
|
|
||||||
.replace(/ +/g, ' ')
|
|
||||||
.trim()
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { json2xml }
|
|
||||||
|
|
|
@ -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>
|
19
tests/__data__/input/sites/parse-channels.config.js
Normal file
19
tests/__data__/input/sites/parse-channels.config.js
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
module.exports = {
|
||||||
|
site: 'parse-channels.com',
|
||||||
|
url() {
|
||||||
|
return `https://parse-channels.com`
|
||||||
|
},
|
||||||
|
parser() {
|
||||||
|
return []
|
||||||
|
},
|
||||||
|
channels() {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
lang: 'en',
|
||||||
|
xmltv_id: 'CNNInternational.us',
|
||||||
|
site_id: 140,
|
||||||
|
name: 'CNN International'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
24
tests/commands/channels/parse.test.js
Normal file
24
tests/commands/channels/parse.test.js
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
const { execSync } = require('child_process')
|
||||||
|
const fs = require('fs-extra')
|
||||||
|
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' }
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('can parse channels', () => {
|
||||||
|
expect(content('tests/__data__/output/channels.xml')).toEqual(
|
||||||
|
content('tests/__data__/expected/sites/parse-channels.channels.xml')
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
function content(filepath) {
|
||||||
|
return fs.readFileSync(path.resolve(filepath), {
|
||||||
|
encoding: 'utf8'
|
||||||
|
})
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue