epg-grabber/bin/epg-grabber.js

158 lines
4.9 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()
2022-01-19 13:54:42 +01:00
const { merge } = require('lodash')
2022-03-15 12:52:59 +01:00
const { gzip } = require('node-gzip')
2022-06-11 20:01:39 +02:00
const file = require('../src/file')
2022-06-16 14:20:19 +02:00
const { EPGGrabber, parseChannels, generateXMLTV } = require('../src/index')
2022-06-11 20:01:39 +02:00
const { create: createLogger } = require('../src/logger')
2022-06-16 14:07:56 +02:00
const { parseNumber, getUTCDate } = require('../src/utils')
2022-06-11 20:01:39 +02:00
const { name, version, description } = require('../package.json')
2022-08-29 03:14:12 +02:00
const _ = require('lodash')
2022-06-16 14:07:56 +02:00
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
2023-05-21 20:05:23 +02:00
const { TaskQueue } = require('cwait')
2022-06-16 14:07:56 +02:00
dayjs.extend(utc)
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')
2023-01-10 09:20:36 +01:00
.option('--days <days>', 'Number of days for which to grab the program', parseNumber)
2023-01-13 09:22:30 +01:00
.option('--delay <delay>', 'Delay between requests (in milliseconds)', parseNumber)
.option('--timeout <timeout>', 'Set a timeout for each request (in milliseconds)', parseNumber)
2023-05-21 20:05:23 +02:00
.option(
'--max-connections <maxConnections>',
'Set a limit on the number of concurrent requests per site',
parseNumber
)
2022-03-29 14:59:58 +02:00
.option(
2022-05-03 18:18:52 +02:00
'--cache-ttl <cacheTtl>',
2022-03-29 14:59:58 +02:00
'Maximum time for storing each request (in milliseconds)',
2022-06-16 14:07:56 +02:00
parseNumber
2022-03-29 14:59:58 +02:00
)
2022-03-15 12:52:59 +01:00
.option('--gzip', 'Compress the output', false)
2021-10-05 23:36:28 +02:00
.option('--debug', 'Enable debug mode', false)
2022-03-06 14:18:05 +01:00
.option('--curl', 'Display request as CURL', false)
2021-11-16 15:59:26 +01:00
.option('--log <log>', 'Path to log file')
.option('--log-level <level>', 'Set log level', 'info')
2021-10-05 23:36:28 +02:00
.parse(process.argv)
const options = program.opts()
2022-06-11 20:01:39 +02:00
const logger = createLogger(options)
2021-11-16 15:59:26 +01:00
2021-10-05 23:36:28 +02:00
async function main() {
2021-11-16 15:59:26 +01:00
logger.info('Starting...')
2021-10-05 23:36:28 +02:00
2021-11-16 15:59:26 +01:00
logger.info(`Loading '${options.config}'...`)
2022-06-11 20:01:39 +02:00
let config = require(file.resolve(options.config))
2021-11-18 15:31:47 +01:00
config = merge(config, {
days: options.days,
debug: options.debug,
2022-03-15 12:52:59 +01:00
gzip: options.gzip,
2022-03-06 14:18:05 +01:00
curl: options.curl,
2021-11-18 15:31:47 +01:00
lang: options.lang,
delay: options.delay,
2023-05-21 20:05:23 +02:00
maxConnections: options.maxConnections,
2022-05-03 18:18:52 +02:00
request: {}
2021-11-18 15:31:47 +01:00
})
2021-10-06 02:02:06 +02:00
2022-05-03 18:18:52 +02:00
if (options.timeout) config.request.timeout = options.timeout
if (options.cacheTtl) config.request.cache.ttl = options.cacheTtl
2021-10-05 23:36:28 +02:00
if (options.channels) config.channels = options.channels
else if (config.channels)
2022-06-11 20:01:39 +02:00
config.channels = file.join(file.dirname(options.config), config.channels)
2021-10-05 23:36:28 +02:00
else throw new Error("The required 'channels' property is missing")
2021-11-16 15:59:26 +01:00
if (!config.channels) return logger.error('Path to [site].channels.xml is missing')
logger.info(`Loading '${config.channels}'...`)
2022-06-11 20:01:39 +02:00
const grabber = new EPGGrabber(config)
const channelsXML = file.read(config.channels)
2023-05-11 03:12:49 +02:00
const { channels: parsedChannels } = parseChannels(channelsXML)
let template = options.output || config.output
const variables = file.templateVariables(template)
const groups = _.groupBy(parsedChannels, channel => {
let groupId = ''
for (let key in channel) {
if (variables.includes(key)) {
groupId += channel[key]
}
2022-06-11 20:01:39 +02:00
}
2023-05-11 03:12:49 +02:00
return groupId
})
for (let groupId in groups) {
const channels = groups[groupId]
let programs = []
let i = 1
let days = config.days || 1
2023-05-21 20:05:23 +02:00
const maxConnections = config.maxConnections || 1
2023-05-11 03:12:49 +02:00
const total = channels.length * days
const utcDate = getUTCDate()
const dates = Array.from({ length: days }, (_, i) => utcDate.add(i, 'd'))
2023-05-21 20:05:23 +02:00
const taskQueue = new TaskQueue(Promise, maxConnections)
let queue = []
2023-05-11 03:12:49 +02:00
for (let channel of channels) {
if (!channel.logo && config.logo) {
channel.logo = await grabber.loadLogo(channel)
}
for (let date of dates) {
2023-05-21 20:05:23 +02:00
queue.push({ channel, date })
2023-05-11 03:12:49 +02:00
}
}
2023-05-21 20:05:23 +02:00
await Promise.all(
queue.map(
taskQueue.wrap(async ({ channel, date }) => {
await grabber
.grab(channel, date, (data, err) => {
logger.info(
`[${i}/${total}] ${config.site} - ${data.channel.xmltv_id} - ${dayjs
.utc(data.date)
.format('MMM D, YYYY')} (${data.programs.length} programs)`
)
if (err) logger.error(err.message)
if (i < total) i++
})
.then(results => {
programs = programs.concat(results)
})
})
)
)
2023-05-11 03:12:49 +02:00
programs = _.uniqBy(programs, p => p.start + p.channel)
const xml = generateXMLTV({ channels, programs })
let outputPath = file.templateFormat(template, channels[0])
if (options.gzip) {
outputPath = outputPath || 'guide.xml.gz'
const compressed = await gzip(xml)
file.write(outputPath, compressed)
} else {
outputPath = outputPath || 'guide.xml'
file.write(outputPath, xml)
2022-01-31 03:48:01 +01:00
}
2021-10-05 23:36:28 +02:00
2023-05-11 03:12:49 +02:00
logger.info(`File '${outputPath}' successfully saved`)
2022-03-15 12:52:59 +01:00
}
2021-10-05 23:36:28 +02:00
2021-11-16 15:59:26 +01:00
logger.info('Finish')
2021-10-05 23:36:28 +02:00
}
main()