From 27871867000d9eda6cdb647ab011db9749d8b107 Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Thu, 17 Jul 2025 17:43:00 +0300 Subject: [PATCH] Update scripts --- scripts/commands/channels/lint.mts | 1 + scripts/commands/channels/validate.ts | 24 ++++++++++++++++++------ scripts/commands/epg/grab.ts | 2 ++ scripts/core/grabber.ts | 4 ++++ scripts/core/guideManager.ts | 2 +- scripts/core/proxyParser.ts | 20 ++++++++++++-------- scripts/core/queueCreator.ts | 2 ++ scripts/models/feed.ts | 4 ++++ 8 files changed, 44 insertions(+), 15 deletions(-) diff --git a/scripts/commands/channels/lint.mts b/scripts/commands/channels/lint.mts index 8d4bb161..72cb003c 100644 --- a/scripts/commands/channels/lint.mts +++ b/scripts/commands/channels/lint.mts @@ -19,6 +19,7 @@ const xsd = ` + diff --git a/scripts/commands/channels/validate.ts b/scripts/commands/channels/validate.ts index 27c5fabb..2ad51148 100644 --- a/scripts/commands/channels/validate.ts +++ b/scripts/commands/channels/validate.ts @@ -1,6 +1,6 @@ import { Storage, Collection, Dictionary, File } from '@freearhey/core' import { ChannelsParser } from '../../core' -import { Channel } from '../../models' +import { Channel, Feed } from '../../models' import { program } from 'commander' import chalk from 'chalk' import langs from 'langs' @@ -10,7 +10,7 @@ import epgGrabber from 'epg-grabber' program.argument('[filepath]', 'Path to *.channels.xml files to validate').parse(process.argv) type ValidationError = { - type: 'duplicate' | 'wrong_xmltv_id' | 'wrong_lang' + type: 'duplicate' | 'wrong_channel_id' | 'wrong_feed_id' | 'wrong_lang' name: string lang?: string xmltv_id?: string @@ -24,7 +24,10 @@ async function main() { const dataStorage = new Storage(DATA_DIR) const channelsData = await dataStorage.json('channels.json') const channels = new Collection(channelsData).map(data => new Channel(data)) - const channelsGroupedById = channels.groupBy((channel: Channel) => channel.id) + const channelsKeyById = channels.keyBy((channel: Channel) => channel.id) + const feedsData = await dataStorage.json('feeds.json') + const feeds = new Collection(feedsData).map(data => new Feed(data)) + const feedsKeyByStreamId = feeds.keyBy((feed: Feed) => feed.getStreamId()) let totalFiles = 0 let totalErrors = 0 @@ -54,12 +57,21 @@ async function main() { } if (!channel.xmltv_id) return - const [channelId] = channel.xmltv_id.split('@') - const foundChannel = channelsGroupedById.get(channelId) + const [channelId, feedId] = channel.xmltv_id.split('@') + + const foundChannel = channelsKeyById.get(channelId) if (!foundChannel) { - errors.push({ type: 'wrong_xmltv_id', ...channel }) + errors.push({ type: 'wrong_channel_id', ...channel }) totalErrors++ } + + if (feedId) { + const foundFeed = feedsKeyByStreamId.get(channel.xmltv_id) + if (!foundFeed) { + errors.push({ type: 'wrong_feed_id', ...channel }) + totalErrors++ + } + } }) if (errors.length) { diff --git a/scripts/commands/epg/grab.ts b/scripts/commands/epg/grab.ts index 7d4e0bb9..9e36a2c2 100644 --- a/scripts/commands/epg/grab.ts +++ b/scripts/commands/epg/grab.ts @@ -44,6 +44,7 @@ program .default(false) .env('GZIP') ) + .addOption(new Option('--curl', 'Display each request as CURL').default(false).env('CURL')) .parse() export type GrabOptions = { @@ -51,6 +52,7 @@ export type GrabOptions = { channels?: string output: string gzip: boolean + curl: boolean maxConnections: number timeout?: string delay?: string diff --git a/scripts/core/grabber.ts b/scripts/core/grabber.ts index 2248cbc5..57bd322d 100644 --- a/scripts/core/grabber.ts +++ b/scripts/core/grabber.ts @@ -70,6 +70,10 @@ export class Grabber { } } + if (this.options.curl === true) { + config.curl = true + } + const _programs = await this.grabber.grab( channel, date, diff --git a/scripts/core/guideManager.ts b/scripts/core/guideManager.ts index 78640a32..b7244662 100644 --- a/scripts/core/guideManager.ts +++ b/scripts/core/guideManager.ts @@ -29,7 +29,7 @@ export class GuideManager { const pathTemplate = new StringTemplate(this.options.output) const groupedChannels = this.channels - .orderBy([(channel: Channel) => channel.xmltv_id]) + .orderBy([(channel: Channel) => channel.index, (channel: Channel) => channel.xmltv_id]) .uniqBy((channel: Channel) => `${channel.xmltv_id}:${channel.site}:${channel.lang}`) .groupBy((channel: Channel) => { return pathTemplate.format({ lang: channel.lang || 'en', site: channel.site || '' }) diff --git a/scripts/core/proxyParser.ts b/scripts/core/proxyParser.ts index 244290d5..3e316ab2 100644 --- a/scripts/core/proxyParser.ts +++ b/scripts/core/proxyParser.ts @@ -2,9 +2,9 @@ import { URL } from 'node:url' type ProxyParserResult = { protocol: string | null - auth: { - username: string | null - password: string | null + auth?: { + username?: string + password?: string } host: string port: number | null @@ -14,14 +14,18 @@ export class ProxyParser { parse(_url: string): ProxyParserResult { const parsed = new URL(_url) - return { + const result: ProxyParserResult = { protocol: parsed.protocol.replace(':', '') || null, - auth: { - username: parsed.username || null, - password: parsed.password || null - }, host: parsed.hostname, port: parsed.port ? parseInt(parsed.port) : null } + + if (parsed.username || parsed.password) { + result.auth = {} + if (parsed.username) result.auth.username = parsed.username + if (parsed.password) result.auth.password = parsed.password + } + + return result } } diff --git a/scripts/core/queueCreator.ts b/scripts/core/queueCreator.ts index a09632e8..8245813a 100644 --- a/scripts/core/queueCreator.ts +++ b/scripts/core/queueCreator.ts @@ -35,8 +35,10 @@ export class QueueCreator { const channelsContent = await this.dataStorage.json('channels.json') const channels = new Collection(channelsContent).map(data => new Channel(data)) + let index = 0 const queue = new Queue() for (const channel of this.parsedChannels.all()) { + channel.index = index++ if (!channel.site || !channel.site_id || !channel.name) continue const configPath = path.resolve(SITES_DIR, `${channel.site}/${channel.site}.config.js`) diff --git a/scripts/models/feed.ts b/scripts/models/feed.ts index 0035bb49..cb90510a 100644 --- a/scripts/models/feed.ts +++ b/scripts/models/feed.ts @@ -69,4 +69,8 @@ export class Feed { return `${this.channel.name} ${this.name}` } + + getStreamId(): string { + return `${this.channelId}@${this.id}` + } }