Files
torsearch/src/Torrentio/Client/Torrentio.php

72 lines
2.0 KiB
PHP

<?php
namespace App\Torrentio\Client;
use App\Torrentio\Result\ResultFactory;
use App\Torrentio\Exception\TorrentioRateLimitException;
class Torrentio
{
public function __construct(
private readonly HttpClient $client,
) {}
public function search(string $imdbCode, string $type, bool $parseResults = true): array
{
$cacheTags = ['torrentio', $type, $imdbCode];
$results = $this->client->get($imdbCode, $cacheTags);
if (true === $parseResults) {
return $this->parse($results, $imdbCode);
}
return $results;
}
public function fetchEpisodeResults(string $imdbId, int $season, int $episode, bool $parseResults = true): array
{
$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:$season:$episode", $cacheTags);
if (null === $results) {
throw new TorrentioRateLimitException();
}
if (true === $parseResults) {
return $this->parse($results, $imdbId);
}
return $results;
}
public function parse(array $data, string $imdbId): array
{
$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,
$imdbId
);
$results[] = $result;
}
return $results;
}
}