Update index.js

This commit is contained in:
Aleksandr Statciuk 2021-08-21 19:10:58 +03:00
parent 45c531eac6
commit bd0b29aaee
1 changed files with 34 additions and 30 deletions

View File

@ -13,11 +13,12 @@ program
.option('-d, --debug', 'Enable debug mode') .option('-d, --debug', 'Enable debug mode')
.parse(process.argv) .parse(process.argv)
const options = program.opts()
const config = utils.loadConfig(options.config)
async function main() { async function main() {
console.log('\r\nStarting...') console.log('\r\nStarting...')
const options = program.opts()
const config = utils.loadConfig(options.config)
const channels = utils.parseChannels(config.channels) const channels = utils.parseChannels(config.channels)
const utcDate = utils.getUTCDate() const utcDate = utils.getUTCDate()
const dates = Array.from({ length: config.days }, (_, i) => utcDate.add(i, 'd')) const dates = Array.from({ length: config.days }, (_, i) => utcDate.add(i, 'd'))
@ -34,35 +35,12 @@ async function main() {
for (let item of queue) { for (let item of queue) {
if (options.debug) console.time(' Response Time') if (options.debug) console.time(' Response Time')
await utils await utils
.fetchData(item, config) .buildRequest(item, config)
.then(utils.fetchData)
.then(response => { .then(response => {
if (options.debug) { if (options.debug) console.timeEnd(' Response Time')
console.timeEnd(' Response Time') const results = parseResponse(response, item)
console.time(' Parsing Time') programs = programs.concat(results)
}
if (!item.channel.logo && config.logo) {
item.channel.logo = config.logo({
channel: item.channel,
content: response.data.toString(),
buffer: response.data
})
}
const parsed = utils.parsePrograms({ response, item, config }).map(program => {
program.lang = program.lang || item.channel.lang || undefined
return program
})
console.log(
` ${config.site} - ${item.channel.xmltv_id} - ${item.date.format('MMM D, YYYY')} (${
parsed.length
} programs)`
)
programs = programs.concat(parsed)
})
.then(() => {
if (options.debug) console.timeEnd(' Parsing Time')
}) })
.then(utils.sleep(config.delay)) .then(utils.sleep(config.delay))
.catch(err => { .catch(err => {
@ -86,4 +64,30 @@ async function main() {
console.log('Finish') console.log('Finish')
} }
async function parseResponse(response, item) {
if (options.debug) console.time(' Parsing Time')
if (!item.channel.logo && config.logo) {
item.channel.logo = config.logo({
channel: item.channel,
content: response.data.toString(),
buffer: response.data
})
}
const parsed = utils.parsePrograms({ response, item, config }).map(program => {
program.lang = program.lang || item.channel.lang || undefined
return program
})
console.log(
` ${config.site} - ${item.channel.xmltv_id} - ${item.date.format('MMM D, YYYY')} (${
parsed.length
} programs)`
)
if (options.debug) console.timeEnd(' Parsing Time')
return parsed
}
main() main()