Update scripts

This commit is contained in:
freearhey
2026-05-03 21:37:49 +03:00
parent 56ccf2ac1b
commit 56a228e9cb
5 changed files with 84 additions and 18 deletions

View File

@@ -13,20 +13,23 @@ interface GuideData {
channels: Collection<Channel>
programs: Collection<Program>
filepath: string
gzip: boolean
gzip: boolean | string
json: boolean | string
}
export class Guide {
channels: Collection<Channel>
programs: Collection<Program>
filepath: string
gzip: boolean
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) {
@@ -50,10 +53,21 @@ export class Guide {
if (this.gzip) {
const compressed = pako.gzip(xmltv)
const gzFilepath = `${this.filepath}.gz`
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)
}
}
}

View File

@@ -14,7 +14,9 @@ export interface WorkerData {
export class Worker {
host: string
channelsPath?: string
guidePath?: string
guideXmlPath?: string
guideGzipPath?: string
guideJsonPath?: string
channels?: Collection<Channel>
status?: string
lastUpdated?: string
@@ -41,10 +43,26 @@ export class Worker {
return url.href
}
getGuideUrl(): string {
if (!this.guidePath) return ''
getGuideXmlUrl(): string {
if (!this.guideXmlPath) return ''
const url = new URL(this.guidePath, this.getBaseUrl())
const url = new URL(this.guideXmlPath, this.getBaseUrl())
return url.href
}
getGuideGzipUrl(): string {
if (!this.guideGzipPath) return ''
const url = new URL(this.guideGzipPath, this.getBaseUrl())
return url.href
}
getGuideJsonUrl(): string {
if (!this.guideJsonPath) return ''
const url = new URL(this.guideJsonPath, this.getBaseUrl())
return url.href
}
@@ -70,4 +88,25 @@ export class Worker {
return dayjs.utc(this.lastUpdated).from(now)
}
getLinks(): { url: string; label: string }[] {
const links = []
if (this.guideXmlPath) {
const url = new URL(this.guideXmlPath, this.getBaseUrl())
links.push({ url: url.href, label: 'XML' })
}
if (this.guideGzipPath) {
const url = new URL(this.guideGzipPath, this.getBaseUrl())
links.push({ url: url.href, label: 'GZIP' })
}
if (this.guideJsonPath) {
const url = new URL(this.guideJsonPath, this.getBaseUrl())
links.push({ url: url.href, label: 'JSON' })
}
return links
}
}