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-03-21 19:17:23 +01:00
|
|
|
if (options.debug) console.time(' time')
|
2021-04-02 19:12:27 +02:00
|
|
|
const progs = await utils
|
|
|
|
.fetchData(item, config)
|
2021-03-13 13:11:33 +01:00
|
|
|
.then(response => {
|
2021-04-01 03:48:30 +02:00
|
|
|
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-03-21 19:17:23 +01:00
|
|
|
const programs = utils.parsePrograms({ response, item, config })
|
|
|
|
console.log(
|
|
|
|
` ${config.site} - ${item.channel.xmltv_id} - ${item.date.format('MMM D, YYYY')} (${
|
|
|
|
programs.length
|
|
|
|
} programs)`
|
|
|
|
)
|
|
|
|
|
2021-04-01 03:48:30 +02:00
|
|
|
return programs.map(program => {
|
|
|
|
program.lang = program.lang || item.channel.lang || undefined
|
|
|
|
return program
|
|
|
|
})
|
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 05:22:35 +02:00
|
|
|
.finally(() => {
|
|
|
|
if (options.debug) console.timeEnd(' time')
|
|
|
|
utils.sleep(config.delay)
|
|
|
|
})
|
2021-03-13 13:11:33 +01:00
|
|
|
|
|
|
|
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()
|