From d15b21bfd333d2fed5224504005fc22f454686b4 Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Fri, 24 Jan 2025 20:02:33 +0300 Subject: [PATCH] Update grabber.ts --- scripts/core/grabber.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/scripts/core/grabber.ts b/scripts/core/grabber.ts index 4bed77a3..2248cbc5 100644 --- a/scripts/core/grabber.ts +++ b/scripts/core/grabber.ts @@ -1,8 +1,9 @@ import { EPGGrabber, GrabCallbackData, EPGGrabberMock, SiteConfig, Channel } from 'epg-grabber' import { Logger, Collection } from '@freearhey/core' -import { Queue } from './' +import { Queue, ProxyParser } from './' import { GrabOptions } from '../commands/epg/grab' import { TaskQueue, PromisyClass } from 'cwait' +import { SocksProxyAgent } from 'socks-proxy-agent' type GrabberProps = { logger: Logger @@ -14,6 +15,7 @@ export class Grabber { logger: Logger queue: Queue options: GrabOptions + grabber: EPGGrabber | EPGGrabberMock constructor({ logger, queue, options }: GrabberProps) { this.logger = logger @@ -23,6 +25,7 @@ export class Grabber { } async grab(): Promise<{ channels: Collection; programs: Collection }> { + const proxyParser = new ProxyParser() const taskQueue = new TaskQueue(Promise as PromisyClass, this.options.maxConnections) const total = this.queue.size() @@ -49,6 +52,24 @@ export class Grabber { config.delay = delay } + if (this.options.proxy !== undefined) { + const proxy = proxyParser.parse(this.options.proxy) + + if ( + proxy.protocol && + ['socks', 'socks5', 'socks5h', 'socks4', 'socks4a'].includes(String(proxy.protocol)) + ) { + const socksProxyAgent = new SocksProxyAgent(this.options.proxy) + + config.request = { + ...config.request, + ...{ httpAgent: socksProxyAgent, httpsAgent: socksProxyAgent } + } + } else { + config.request = { ...config.request, ...{ proxy } } + } + } + const _programs = await this.grabber.grab( channel, date,