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')
program
.option('--channels <channels>', 'Path to channels.xml file')
.option('--output <output>', 'Path to output file', 'guide.xml')
.requiredOption('--site <site>', 'Site domain')
.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('--output <output>', 'Path to output file', 'guide.xml')
.parse(process.argv)
const options = program.opts()
async function main() {
console.log('Starting...')
console.time('Done in')
const buffer = {}
const channels = parseChannels(options.channels).filter(channel => {
if (!buffer[channel.xmltv_id]) {
buffer[channel.xmltv_id] = true
return true
}
return false
})
const options = program.opts()
const channelsPath = `sites/${options.site}.channels.xml`
console.log(`Loading '${channelsPath}'...`)
let channels = parseChannels(path.resolve(channelsPath))
channels = filterChannels(channels, options)
console.log('Parsing:')
let programs = []
for (let channel of channels) {
const configPath = `sites/${channel.site}.config.js`
const config = require(path.resolve(configPath))
const configPath = path.resolve(`sites/${channel.site}.config.js`)
const config = require(configPath)
config.days = options.days
await grabber
.grab(channel, config, (item, err) => {
console.log(
` ${item.channel.xmltv_id} - ${item.channel.site} (${
item.channel.lang
}) - ${item.date.format('MMM D, YYYY')} (${item.programs.length} programs)`
` ${item.channel.site} - ${item.channel.xmltv_id} - ${item.date.format(
'MMM D, YYYY'
)} (${item.programs.length} programs)`
)
if (err) {
@ -65,23 +64,39 @@ async function main() {
main()
function parseChannels(filename) {
if (!filename) throw new Error('Path to [site].channels.xml is missing')
console.log(`Loading '${filename}'...`)
function filterChannels(channels, options) {
return channels.filter(channel => {
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 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
.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
let output = []
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) {