From aa042e8275099ba91cbde810a9efb3dd42c53488 Mon Sep 17 00:00:00 2001 From: Brock H Caldwell Date: Sun, 31 Aug 2025 18:01:40 -0500 Subject: [PATCH] fix: creates dedicated http client for torrentio --- src/Torrentio/Client/HttpClient.php | 55 +++++++++++++++++++++++ src/Torrentio/Client/Torrentio.php | 70 +++-------------------------- 2 files changed, 61 insertions(+), 64 deletions(-) create mode 100644 src/Torrentio/Client/HttpClient.php diff --git a/src/Torrentio/Client/HttpClient.php b/src/Torrentio/Client/HttpClient.php new file mode 100644 index 0000000..98defb7 --- /dev/null +++ b/src/Torrentio/Client/HttpClient.php @@ -0,0 +1,55 @@ +client = new GuzzleClient([ + 'base_uri' => sprintf($this->baseUrl, $this->realDebridKey), + ]); + } + + public function get(string $imdbId, array $cacheTags = []): array + { + $cacheKey = "torrentio.{$imdbId}"; + + return $this->cache->get($cacheKey, function (ItemInterface $item) use ($imdbId, $cacheTags) { + $item->expiresAt(Carbon::now()->addHour()->setMinute(0)->setSecond(0)); + if (count($cacheTags) > 0) { + $item->tag($cacheTags); + } + try { + $response = $this->client->get("$imdbId.json"); + return json_decode( + $response->getBody()->getContents(), + true + ); + } catch (\Throwable $exception) { + dd($exception); + if ($exception->getCode() === 429) { + $this->logger->warning("> [TorrentioClient] Rate limit exceeded"); + return null; + } + } + + $this->logger->error("> [TorrentioClient] Request error: " . $response->getStatusCode() . " - " . $response->getBody()->getContents()); + return []; + }); + } +} diff --git a/src/Torrentio/Client/Torrentio.php b/src/Torrentio/Client/Torrentio.php index 3ab0d3d..631acc4 100644 --- a/src/Torrentio/Client/Torrentio.php +++ b/src/Torrentio/Client/Torrentio.php @@ -2,59 +2,19 @@ namespace App\Torrentio\Client; -use App\Torrentio\Client\Rule\DownloadOptionFilter\Resolution; -use App\Torrentio\Client\Rule\RuleEngine; use App\Torrentio\Result\ResultFactory; -use Carbon\Carbon; use App\Torrentio\Exception\TorrentioRateLimitException; -use GuzzleHttp\Client; -use Psr\Log\LoggerInterface; -use Symfony\Component\DependencyInjection\Attribute\Autowire; -use Symfony\Contracts\Cache\ItemInterface; -use Symfony\Contracts\Cache\TagAwareCacheInterface; class Torrentio { - private string $baseUrl = 'https://torrentio.strem.fun/providers%253Dyts%252Ceztv%252Crarbg%252C1337x%252Cthepiratebay%252Ckickasstorrents%252Ctorrentgalaxy%252Cmagnetdl%252Chorriblesubs%252Cnyaasi%7Csort%253Dqualitysize%7Cqualityfilter%253D480p%252Cscr%252Ccam%7Crealdebrid={realDebridKey}/stream/movie'; - - private string $searchUrl; - - private Client $client; - public function __construct( - #[Autowire(env: 'REAL_DEBRID_KEY')] private string $realDebridKey, - private TagAwareCacheInterface $cache, - private LoggerInterface $logger, - ) { - $this->searchUrl = str_replace('{realDebridKey}', $this->realDebridKey, $this->baseUrl); - $this->client = new Client([ - 'base_uri' => $this->searchUrl, - ]); - } + private readonly HttpClient $client, + ) {} public function search(string $imdbCode, string $type, bool $parseResults = true): array { - $cacheKey = "torrentio.{$imdbCode}"; - - $results = $this->cache->get($cacheKey, function (ItemInterface $item) use ($imdbCode, $type) { - $item->expiresAt(Carbon::now()->addHour()->setMinute(0)->setSecond(0)); - $item->tag(['torrentio', $type, $imdbCode]); - try { - $response = $this->client->get("$this->searchUrl/$imdbCode.json"); - return json_decode( - $response->getBody()->getContents(), - true - ); - } catch (\Throwable $exception) { - if ($exception->getCode() === 429) { - $this->logger->warning("> [TorrentioClient] Rate limit exceeded"); - return null; - } - } - - $this->logger->error("> [TorrentioClient] Request error: " . $response->getStatusCode() . " - " . $response->getBody()->getContents()); - return []; - }); + $cacheTags = ['torrentio', $type, $imdbCode]; + $results = $this->client->get($imdbCode, $cacheTags); if (true === $parseResults) { return $this->parse($results); @@ -65,26 +25,8 @@ class Torrentio public function fetchEpisodeResults(string $imdbId, int $season, int $episode, bool $parseResults = true): array { - $cacheKey = "torrentio.$imdbId.$season.$episode"; - $results = $this->cache->get($cacheKey, function (ItemInterface $item) use ($imdbId, $season, $episode) { - $item->expiresAt(Carbon::now()->addHour()->setMinute(0)->setSecond(0)); - $item->tag(['torrentio', 'tvshows', 'torrentio.tvshows', $imdbId, "torrentio.$imdbId", "$imdbId.$season", "torrentio.$imdbId.$season", "$imdbId.$season.$episode", "torrentio.$imdbId.$season.$episode"]); - try { - $response = $this->client->get("$this->searchUrl/$imdbId:$season:$episode.json"); - return json_decode( - $response->getBody()->getContents(), - true - ); - } catch (\Throwable $exception) { - if ($exception->getCode() === 429) { - $this->logger->warning("> [TorrentioClient] Rate limit exceeded"); - return null; - } - } - - $this->logger->error("> [TorrentioClient] Request error: " . $response->getStatusCode() . " - " . $response->getBody()->getContents()); - return []; - }); + $cacheTags = ['torrentio', 'tvshows', 'torrentio.tvshows', $imdbId, "torrentio.$imdbId", "$imdbId.$season", "torrentio.$imdbId.$season", "$imdbId.$season.$episode", "torrentio.$imdbId.$season.$episode"]; + $results = $this->client->get($imdbId, $cacheTags); if (null === $results) { throw new TorrentioRateLimitException();