Files
epg/bin/epg-grabber/index.js

78 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-03-10 17:37:12 +03:00
#! /usr/bin/env node
const { Command } = require('commander')
const program = new Command()
2021-03-12 17:17:57 +03:00
const utils = require('./utils')
2021-03-12 00:51:53 +03:00
2021-03-10 17:37:12 +03:00
program
.version('0.1.0', '-v, --version')
.name('epg-grabber')
.description('EPG grabber')
2021-03-12 17:17:57 +03:00
.option('-c, --config <config>', 'Path to [site].config.js file')
2021-03-10 17:37:12 +03:00
.parse(process.argv)
2021-03-10 21:31:32 +03:00
async function main() {
2021-03-11 18:17:24 +03:00
console.log('\r\nStarting...')
2021-03-12 17:17:57 +03:00
2021-03-12 19:04:56 +03:00
const options = program.opts()
2021-03-12 17:17:57 +03:00
const config = utils.loadConfig(options.config)
const client = utils.createHttpClient(config)
const channels = utils.parseChannels(config.channels)
2021-03-12 19:04:56 +03:00
const utcDate = utils.getUTCDate()
const dates = Array.from({ length: config.days }, (_, i) => utcDate.add(i, 'd'))
2021-03-12 17:17:57 +03:00
const queue = []
2021-03-10 17:37:12 +03:00
channels.forEach(channel => {
dates.forEach(date => {
2021-03-12 19:04:56 +03:00
queue.push({ date, channel })
2021-03-10 17:37:12 +03:00
})
})
2021-03-10 21:31:32 +03:00
let programs = []
2021-03-12 19:04:56 +03:00
console.log('Parsing:')
2021-03-12 17:17:57 +03:00
for (let item of queue) {
2021-03-12 19:04:56 +03:00
const url = config.url(item)
2021-03-10 21:54:06 +03:00
const progs = await client
2021-03-12 19:04:56 +03:00
.get(url)
2021-03-10 21:31:32 +03:00
.then(response => {
2021-03-12 17:17:57 +03:00
const parserOptions = Object.assign({}, item, config, { content: response.data })
2021-03-12 17:51:59 +03:00
const programs = config
.parser(parserOptions)
.filter(i => i)
.map(p => {
p.channel = item.channel.xmltv_id
return p
})
2021-03-11 18:17:24 +03:00
2021-03-12 01:37:08 +03:00
console.log(
2021-03-12 17:17:57 +03:00
` ${item.channel.site} - ${item.channel.xmltv_id} - ${item.date.format(
'MMM D, YYYY'
)} (${programs.length} programs)`
2021-03-12 01:37:08 +03:00
)
2021-03-11 18:17:24 +03:00
return programs
2021-03-10 21:31:32 +03:00
})
2021-03-12 17:44:15 +03:00
.then(utils.sleep(config.delay))
2021-03-12 18:44:51 +03:00
.catch(err => {
console.log(
` ${item.channel.site} - ${item.channel.xmltv_id} - ${item.date.format(
'MMM D, YYYY'
)} (0 programs)`
)
console.log(` Error: ${err.message}`)
})
2021-03-10 21:54:06 +03:00
2021-03-10 21:31:32 +03:00
programs = programs.concat(progs)
}
2021-03-10 21:54:06 +03:00
2021-03-12 00:07:14 +03:00
const xml = utils.convertToXMLTV({ config, channels, programs })
2021-03-12 17:17:57 +03:00
const outputDir = utils.getDirectory(config.output)
utils.createDir(outputDir)
utils.writeToFile(config.output, xml)
console.log(`File '${config.output}' successfully saved`)
2021-03-12 17:42:01 +03:00
console.log('Finish')
2021-03-10 17:37:12 +03:00
}
main()