epg-grabber/src/index.js

90 lines
2.5 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)
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
.fetchData(item, config)
2021-03-13 13:11:33 +01:00
.then(response => {
2021-04-17 12:28:56 +02:00
if (options.debug) {
console.timeEnd(' Response Time')
console.time(' Parsing Time')
}
if (!item.channel.logo && config.logo) {
item.channel.logo = config.logo({
channel: item.channel,
content: response.data.toString(),
buffer: response.data
})
}
2021-03-13 13:11:33 +01:00
2021-04-18 14:10:58 +02:00
const parsed = utils.parsePrograms({ response, item, config }).map(program => {
program.lang = program.lang || item.channel.lang || undefined
return program
})
2021-03-21 19:17:23 +01:00
console.log(
` ${config.site} - ${item.channel.xmltv_id} - ${item.date.format('MMM D, YYYY')} (${
2021-04-18 14:10:58 +02:00
parsed.length
2021-03-21 19:17:23 +01:00
} programs)`
)
2021-04-18 14:10:58 +02:00
programs = programs.concat(parsed)
2021-03-13 13:11:33 +01:00
})
2021-04-17 11:24:28 +02:00
.then(() => {
2021-04-17 12:28:56 +02:00
if (options.debug) console.timeEnd(' Parsing Time')
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()