Files
epg/scripts/grab.js

82 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-10-06 04:56:56 +03:00
#! /usr/bin/env node
const { Command } = require('commander')
const grabber = require('epg-grabber')
2021-10-12 00:34:50 +03:00
const parser = require('./parser')
const file = require('./file')
2021-10-06 04:56:56 +03:00
2021-10-12 00:34:50 +03:00
const program = new Command()
2021-10-06 04:56:56 +03:00
program
2021-10-11 23:45:23 +03:00
.requiredOption('--site <site>', 'Site domain')
.option('--country <country>', 'Filter channels by country ISO code')
.option('--language <language>', 'Filter channels by language ISO code')
2021-10-07 02:08:31 +03:00
.option('--days <days>', 'Number of days for which to grab the program', parseInteger, 1)
2021-10-11 23:45:23 +03:00
.option('--output <output>', 'Path to output file', 'guide.xml')
2021-10-06 04:56:56 +03:00
.parse(process.argv)
async function main() {
console.log('Starting...')
2021-10-06 21:31:51 +03:00
console.time('Done in')
2021-10-06 04:56:56 +03:00
2021-10-11 23:45:23 +03:00
const options = program.opts()
const channelsPath = `sites/${options.site}.channels.xml`
console.log(`Loading '${channelsPath}'...`)
2021-10-12 00:34:50 +03:00
const channelsFile = file.read(channelsPath)
2021-10-12 01:23:31 +03:00
let parsed = parser.parseChannels(channelsFile)
let channels = parsed.groups.reduce((acc, curr) => {
acc = acc.concat(curr.channels)
return acc
}, [])
2021-10-11 23:45:23 +03:00
channels = filterChannels(channels, options)
2021-10-06 04:56:56 +03:00
console.log('Parsing:')
let programs = []
for (let channel of channels) {
2021-10-12 00:34:50 +03:00
const config = file.load(`sites/${channel.site}.config.js`)
2021-10-07 02:08:31 +03:00
config.days = options.days
2021-10-06 17:52:30 +03:00
await grabber
.grab(channel, config, (item, err) => {
2021-10-06 04:56:56 +03:00
console.log(
2021-10-13 06:47:11 +03:00
` ${item.channel.site} (${options.country.toUpperCase()}) - ${
item.channel.xmltv_id
} - ${item.date.format('MMM D, YYYY')} (${item.programs.length} programs)`
2021-10-06 04:56:56 +03:00
)
2021-10-06 17:52:30 +03:00
if (err) {
console.log(` Error: ${err.message}`)
}
})
.then(results => {
programs = programs.concat(results)
})
2021-10-06 21:31:51 +03:00
.catch(err => {
console.log(` Error: ${err.message}`)
})
2021-10-06 04:56:56 +03:00
}
const xml = grabber.convertToXMLTV({ channels, programs })
2021-10-12 00:34:50 +03:00
file.write(options.output, xml)
2021-10-06 04:56:56 +03:00
console.log(`File '${options.output}' successfully saved`)
2021-10-06 21:31:51 +03:00
console.timeEnd(`Done in`)
2021-10-06 04:56:56 +03:00
return true
}
2021-10-11 23:45:23 +03:00
function filterChannels(channels, options) {
return channels.filter(channel => {
let result = true
2021-10-12 21:44:23 +03:00
if (options.country) result = channel.country === options.country.toUpperCase()
2021-10-11 23:45:23 +03:00
if (options.language) result = channel.lang === options.language
return result
})
}
2021-10-06 04:56:56 +03:00
2021-10-07 02:08:31 +03:00
function parseInteger(val) {
return val ? parseInt(val) : null
}
2021-10-12 00:34:50 +03:00
main()