Files
epg/scripts/models/worker.ts

74 lines
1.5 KiB
TypeScript
Raw Normal View History

2026-02-13 12:52:15 +03:00
import relativeTime from 'dayjs/plugin/relativeTime'
import { Collection } from '@freearhey/core'
import { Channel } from './channel'
2026-02-15 08:23:23 +03:00
import utc from 'dayjs/plugin/utc'
2026-02-13 12:52:15 +03:00
import dayjs from 'dayjs'
dayjs.extend(relativeTime)
2026-02-15 08:23:23 +03:00
dayjs.extend(utc)
2026-02-13 12:52:15 +03:00
export interface WorkerData {
host: string
}
export class Worker {
host: string
channelsPath?: string
guidePath?: string
channels?: Collection<Channel>
status?: string
lastUpdated?: string
constructor(data: WorkerData) {
this.host = data.host
}
getBaseUrl(): string {
return `https://${this.host}`
}
getConfigUrl(): string {
const url = new URL('worker.json', this.getBaseUrl())
return url.href
}
getChannelsUrl(): string {
if (!this.channelsPath) return ''
const url = new URL(this.channelsPath, this.getBaseUrl())
return url.href
}
getGuideUrl(): string {
if (!this.guidePath) return ''
const url = new URL(this.guidePath, this.getBaseUrl())
return url.href
}
getStatusEmoji(): string {
if (!this.status) return '⚪'
if (this.status === 'OK') return '🟢'
return '🔴'
}
getChannelsCount(): number {
if (!this.channels) return 0
return this.channels.count()
}
getLastUpdated(): string {
if (!this.lastUpdated) return '-'
2026-02-15 08:23:23 +03:00
let now = dayjs()
if (process.env.NODE_ENV === 'test') now = dayjs.utc('2026-02-13')
return dayjs.utc(this.lastUpdated).from(now)
2026-02-13 12:52:15 +03:00
}
}