Files
epg/scripts/core/queueCreator.ts

64 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-10-15 14:08:23 +03:00
import { Storage, Collection, DateTime, Logger } from '@freearhey/core'
import { SITES_DIR, DATA_DIR } from '../constants'
2025-07-18 22:51:01 +03:00
import { GrabOptions } from '../commands/epg/grab'
import { ConfigLoader, Queue } from './'
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'
type QueueCreatorProps = {
logger: Logger
options: GrabOptions
2025-07-18 22:51:01 +03:00
channels: Collection
2023-10-15 14:08:23 +03:00
}
export class QueueCreator {
configLoader: ConfigLoader
logger: Logger
sitesStorage: Storage
dataStorage: Storage
2025-07-18 22:51:01 +03:00
channels: Collection
2023-10-15 14:08:23 +03:00
options: GrabOptions
2025-07-18 22:51:01 +03:00
constructor({ channels, logger, options }: QueueCreatorProps) {
this.channels = channels
2023-10-15 14:08:23 +03:00
this.logger = logger
this.sitesStorage = new Storage()
this.dataStorage = new Storage(DATA_DIR)
this.options = options
this.configLoader = new ConfigLoader()
}
async create(): Promise<Queue> {
2025-07-17 17:43:00 +03:00
let index = 0
2023-10-15 14:08:23 +03:00
const queue = new Queue()
2025-07-18 22:51:01 +03:00
for (const channel of this.channels.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)
2025-07-18 22:51:01 +03:00
if (!channel.xmltv_id) {
2023-12-01 20:11:28 +03:00
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
}
}