Update scripts

This commit is contained in:
freearhey
2026-05-04 18:01:51 +03:00
parent 3f6b8267cc
commit e8e47b511a
8 changed files with 353 additions and 178 deletions

View File

@@ -0,0 +1,49 @@
import { WorkerGuideSource } from './worker'
export interface ApiGuideData {
channel: string | null
feed: string | null
site: string
site_id: string
site_name: string
lang: string | null
sources?: WorkerGuideSource[]
}
export class ApiGuide {
channel: string | null
feed: string | null
site: string
site_id: string
site_name: string
lang: string
sources: WorkerGuideSource[] = []
constructor(data: ApiGuideData) {
this.channel = data.channel || null
this.feed = data.feed || null
this.site = data.site
this.site_id = data.site_id
this.site_name = data.site_name
this.lang = data.lang || 'en'
this.sources = data.sources || []
}
addSource(source: ApiGuideSource): this {
this.sources.push(source)
return this
}
toJSON(): ApiGuideData {
return {
channel: this.channel,
feed: this.feed,
site: this.site,
site_id: this.site_id,
site_name: this.site_name,
lang: this.lang,
sources: this.sources
}
}
}

View File

@@ -1,22 +1,8 @@
import { ChannelGuideObject } from '../types/channel'
import * as epgGrabber from 'epg-grabber'
import { SITES_DIR } from '../constants'
import path from 'node:path'
export class Channel extends epgGrabber.Channel {
getGuideObject(): ChannelGuideObject {
const [channelId, feedId] = this.xmltv_id.split('@')
return {
channel: channelId || null,
feed: feedId || null,
site: this.site,
site_id: this.site_id,
site_name: this.name,
lang: this.lang || 'en'
}
}
getConfigPath(): string {
return path.resolve(SITES_DIR, `${this.site}/${this.site}.config.js`)
}

View File

@@ -1,5 +1,6 @@
import relativeTime from 'dayjs/plugin/relativeTime'
import { Collection } from '@freearhey/core'
import * as epgGrabber from 'epg-grabber'
import { Channel } from './channel'
import utc from 'dayjs/plugin/utc'
import dayjs from 'dayjs'
@@ -7,22 +8,79 @@ import dayjs from 'dayjs'
dayjs.extend(relativeTime)
dayjs.extend(utc)
export interface WorkerGuideSource {
host: string
format: string
url: string
}
export interface WorkerData {
host: string
channelsPath?: string
channels?: epgGrabber.Types.ChannelData[]
guideXmlPath?: string
guideGzipPath?: string
guideJsonPath?: string
status?: string
lastUpdated?: string
}
export class Worker {
host: string
channelsPath?: string
channels?: Collection<Channel>
guideXmlPath?: string
guideGzipPath?: string
guideJsonPath?: string
channels?: Collection<Channel>
status?: string
lastUpdated?: string
constructor(data: WorkerData) {
this.host = data.host
this.update(data)
}
update(data: Partial<WorkerData>): this {
if (data.host) this.host = data.host
if (data.channelsPath) this.channelsPath = data.channelsPath
if (data.guideXmlPath) this.guideXmlPath = data.guideXmlPath
if (data.guideGzipPath) this.guideGzipPath = data.guideGzipPath
if (data.guideJsonPath) this.guideJsonPath = data.guideJsonPath
if (data.status) this.status = data.status
if (data.lastUpdated) this.lastUpdated = data.lastUpdated
if (data.channels) {
const channelInstances = data.channels.map(c => new Channel(c))
this.channels = new Collection(channelInstances)
}
return this
}
setChannelsPath(path: string): this {
this.channelsPath = path
return this
}
setGuideXmlPath(path: string): this {
this.guideXmlPath = path
return this
}
setGuideGzipPath(path: string): this {
this.guideGzipPath = path
return this
}
setGuideJsonPath(path: string): this {
this.guideJsonPath = path
return this
}
setStatus(status: string): this {
this.status = status
return this
}
getBaseUrl(): string {
@@ -83,30 +141,42 @@ export class Worker {
getLastUpdated(): string {
if (!this.lastUpdated) return '-'
let now = dayjs()
if (process.env.NODE_ENV === 'test') now = dayjs.utc('2026-02-13')
const now = dayjs.utc(process.env.CURR_DATE || new Date().toISOString())
return dayjs.utc(this.lastUpdated).from(now)
}
getLinks(): { url: string; label: string }[] {
const links = []
getGuideSources(): WorkerGuideSource[] {
const sources = []
if (this.guideXmlPath) {
const url = new URL(this.guideXmlPath, this.getBaseUrl())
links.push({ url: url.href, label: 'XML' })
sources.push({ host: this.host, url: url.href, format: 'XML' })
}
if (this.guideGzipPath) {
const url = new URL(this.guideGzipPath, this.getBaseUrl())
links.push({ url: url.href, label: 'GZIP' })
sources.push({ host: this.host, url: url.href, format: 'GZIP' })
}
if (this.guideJsonPath) {
const url = new URL(this.guideJsonPath, this.getBaseUrl())
links.push({ url: url.href, label: 'JSON' })
sources.push({ host: this.host, url: url.href, format: 'JSON' })
}
return links
return sources
}
toJSON(): WorkerData {
return {
host: this.host,
channelsPath: this.channelsPath,
channels: this.channels ? this.channels.map(c => c.toObject()).all() : [],
guideXmlPath: this.guideXmlPath,
guideGzipPath: this.guideGzipPath,
guideJsonPath: this.guideJsonPath,
status: this.status,
lastUpdated: this.lastUpdated
}
}
}