searchUrl = str_replace('{realDebridKey}', $this->realDebridKey, $this->baseUrl); } public function search(string $imdbCode, string $type, array $filter = []): array { $cacheKey = "torrentio.{$imdbCode}"; $results = $this->cache->get($cacheKey, function (ItemInterface $item) use ($imdbCode) { $item->expiresAt(Carbon::now()->addHour()->setMinute(0)->setSecond(0)); $response = file_get_contents(str_replace('{imdbCode}', $imdbCode, $this->searchUrl)); return json_decode( $response, true ); }); return $this->parse($results, $filter); } public function fetchEpisodeResults(string $imdbId, int $season, int $episode): 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)); $response = file_get_contents(str_replace('{imdbCode}', "$imdbId:$season:$episode", $this->searchUrl)); return json_decode( $response, true ); }); return $this->parse($results, []); } public function parse(array $data, array $filter): array { $ruleEngine = new RuleEngine(); foreach ($filter as $rule => $value) { if ('resolution' === $rule) { $ruleEngine->addRule(new Resolution($value)); } } $results = []; foreach ($data['streams'] as $stream) { if (!str_starts_with($stream['url'], "https")) { continue; } if ( array_key_exists('behaviorHints', $stream) && array_key_exists('bingeGroup', $stream['behaviorHints']) ) { $bingeGroup = $stream['behaviorHints']['bingeGroup']; } else { $bingeGroup = '-'; } $result = ResultFactory::map( $stream['url'], $stream['title'], $bingeGroup ); if ($ruleEngine->validateAll($result)) { $results[] = $result; } } return $results; } }