epg-grabber/src/index.js

81 lines
2.1 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')
.option('-d, --debug', 'Enable debug mode')
.parse(process.argv)
async function main() {
console.log('\r\nStarting...')
const options = program.opts()
const config = utils.loadConfig(options.config)
if (options.debug) console.log(config)
const client = utils.createHttpClient(config)
2021-03-19 19:26:32 +01:00
const channels = utils.parseChannels(config, 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) {
const url = config.url(item)
const progs = await client
.get(url)
.then(response => {
2021-03-13 13:49:02 +01:00
const parserOptions = Object.assign({}, item, config, {
content: response.data
})
2021-03-13 13:11:33 +01:00
const programs = config
.parser(parserOptions)
.filter(i => i)
.map(p => {
p.channel = item.channel.xmltv_id
return p
})
console.log(
2021-03-13 14:42:18 +01:00
` ${config.site} - ${item.channel.xmltv_id} - ${item.date.format('MMM D, YYYY')} (${
programs.length
} programs)`
2021-03-13 13:11:33 +01:00
)
return programs
})
.then(utils.sleep(config.delay))
.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}`)
})
programs = programs.concat(progs)
}
const xml = utils.convertToXMLTV({ config, channels, programs })
utils.writeToFile(config.output, xml)
console.log(`File '${config.output}' successfully saved`)
console.log('Finish')
}
main()