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

@@ -54,7 +54,10 @@ program
.argParser(parseNumber) .argParser(parseNumber)
.env('MAX_CONNECTIONS') .env('MAX_CONNECTIONS')
) )
.addOption(new Option('--gzip', 'Create a compressed version of the guide as well').env('GZIP')) .addOption(
new Option('--gzip [path]', 'Create a compressed version of the guide as well').env('GZIP')
)
.addOption(new Option('--json [path]', 'Create a JSON version of the guide as well').env('JSON'))
.addOption(new Option('--curl', 'Display each request as CURL').env('CURL')) .addOption(new Option('--curl', 'Display each request as CURL').env('CURL'))
.addOption(new Option('--debug', 'Enable debug mode').env('DEBUG')) .addOption(new Option('--debug', 'Enable debug mode').env('DEBUG'))
.parse() .parse()
@@ -63,7 +66,8 @@ interface GrabOptions {
sites?: string[] sites?: string[]
channels?: string channels?: string
output?: string output?: string
gzip?: boolean gzip?: boolean | string
json?: boolean | string
curl?: boolean curl?: boolean
debug?: boolean debug?: boolean
maxConnections?: number maxConnections?: number
@@ -109,7 +113,10 @@ async function main() {
if (typeof options.maxConnections === 'number') if (typeof options.maxConnections === 'number')
globalConfig.maxConnections = options.maxConnections globalConfig.maxConnections = options.maxConnections
if (typeof options.curl === 'boolean') globalConfig.curl = options.curl if (typeof options.curl === 'boolean') globalConfig.curl = options.curl
if (typeof options.gzip === 'boolean') globalConfig.gzip = options.gzip if (typeof options.gzip === 'boolean' || typeof options.gzip === 'string')
globalConfig.gzip = options.gzip
if (typeof options.json === 'boolean' || typeof options.json === 'string')
globalConfig.json = options.json
if (typeof options.debug === 'boolean') globalConfig.debug = options.debug if (typeof options.debug === 'boolean') globalConfig.debug = options.debug
logger.debug(`config: ${JSON.stringify(globalConfig, null, 2)}`) logger.debug(`config: ${JSON.stringify(globalConfig, null, 2)}`)
@@ -298,6 +305,7 @@ async function main() {
}) })
const gzip = globalConfig.gzip || defaultConfig.gzip const gzip = globalConfig.gzip || defaultConfig.gzip
const json = globalConfig.json || defaultConfig.json
for (const groupKey of channelsGroupedByKey.keys()) { for (const groupKey of channelsGroupedByKey.keys()) {
const groupChannels = new Collection(channelsGroupedByKey.get(groupKey)) const groupChannels = new Collection(channelsGroupedByKey.get(groupKey))
@@ -305,6 +313,7 @@ async function main() {
const guide = new Guide({ const guide = new Guide({
filepath: groupKey, filepath: groupKey,
gzip, gzip,
json,
channels: groupChannels, channels: groupChannels,
programs: groupPrograms programs: groupPrograms
}) })

View File

@@ -61,7 +61,10 @@ async function main() {
} }
worker.channelsPath = workerJson.channels worker.channelsPath = workerJson.channels
worker.guidePath = workerJson.guide worker.guideXmlPath =
typeof workerJson.guide === 'string' ? workerJson.guide : workerJson?.guide?.xml
worker.guideGzipPath = workerJson?.guide?.gzip
worker.guideJsonPath = workerJson?.guide?.json
if (!worker.channelsPath) { if (!worker.channelsPath) {
worker.status = 'MISSING_CHANNELS_PATH' worker.status = 'MISSING_CHANNELS_PATH'
@@ -69,8 +72,8 @@ async function main() {
continue continue
} }
if (!worker.guidePath) { if (!worker.guideXmlPath) {
worker.status = 'MISSING_GUIDE_PATH' worker.status = 'MISSING_GUIDE_XML_PATH'
logger.error('The "guide" property is missing from the workers config') logger.error('The "guide" property is missing from the workers config')
continue continue
} }
@@ -91,7 +94,7 @@ async function main() {
) )
const guideXml = await client const guideXml = await client
.get(worker.guidePath) .get(worker.guideXmlPath)
.then(res => res.data) .then(res => res.data)
.catch(err => { .catch(err => {
worker.status = err.status worker.status = err.status
@@ -109,6 +112,7 @@ async function main() {
logger.info('creating guides table...') logger.info('creating guides table...')
const rows = new Collection<HTMLTableRow>() const rows = new Collection<HTMLTableRow>()
workers.forEach((worker: Worker) => { workers.forEach((worker: Worker) => {
const links = worker.getLinks()
rows.add( rows.add(
new Collection<HTMLTableDataItem>([ new Collection<HTMLTableDataItem>([
{ value: worker.host }, { value: worker.host },
@@ -116,10 +120,9 @@ async function main() {
{ value: worker.getChannelsCount().toString(), align: 'right' }, { value: worker.getChannelsCount().toString(), align: 'right' },
{ value: worker.getLastUpdated(), align: 'left' }, { value: worker.getLastUpdated(), align: 'left' },
{ {
value: value: links.length
worker.status === 'OK' ? links.map(link => `<a href="${link.url}">${link.label}</a>`).join(' | ')
? `<a href="${worker.getChannelsUrl()}">${worker.channelsPath}</a><br><a href="${worker.getGuideUrl()}">${worker.guidePath}</a>` : '-'
: ''
} }
]) ])
) )

View File

@@ -5,6 +5,7 @@ export default {
maxConnections: 1, maxConnections: 1,
curl: false, curl: false,
gzip: false, gzip: false,
json: false,
debug: false, debug: false,
request: { request: {
maxContentLength: 5242880, maxContentLength: 5242880,

View File

@@ -13,20 +13,23 @@ interface GuideData {
channels: Collection<Channel> channels: Collection<Channel>
programs: Collection<Program> programs: Collection<Program>
filepath: string filepath: string
gzip: boolean gzip: boolean | string
json: boolean | string
} }
export class Guide { export class Guide {
channels: Collection<Channel> channels: Collection<Channel>
programs: Collection<Program> programs: Collection<Program>
filepath: string filepath: string
gzip: boolean gzip: boolean | string
json: boolean | string
constructor(data: GuideData) { constructor(data: GuideData) {
this.channels = data.channels this.channels = data.channels
this.programs = data.programs this.programs = data.programs
this.filepath = data.filepath this.filepath = data.filepath
this.gzip = data.gzip || false this.gzip = data.gzip || false
this.json = data.json || false
} }
addChannel(channel: Channel) { addChannel(channel: Channel) {
@@ -50,10 +53,21 @@ export class Guide {
if (this.gzip) { if (this.gzip) {
const compressed = pako.gzip(xmltv) 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) const gzFilename = path.basename(gzFilepath)
logger.info(` saving to "${gzFilepath}"...`) logger.info(` saving to "${gzFilepath}"...`)
await storage.save(gzFilename, compressed) 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 { export class Worker {
host: string host: string
channelsPath?: string channelsPath?: string
guidePath?: string guideXmlPath?: string
guideGzipPath?: string
guideJsonPath?: string
channels?: Collection<Channel> channels?: Collection<Channel>
status?: string status?: string
lastUpdated?: string lastUpdated?: string
@@ -41,10 +43,26 @@ export class Worker {
return url.href return url.href
} }
getGuideUrl(): string { getGuideXmlUrl(): string {
if (!this.guidePath) return '' 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 return url.href
} }
@@ -70,4 +88,25 @@ export class Worker {
return dayjs.utc(this.lastUpdated).from(now) 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
}
} }