Files
epg/scripts/core/queueCreator.ts

80 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-10-15 14:08:23 +03:00
import { Storage, Collection, DateTime, Logger } from '@freearhey/core'
2025-04-02 07:13:39 +03:00
import { ChannelsParser, ConfigLoader, Queue } from './'
import { SITES_DIR, DATA_DIR } from '../constants'
2025-01-01 10:18:30 +03:00
import { SiteConfig } from 'epg-grabber'
2023-10-15 14:08:23 +03:00
import path from 'path'
import { GrabOptions } from '../commands/epg/grab'
2025-04-02 07:13:39 +03:00
import { Channel } from '../models'
2023-10-15 14:08:23 +03:00
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
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.options = options
this.configLoader = new ConfigLoader()
}
async create(): Promise<Queue> {
const channelsContent = await this.dataStorage.json('channels.json')
2025-04-02 07:13:39 +03:00
const channels = new Collection(channelsContent).map(data => new Channel(data))
2023-10-15 14:08:23 +03:00
2025-07-17 17:43:00 +03:00
let index = 0
2023-10-15 14:08:23 +03:00
const queue = new Queue()
for (const channel of this.parsedChannels.all()) {
2025-07-17 17:43:00 +03:00
channel.index = index++
2023-12-01 20:11:28 +03:00
if (!channel.site || !channel.site_id || !channel.name) continue
2023-10-15 14:08:23 +03:00
const configPath = path.resolve(SITES_DIR, `${channel.site}/${channel.site}.config.js`)
const config: SiteConfig = await this.configLoader.load(configPath)
2023-12-01 20:11:28 +03:00
if (channel.xmltv_id) {
2025-01-22 21:26:06 +03:00
if (!channel.icon) {
2025-04-02 07:13:39 +03:00
const found: Channel = channels.first(
(_channel: Channel) => _channel.id === channel.xmltv_id
2025-01-22 21:26:06 +03:00
)
if (found) {
channel.icon = found.logo
}
2023-12-01 20:11:28 +03:00
}
} else {
channel.xmltv_id = channel.site_id
2023-10-15 14:08:23 +03:00
}
const days = this.options.days || config.days || 1
const currDate = new DateTime(process.env.CURR_DATE || new Date().toISOString())
const dates = Array.from({ length: days }, (_, day) => currDate.add(day, 'd'))
2023-10-15 14:08:23 +03:00
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
}
}