epg-grabber/bin/epg-grabber.js

71 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-10-05 23:36:28 +02:00
#! /usr/bin/env node
const { Command } = require('commander')
const program = new Command()
const path = require('path')
const grabber = require('../src/index')
const utils = require('../src/utils')
const { name, version, description } = require('../package.json')
2021-10-06 02:43:10 +02:00
const merge = require('lodash.merge')
2021-10-05 23:36:28 +02:00
program
.name(name)
.version(version, '-v, --version')
.description(description)
2021-10-06 02:02:06 +02:00
.requiredOption('-c, --config <config>', 'Path to [site].config.js file')
2021-10-06 02:52:34 +02:00
.option('-o, --output <output>', 'Path to output file')
2021-10-05 23:36:28 +02:00
.option('--channels <channels>', 'Path to channels.xml file')
2021-10-06 02:52:34 +02:00
.option('--lang <lang>', 'Set default language for all programs')
.option('--days <days>', 'Number of days for which to grab the program', parseInteger)
.option('--delay <delay>', 'Delay between requests (in mileseconds)', parseInteger)
2021-10-05 23:36:28 +02:00
.option('--debug', 'Enable debug mode', false)
.parse(process.argv)
const options = program.opts()
async function main() {
console.log('\r\nStarting...')
2021-10-06 02:02:06 +02:00
console.log(`Loading '${options.config}'...`)
2021-10-06 02:43:10 +02:00
let config = require(path.resolve(options.config))
config = merge(config, options)
2021-10-06 02:02:06 +02:00
2021-10-05 23:36:28 +02:00
if (options.channels) config.channels = options.channels
else if (config.channels)
config.channels = path.join(path.dirname(options.config), config.channels)
else throw new Error("The required 'channels' property is missing")
let programs = []
const channels = utils.parseChannels(config.channels)
for (let channel of channels) {
2021-10-06 15:38:16 +02:00
await grabber
.grab(channel, config, (data, err) => {
2021-10-06 17:12:11 +02:00
console.log(
` ${config.site} - ${data.channel.xmltv_id} - ${data.date.format('MMM D, YYYY')} (${
data.programs.length
} programs)`
)
2021-10-06 15:38:16 +02:00
if (err) {
console.log(` Error: ${err.message}`)
}
2021-10-05 23:36:28 +02:00
})
2021-10-06 15:38:16 +02:00
.then(results => {
2021-10-05 23:36:28 +02:00
programs = programs.concat(results)
})
}
const xml = utils.convertToXMLTV({ config, channels, programs })
2021-10-14 18:59:46 +02:00
const outputPath = config.output || 'guide.xml'
utils.writeToFile(outputPath, xml)
2021-10-05 23:36:28 +02:00
2021-10-14 18:59:46 +02:00
console.log(`File '${outputPath}' successfully saved`)
2021-10-05 23:36:28 +02:00
console.log('Finish')
}
main()
2021-10-06 02:43:10 +02:00
function parseInteger(val) {
return val ? parseInt(val) : null
}