120 lines
4.6 KiB
PHP
120 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Torrentio\Client;
|
|
|
|
use App\Torrentio\Client\Rule\DownloadOptionFilter\Resolution;
|
|
use App\Torrentio\Client\Rule\RuleEngine;
|
|
use App\Torrentio\MediaResult;
|
|
use App\Torrentio\Result\ResultFactory;
|
|
use Carbon\Carbon;
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
|
use Symfony\Contracts\Cache\CacheInterface;
|
|
use Symfony\Contracts\Cache\ItemInterface;
|
|
|
|
/**
|
|
* ToDo: Fix
|
|
*/
|
|
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/{imdbCode}.json';
|
|
// private string $baseUrl = 'https://torrentio.strem.fun/providers=yts,eztv,rarbg,1337x,thepiratebay,kickasstorrents,torrentgalaxy,magnetdl,horriblesubs|sort=qualitysize|qualityfilter=480p,cam,unknown|debridoptions=nodownloadlinks|realdebrid=QYYBR7OSQ4VEFKWASDEZ2B4VO67KHUJY6IWOT7HHA7ATXO7QCYDQ/stream/{imdbCode}.json';
|
|
|
|
private string $searchUrl;
|
|
|
|
public function __construct(
|
|
#[Autowire(env: 'REAL_DEBRID_KEY')] private string $realDebridKey,
|
|
private CacheInterface $cache,
|
|
) {
|
|
$this->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 searchBySeriesSeason(MediaResult $series): MediaResult
|
|
{
|
|
$imdbCode = $series->imdbId;
|
|
// foreach ($series->episodes as $season => $episodes) {
|
|
// foreach ($episodes as $key => $episode) {
|
|
// $cacheKey = "torrentio.$series->imdbId.$season.{$episode['episode_number']}";
|
|
// $downloadOptions = $this->cache->get($cacheKey, function (ItemInterface $item) use ($imdbCode, $season, $episode) {
|
|
// $item->expiresAt(new \DateTimeImmutable("today 11:59 pm"));
|
|
// $response = file_get_contents(str_replace('{imdbCode}', "$imdbCode:$season:{$episode['episode_number']}", $this->searchUrl));
|
|
// return json_decode(
|
|
// $response,
|
|
// true
|
|
// );
|
|
// });
|
|
// $series->episodes[$season][$key]['download_options'] = $this->parse($downloadOptions, []);
|
|
// }
|
|
// }
|
|
return $series;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|