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

84 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-03-10 17:37:12 +03:00
#! /usr/bin/env node
const fs = require('fs')
const path = require('path')
const axios = require('axios')
2021-03-10 21:31:32 +03:00
const axiosDelayAdapter = require('axios-delay').default
2021-03-10 17:37:12 +03:00
const utils = require('./utils')
const { Command } = require('commander')
const program = new Command()
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
dayjs.extend(utc)
program
.version('0.1.0', '-v, --version')
.name('epg-grabber')
.description('EPG grabber')
.usage('[options] [file-or-url]')
2021-03-11 18:17:24 +03:00
.option('-c, --config <config>', 'Path to [site].config.xml file', './')
.option('-s, --sites <sites>', 'Path to sites folder', './sites')
2021-03-10 17:37:12 +03:00
.parse(process.argv)
const options = program.opts()
const config = utils.parseConfig(options.config)
2021-03-10 21:31:32 +03:00
const sites = utils.loadSites(options.sites)
2021-03-10 17:37:12 +03:00
2021-03-10 21:31:32 +03:00
const client = axios.create({
adapter: axiosDelayAdapter(axios.defaults.adapter),
headers: { 'User-Agent': config.userAgent }
})
2021-03-10 17:37:12 +03:00
2021-03-10 21:31:32 +03:00
async function main() {
2021-03-11 18:17:24 +03:00
console.log('\r\nStarting...')
console.log(`Loading '${options.config}'...`)
2021-03-10 17:37:12 +03:00
const d = dayjs.utc()
const dates = Array.from({ length: config.days }, (_, i) => d.add(i, 'd'))
const channels = config.channels
2021-03-10 21:31:32 +03:00
const requests = []
2021-03-10 17:37:12 +03:00
channels.forEach(channel => {
const site = sites[channel.site]
dates.forEach(date => {
2021-03-10 21:31:32 +03:00
requests.push({
2021-03-10 21:54:06 +03:00
url: site.url({ date, channel }),
2021-03-11 17:06:43 +03:00
date,
2021-03-10 21:54:06 +03:00
channel
2021-03-10 21:31:32 +03:00
})
2021-03-10 17:37:12 +03:00
})
})
2021-03-11 18:17:24 +03:00
console.log('Parsing:')
2021-03-10 21:31:32 +03:00
let programs = []
for (let request of requests) {
2021-03-10 21:54:06 +03:00
const progs = await client
.get(request.url)
2021-03-10 21:31:32 +03:00
.then(response => {
const channel = request.channel
2021-03-10 21:54:06 +03:00
const site = sites[channel.site]
2021-03-10 17:37:12 +03:00
2021-03-11 18:17:24 +03:00
const programs = site.parser({
2021-03-11 17:06:43 +03:00
channel,
content: response.data,
date: request.date
})
2021-03-11 18:17:24 +03:00
console.log(` ${channel.site} - ${channel.xmltv_id} (${programs.length} programs)`)
return programs
2021-03-10 21:31:32 +03:00
})
.then(utils.sleep(3000))
2021-03-10 21:54:06 +03:00
.catch(console.log)
2021-03-10 21:31:32 +03:00
programs = programs.concat(progs)
}
2021-03-10 21:54:06 +03:00
2021-03-10 21:31:32 +03:00
const xml = utils.convertToXMLTV({ channels, programs })
2021-03-11 18:17:24 +03:00
utils.createDir(path.dirname(config.filename))
utils.writeToFile(config.filename, xml)
console.log(`File '${config.filename}' successfully updated`)
console.log('Finish')
2021-03-10 17:37:12 +03:00
}
main()