mirror of
https://github.com/iptv-org/epg
synced 2026-05-11 11:57:04 -04:00
74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import { Collection, Logger } from '@freearhey/core'
|
|
import { Storage } from '@freearhey/storage-js'
|
|
import { EPGGrabber } from 'epg-grabber'
|
|
import { Channel, Program } from '.'
|
|
import utc from 'dayjs/plugin/utc'
|
|
import dayjs from 'dayjs'
|
|
import path from 'node:path'
|
|
import pako from 'pako'
|
|
|
|
dayjs.extend(utc)
|
|
|
|
interface GuideData {
|
|
channels: Collection<Channel>
|
|
programs: Collection<Program>
|
|
filepath: string
|
|
gzip: boolean | string
|
|
json: boolean | string
|
|
}
|
|
|
|
export class Guide {
|
|
channels: Collection<Channel>
|
|
programs: Collection<Program>
|
|
filepath: string
|
|
gzip: boolean | string
|
|
json: boolean | string
|
|
|
|
constructor(data: GuideData) {
|
|
this.channels = data.channels
|
|
this.programs = data.programs
|
|
this.filepath = data.filepath
|
|
this.gzip = data.gzip || false
|
|
this.json = data.json || false
|
|
}
|
|
|
|
addChannel(channel: Channel) {
|
|
this.channels.add(channel)
|
|
}
|
|
|
|
toString() {
|
|
const currDate = dayjs.utc(process.env.CURR_DATE || new Date().toISOString())
|
|
|
|
return EPGGrabber.generateXMLTV(this.channels.all(), this.programs.all(), currDate)
|
|
}
|
|
|
|
async save({ logger }: { logger: Logger }) {
|
|
const dir = path.dirname(this.filepath)
|
|
const storage = new Storage(dir)
|
|
const xmlFilepath = this.filepath
|
|
const xmlFilename = path.basename(xmlFilepath)
|
|
logger.info(` saving to "${xmlFilepath}"...`)
|
|
const xmltv = this.toString()
|
|
await storage.save(xmlFilename, xmltv)
|
|
|
|
if (this.gzip) {
|
|
const compressed = pako.gzip(xmltv)
|
|
const gzFilepath = typeof this.gzip === 'string' ? this.gzip : `${this.filepath}.gz`
|
|
const gzFilename = path.basename(gzFilepath)
|
|
logger.info(` saving to "${gzFilepath}"...`)
|
|
await storage.save(gzFilename, compressed)
|
|
}
|
|
|
|
if (this.json) {
|
|
const filename = path.basename(this.filepath).split('.')[0]
|
|
const jsonFilepath =
|
|
typeof this.json === 'string' ? this.json : path.join(dir, `${filename}.json`)
|
|
const channels = this.channels.map((channel: Channel) => channel.toObject()).all()
|
|
const programs = this.programs.map((program: Program) => program.toObject()).all()
|
|
const json = JSON.stringify({ channels, programs })
|
|
logger.info(` saving to "${jsonFilepath}"...`)
|
|
await storage.save(jsonFilepath, json)
|
|
}
|
|
}
|
|
}
|