From 487c0e2e30c8a3257c39a3b5d714561c1be80e20 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Thu, 16 Jun 2022 21:58:15 +0300 Subject: [PATCH] Update scripts/commands --- scripts/commands/api/update.js | 4 ++-- scripts/commands/channels/validate.js | 10 ++++---- scripts/commands/cluster/load.js | 14 +++++++---- scripts/commands/guides/update.js | 20 +++++++--------- scripts/commands/programs/save.js | 34 +++++---------------------- scripts/commands/queue/create.js | 20 ++++++---------- 6 files changed, 37 insertions(+), 65 deletions(-) diff --git a/scripts/commands/api/update.js b/scripts/commands/api/update.js index 0b925643..e705db67 100644 --- a/scripts/commands/api/update.js +++ b/scripts/commands/api/update.js @@ -21,8 +21,8 @@ async function main() { for (const channel of channels) { guides.push({ - channel: channel.xmltv_id, - site, + channel: channel.id, + site: channel.site, lang: channel.lang, url: `https://iptv-org.github.io/epg/guides/${suffix}/${site}.epg.xml` }) diff --git a/scripts/commands/channels/validate.js b/scripts/commands/channels/validate.js index f841b598..5996369e 100644 --- a/scripts/commands/channels/validate.js +++ b/scripts/commands/channels/validate.js @@ -24,15 +24,15 @@ async function main() { const buffer = {} const errors = [] for (const channel of channels) { - if (!buffer[channel.xmltv_id]) { - buffer[channel.xmltv_id] = channel + if (!buffer[channel.id]) { + buffer[channel.id] = channel } else { - errors.push({ type: 'duplicate', ...channel }) + errors.push({ type: 'duplicate', xmltv_id: channel.id, ...channel }) stats.errors++ } - if (!api.channels.find({ id: channel.xmltv_id })) { - errors.push({ type: 'wrong_xmltv_id', ...channel }) + if (!api.channels.find({ id: channel.id })) { + errors.push({ type: 'wrong_xmltv_id', xmltv_id: channel.id, ...channel }) stats.errors++ } } diff --git a/scripts/commands/cluster/load.js b/scripts/commands/cluster/load.js index bc9f7df3..a322b4c8 100644 --- a/scripts/commands/cluster/load.js +++ b/scripts/commands/cluster/load.js @@ -1,7 +1,11 @@ const _ = require('lodash') -const EPGGrabber = require('epg-grabber') +const { EPGGrabber } = require('epg-grabber') const { program } = require('commander') const { db, logger, timer, file, parser } = require('../../core') +const dayjs = require('dayjs') +const utc = require('dayjs/plugin/utc') + +dayjs.extend(utc) const options = program .requiredOption('-c, --cluster-id ', 'The ID of cluster to load', parser.parseNumber) @@ -27,7 +31,7 @@ async function main() { await file.create(CLUSTER_PATH) await db.queue.load() let items = await db.queue.find({ cluster_id: options.clusterId }) - items = _.orderBy(items, [i => i.channel.xmltv_id.toLowerCase(), 'date']) + items = _.orderBy(items, [i => i.channel.id.toLowerCase(), 'date']) const total = items.length logger.info('Loading...') @@ -45,9 +49,9 @@ async function main() { for (const item of items) { await grabber.grab(item.channel, item.date, async (data, err) => { logger.info( - `[${i}/${total}] ${item.channel.site} (${item.channel.lang}) - ${ - item.channel.xmltv_id - } - ${data.date.format('MMM D, YYYY')} (${data.programs.length} programs)` + `[${i}/${total}] ${item.channel.site} (${item.channel.lang}) - ${item.channel.id} - ${dayjs + .utc(data.date) + .format('MMM D, YYYY')} (${data.programs.length} programs)` ) if (err) logger.error(err.message) diff --git a/scripts/commands/guides/update.js b/scripts/commands/guides/update.js index 2c8dd097..aaf509e8 100644 --- a/scripts/commands/guides/update.js +++ b/scripts/commands/guides/update.js @@ -1,5 +1,5 @@ const { db, logger, file, api, zip } = require('../../core') -const grabber = require('epg-grabber') +const { generateXMLTV, Program, Channel } = require('epg-grabber') const _ = require('lodash') const PUBLIC_DIR = process.env.PUBLIC_DIR || '.gh-pages' @@ -23,25 +23,21 @@ async function main() { const itemPrograms = await loadProgramsForItem(item) programs = programs.concat(itemPrograms) - if (channels[item.channel.xmltv_id]) continue - const channel = api.channels.find({ id: item.channel.xmltv_id }) - if (channel) { - channels[channel.id] = { - xmltv_id: channel.id, - name: item.channel.display_name, - logo: channel.logo, - site: item.channel.site - } + if (channels[item.channel.id]) continue + const found = api.channels.find({ id: item.channel.id }) + if (found) { + channels[item.channel.id] = new Channel(item.channel) } } channels = Object.values(channels) - channels = _.sortBy(channels, 'xmltv_id') + channels = _.sortBy(channels, 'id') programs = _.sortBy(programs, ['channel', 'start']) + programs = programs.map(p => new Program(p)) total += programs.length const filepath = `${PUBLIC_DIR}/guides/${key}.epg.xml` logger.info(`Creating "${filepath}"...`) - const output = grabber.convertToXMLTV({ channels, programs, date: CURR_DATE }) + const output = generateXMLTV({ channels, programs, date: CURR_DATE }) await file.create(filepath, output) const compressed = await zip.compress(output) await file.create(filepath + '.gz', compressed) diff --git a/scripts/commands/programs/save.js b/scripts/commands/programs/save.js index d53d576a..bbad2fdf 100644 --- a/scripts/commands/programs/save.js +++ b/scripts/commands/programs/save.js @@ -1,4 +1,5 @@ const { db, logger, file, parser } = require('../../core') +const { Program } = require('epg-grabber') const _ = require('lodash') const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs' @@ -15,34 +16,11 @@ async function main() { const queue = await db.queue.find({ _id: result._qid }).limit(1) if (!queue.length) continue const item = queue[0] - const programs = result.programs.map(program => { - return { - title: program.title, - description: program.description || null, - category: program.category || null, - season: program.season || null, - episode: program.episode || null, - url: program.url || null, - icon: program.icon || null, - channel: program.channel, - sub_title: program.sub_title || null, - date: program.date || null, - lang: program.lang, - start: program.start, - stop: program.stop, - director: program.director || null, - actor: program.actor || null, - writer: program.writer || null, - adapter: program.adapter || null, - producer: program.producer || null, - composer: program.composer || null, - editor: program.editor || null, - presenter: program.presenter || null, - commentator: program.commentator || null, - guest: program.guest || null, - site: item.channel.site, - _qid: result._qid - } + const programs = result.programs.map(p => { + p = new Program(p) + p._qid = result._qid + + return p }) await db.programs.insert(programs) diff --git a/scripts/commands/queue/create.js b/scripts/commands/queue/create.js index 2268edea..062135b5 100644 --- a/scripts/commands/queue/create.js +++ b/scripts/commands/queue/create.js @@ -39,7 +39,7 @@ async function createQueue() { for (const filepath of files) { try { const dir = file.dirname(filepath) - const { site, channels: items } = await parser.parseChannels(filepath) + const { site, channels } = await parser.parseChannels(filepath) if (!site) continue const configPath = `${dir}/${site}.config.js` const config = require(file.resolve(configPath)) @@ -47,22 +47,16 @@ async function createQueue() { const filename = file.basename(filepath) const [__, region] = filename.match(/_([a-z-]+)\.channels\.xml/i) || [null, null] const groupId = `${region}/${site}` - for (const item of items) { - if (!item.site || !item.xmltv_id) continue - const channel = api.channels.find({ id: item.xmltv_id }) - if (!channel) continue + for (const channel of channels) { + if (!channel.site || !channel.id) continue + const found = api.channels.find({ id: channel.id }) + if (!found) continue for (const d of dates) { const dString = d.toJSON() - const key = `${item.site}:${item.lang}:${item.xmltv_id}:${dString}` + const key = `${channel.site}:${channel.lang}:${channel.id}:${dString}` if (!queue[key]) { queue[key] = { - channel: { - lang: item.lang, - xmltv_id: item.xmltv_id, - display_name: item.name, - site_id: item.site_id, - site: item.site - }, + channel, date: dString, configPath, groups: [],