Update grab.js

This commit is contained in:
Aleksandr Statciuk 2021-10-11 23:45:23 +03:00
parent b961162e25
commit a3133a2b26

View file

@ -8,38 +8,37 @@ const fs = require('fs')
const convert = require('xml-js') const convert = require('xml-js')
program program
.option('--channels <channels>', 'Path to channels.xml file') .requiredOption('--site <site>', 'Site domain')
.option('--output <output>', 'Path to output file', 'guide.xml') .option('--country <country>', 'Filter channels by country ISO code')
.option('--language <language>', 'Filter channels by language ISO code')
.option('--days <days>', 'Number of days for which to grab the program', parseInteger, 1) .option('--days <days>', 'Number of days for which to grab the program', parseInteger, 1)
.option('--output <output>', 'Path to output file', 'guide.xml')
.parse(process.argv) .parse(process.argv)
const options = program.opts()
async function main() { async function main() {
console.log('Starting...') console.log('Starting...')
console.time('Done in') console.time('Done in')
const buffer = {} const options = program.opts()
const channels = parseChannels(options.channels).filter(channel => {
if (!buffer[channel.xmltv_id]) { const channelsPath = `sites/${options.site}.channels.xml`
buffer[channel.xmltv_id] = true
return true console.log(`Loading '${channelsPath}'...`)
} let channels = parseChannels(path.resolve(channelsPath))
return false channels = filterChannels(channels, options)
})
console.log('Parsing:') console.log('Parsing:')
let programs = [] let programs = []
for (let channel of channels) { for (let channel of channels) {
const configPath = `sites/${channel.site}.config.js` const configPath = path.resolve(`sites/${channel.site}.config.js`)
const config = require(path.resolve(configPath)) const config = require(configPath)
config.days = options.days config.days = options.days
await grabber await grabber
.grab(channel, config, (item, err) => { .grab(channel, config, (item, err) => {
console.log( console.log(
` ${item.channel.xmltv_id} - ${item.channel.site} (${ ` ${item.channel.site} - ${item.channel.xmltv_id} - ${item.date.format(
item.channel.lang 'MMM D, YYYY'
}) - ${item.date.format('MMM D, YYYY')} (${item.programs.length} programs)` )} (${item.programs.length} programs)`
) )
if (err) { if (err) {
@ -65,23 +64,39 @@ async function main() {
main() main()
function parseChannels(filename) { function filterChannels(channels, options) {
if (!filename) throw new Error('Path to [site].channels.xml is missing') return channels.filter(channel => {
console.log(`Loading '${filename}'...`) let result = true
if (options.country) result = channel.country === options.country
if (options.language) result = channel.lang === options.language
return result
})
}
function parseChannels(filename) {
const xml = fs.readFileSync(path.resolve(filename), { encoding: 'utf-8' }) const xml = fs.readFileSync(path.resolve(filename), { encoding: 'utf-8' })
const result = convert.xml2js(xml) const result = convert.xml2js(xml)
const channels = result.elements.find(el => el.name === 'channels') const siteTag = result.elements.find(el => el.name === 'site')
const channelsTags = siteTag.elements.filter(el => el.name === 'channels')
return channels.elements let output = []
.filter(el => el.name === 'channel')
.map(el => {
const channel = el.attributes
if (!el.elements) throw new Error(`Channel '${channel.xmltv_id}' has no valid name`)
channel.name = el.elements.find(el => el.type === 'text').text
return channel channelsTags.forEach(channelsTag => {
}) const channels = channelsTag.elements
.filter(el => el.name === 'channel')
.map(el => {
const channel = el.attributes
if (!el.elements) throw new Error(`Channel '${channel.xmltv_id}' has no valid name`)
channel.name = el.elements.find(el => el.type === 'text').text
channel.country = channelsTag.attributes.country
channel.site = siteTag.attributes.site
return channel
})
output = output.concat(channels)
})
return output
} }
function writeToFile(filename, data) { function writeToFile(filename, data) {