From 11856161c2399ac503175fd1f8d8661bed060f76 Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Sun, 15 Oct 2023 09:48:30 +0300 Subject: [PATCH] Create QueueCreator --- scripts/commands/epg/grab.ts | 6 +-- scripts/core/index.ts | 1 + scripts/core/queue.ts | 91 +++++++++--------------------------- scripts/core/queueCreator.ts | 71 ++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 73 deletions(-) create mode 100644 scripts/core/queueCreator.ts diff --git a/scripts/commands/epg/grab.ts b/scripts/commands/epg/grab.ts index 38102d99..9741c8d7 100644 --- a/scripts/commands/epg/grab.ts +++ b/scripts/commands/epg/grab.ts @@ -1,7 +1,7 @@ import { Logger, Timer, Storage, Collection } from '@freearhey/core' import { program } from 'commander' import { CronJob } from 'cron' -import { Queue, Job, ChannelsParser } from '../../core' +import { QueueCreator, Job, ChannelsParser } from '../../core' import { Channel } from 'epg-grabber' import path from 'path' import { SITES_DIR } from '../../constants' @@ -78,12 +78,12 @@ async function main() { logger.info(` found ${parsedChannels.count()} channels`) logger.info('creating queue...') - const queue = new Queue({ + const queueCreator = new QueueCreator({ parsedChannels, logger, options }) - await queue.create() + const queue = await queueCreator.create() logger.info(` added ${queue.size()} items`) const job = new Job({ diff --git a/scripts/core/index.ts b/scripts/core/index.ts index 6ab0d340..0af8e636 100644 --- a/scripts/core/index.ts +++ b/scripts/core/index.ts @@ -9,3 +9,4 @@ export * from './guideManager' export * from './guide' export * from './apiChannel' export * from './apiClient' +export * from './queueCreator' diff --git a/scripts/core/queue.ts b/scripts/core/queue.ts index c197da65..28af9d88 100644 --- a/scripts/core/queue.ts +++ b/scripts/core/queue.ts @@ -1,9 +1,5 @@ -import { Storage, Collection, DateTime, Logger, Dictionary } from '@freearhey/core' -import { ChannelsParser, ConfigLoader, ApiChannel } from './' -import { SITES_DIR, DATA_DIR, CURR_DATE } from '../constants' -import { Channel, SiteConfig } from 'epg-grabber' -import path from 'path' -import { GrabOptions } from '../commands/epg/grab' +import { Dictionary } from '@freearhey/core' +import { SiteConfig, Channel } from 'epg-grabber' export type QueueItem = { channel: Channel @@ -12,83 +8,38 @@ export type QueueItem = { error: string | null } -type QueueProps = { - logger: Logger - options: GrabOptions - parsedChannels: Collection -} - export class Queue { - configLoader: ConfigLoader - logger: Logger - sitesStorage: Storage - dataStorage: Storage - parser: ChannelsParser - parsedChannels: Collection - options: GrabOptions - date: DateTime - _items: QueueItem[] = [] + _data: Dictionary - constructor({ parsedChannels, logger, options }: QueueProps) { - this.parsedChannels = parsedChannels - this.logger = logger - this.sitesStorage = new Storage() - this.dataStorage = new Storage(DATA_DIR) - this.parser = new ChannelsParser({ storage: new Storage() }) - this.date = new DateTime(CURR_DATE) - this.options = options - this.configLoader = new ConfigLoader() + constructor() { + this._data = new Dictionary() } - async create() { - const channelsContent = await this.dataStorage.json('channels.json') - const channels = new Collection(channelsContent).map(data => new ApiChannel(data)) + missing(key: string): boolean { + return this._data.missing(key) + } - const queue = new Dictionary() - - for (const channel of this.parsedChannels.all()) { - if (!channel.site || !channel.xmltv_id) continue - if (this.options.lang && channel.lang !== this.options.lang) continue - - const configPath = path.resolve(SITES_DIR, `${channel.site}/${channel.site}.config.js`) - const config: SiteConfig = await this.configLoader.load(configPath) - - const found: ApiChannel = channels.first( - (_channel: ApiChannel) => _channel.id === channel.xmltv_id - ) - if (found) { - channel.logo = found.logo - } - - const days = this.options.days || config.days || 1 - const dates = Array.from({ length: days }, (_, day) => this.date.add(day, 'd')) - dates.forEach((date: DateTime) => { - const dateString = date.toJSON() - const key = `${channel.site}:${channel.lang}:${channel.xmltv_id}:${dateString}` - - if (queue.missing(key)) { - queue.set(key, { - channel, - date: dateString, - config, - error: null - }) - } - }) - } - - this._items = Object.values(queue.data()) + add( + key: string, + { channel, config, date }: { channel: Channel; date: string | null; config: SiteConfig } + ) { + this._data.set(key, { + channel, + date, + config, + error: null + }) } size(): number { - return this._items.length + return Object.values(this._data.data()).length } items(): QueueItem[] { - return this._items + return Object.values(this._data.data()) as QueueItem[] } isEmpty(): boolean { - return this._items.length === 0 + return this.size() === 0 } } diff --git a/scripts/core/queueCreator.ts b/scripts/core/queueCreator.ts new file mode 100644 index 00000000..b23a8f4a --- /dev/null +++ b/scripts/core/queueCreator.ts @@ -0,0 +1,71 @@ +import { Storage, Collection, DateTime, Logger } from '@freearhey/core' +import { ChannelsParser, ConfigLoader, ApiChannel, Queue } from './' +import { SITES_DIR, DATA_DIR, CURR_DATE } from '../constants' +import { SiteConfig } from 'epg-grabber' +import path from 'path' +import { GrabOptions } from '../commands/epg/grab' + +type QueueCreatorProps = { + logger: Logger + options: GrabOptions + parsedChannels: Collection +} + +export class QueueCreator { + configLoader: ConfigLoader + logger: Logger + sitesStorage: Storage + dataStorage: Storage + parser: ChannelsParser + parsedChannels: Collection + options: GrabOptions + date: DateTime + + constructor({ parsedChannels, logger, options }: QueueCreatorProps) { + this.parsedChannels = parsedChannels + this.logger = logger + this.sitesStorage = new Storage() + this.dataStorage = new Storage(DATA_DIR) + this.parser = new ChannelsParser({ storage: new Storage() }) + this.date = new DateTime(CURR_DATE) + this.options = options + this.configLoader = new ConfigLoader() + } + + async create(): Promise { + const channelsContent = await this.dataStorage.json('channels.json') + const channels = new Collection(channelsContent).map(data => new ApiChannel(data)) + + const queue = new Queue() + for (const channel of this.parsedChannels.all()) { + if (!channel.site || !channel.xmltv_id) continue + if (this.options.lang && channel.lang !== this.options.lang) continue + + const configPath = path.resolve(SITES_DIR, `${channel.site}/${channel.site}.config.js`) + const config: SiteConfig = await this.configLoader.load(configPath) + + const found: ApiChannel = channels.first( + (_channel: ApiChannel) => _channel.id === channel.xmltv_id + ) + if (found) { + channel.logo = found.logo + } + + const days = this.options.days || config.days || 1 + const dates = Array.from({ length: days }, (_, day) => this.date.add(day, 'd')) + dates.forEach((date: DateTime) => { + const dateString = date.toJSON() + const key = `${channel.site}:${channel.lang}:${channel.xmltv_id}:${dateString}` + if (queue.missing(key)) { + queue.add(key, { + channel, + date: dateString, + config + }) + } + }) + } + + return queue + } +}