Create commands/load-cluster.js

This commit is contained in:
Aleksandr Statciuk 2022-01-06 16:01:23 +03:00
parent f5dbc9376e
commit a1f0bc2d2a
10 changed files with 29683 additions and 29503 deletions

View file

@ -32,9 +32,11 @@ async function loadChannels() {
const files = await file.list(options.channels)
for (const filepath of files) {
const dir = file.dirname(filepath)
const items = await parser.parseChannels(filepath)
for (const item of items) {
item.filepath = filepath
item.channelsPath = filepath
item.configPath = `${dir}/${item.site}.config.js`
channels.push(item)
}
}

View file

@ -0,0 +1,46 @@
const grabber = require('epg-grabber')
const { program } = require('commander')
const { db, logger, timer, file, parser } = require('../core')
const options = program
.requiredOption('-c, --cluster-id <cluster-id>', 'The ID of cluster to load', parser.parseNumber)
.parse(process.argv)
.opts()
const LOGS_PATH = process.env.LOGS_PATH || 'scripts/logs'
async function main() {
logger.info('Starting...')
timer.start()
const clusterLog = `${LOGS_PATH}/load-cluster/cluster_${options.clusterId}.log`
logger.info(`Loading cluster: ${options.clusterId}`)
logger.info(`Creating '${clusterLog}'...`)
await file.create(clusterLog)
const items = await db.find({ cluster_id: options.clusterId })
const total = items.length
logger.info(`Found ${total} links`)
logger.info('Loading...')
const results = {}
let i = 1
for (const item of items) {
const config = require(file.resolve(item.configPath))
const programs = await grabber.grab(item, config, (data, err) => {
logger.info(
`[${i}/${total}] ${config.site} - ${data.channel.xmltv_id} - ${data.date.format(
'MMM D, YYYY'
)} (${data.programs.length} programs)`
)
if (err) logger.error(err.message)
if (i < total) i++
})
await file.append(clusterLog, JSON.stringify({ [item._id]: programs }) + '\n')
}
logger.info(`Done in ${timer.format('HH[h] mm[m] ss[s]')}`)
}
main()