epg-grabber/src/index.js

75 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-03-13 13:11:33 +01:00
#! /usr/bin/env node
const { Command } = require('commander')
const program = new Command()
const utils = require('./utils')
2021-03-13 13:49:02 +01:00
const { name, version, description } = require('../package.json')
2021-03-13 13:11:33 +01:00
program
2021-03-13 13:49:02 +01:00
.name(name)
.version(version, '-v, --version')
.description(description)
2021-03-13 13:11:33 +01:00
.option('-c, --config <config>', 'Path to [site].config.js file')
2021-09-15 10:19:45 +02:00
.option('-o, --output <output>', 'Path to output file', 'guide.xml')
.option('--channels <channels>', 'Path to channels.xml file')
.option('--lang <lang>', 'Set default language for all programs', 'en')
.option('--days <days>', 'Number of days for which to grab the program', 1)
.option('--delay <delay>', 'Delay between requests (in mileseconds)', 3000)
.option('--debug', 'Enable debug mode', false)
2021-03-13 13:11:33 +01:00
.parse(process.argv)
2021-08-21 18:10:58 +02:00
const options = program.opts()
2021-09-15 10:19:45 +02:00
const config = utils.loadConfig(options)
2021-08-21 18:10:58 +02:00
2021-03-13 13:11:33 +01:00
async function main() {
console.log('\r\nStarting...')
2021-03-19 20:19:48 +01:00
const channels = utils.parseChannels(config.channels)
2021-03-13 13:11:33 +01:00
const utcDate = utils.getUTCDate()
const dates = Array.from({ length: config.days }, (_, i) => utcDate.add(i, 'd'))
const queue = []
channels.forEach(channel => {
dates.forEach(date => {
queue.push({ date, channel })
})
})
let programs = []
console.log('Parsing:')
for (let item of queue) {
2021-04-17 12:28:56 +02:00
if (options.debug) console.time(' Response Time')
2021-04-18 14:10:58 +02:00
await utils
2021-08-21 18:10:58 +02:00
.buildRequest(item, config)
.then(utils.fetchData)
2021-08-23 12:45:50 +02:00
.then(async response => {
2021-08-21 18:10:58 +02:00
if (options.debug) console.timeEnd(' Response Time')
2021-08-23 12:45:50 +02:00
if (options.debug) console.time(' Parsing Time')
const results = await utils.parseResponse(item, response, config)
if (options.debug) console.timeEnd(' Parsing Time')
2021-08-21 18:10:58 +02:00
programs = programs.concat(results)
2021-04-17 11:24:28 +02:00
})
.then(utils.sleep(config.delay))
2021-03-13 13:11:33 +01:00
.catch(err => {
console.log(
2021-03-13 14:42:18 +01:00
` ${config.site} - ${item.channel.xmltv_id} - ${item.date.format(
2021-03-13 13:11:33 +01:00
'MMM D, YYYY'
)} (0 programs)`
)
console.log(` Error: ${err.message}`)
2021-04-17 12:28:56 +02:00
if (options.debug) {
console.timeEnd(' Response Time')
console.timeEnd(' Parsing Time')
}
2021-04-17 05:22:35 +02:00
})
2021-03-13 13:11:33 +01:00
}
const xml = utils.convertToXMLTV({ config, channels, programs })
utils.writeToFile(config.output, xml)
console.log(`File '${config.output}' successfully saved`)
console.log('Finish')
}
main()