From 7dd61355b7893a9ebbf342afc24031588c3121d9 Mon Sep 17 00:00:00 2001 From: Brock H Caldwell Date: Fri, 1 Aug 2025 14:10:33 -0500 Subject: [PATCH] fix: backend download option evaluator used by monitor service --- src/Download/DownloadOptionEvaluator.php | 124 ++++++++++------------- 1 file changed, 56 insertions(+), 68 deletions(-) diff --git a/src/Download/DownloadOptionEvaluator.php b/src/Download/DownloadOptionEvaluator.php index bc41e62..caca695 100644 --- a/src/Download/DownloadOptionEvaluator.php +++ b/src/Download/DownloadOptionEvaluator.php @@ -3,92 +3,80 @@ namespace App\Download; use Aimeos\Map; -use App\Monitor\Framework\Entity\Monitor; use App\Torrentio\Result\TorrentioResult; use App\User\Dto\UserPreferences; class DownloadOptionEvaluator { /** - * @param Monitor $monitor * @param TorrentioResult[] $results + * @param UserPreferences $filter * @return TorrentioResult|null * @throws \Throwable */ - public function evaluateOptions(array $results, UserPreferences $userPreferences): ?TorrentioResult + public function evaluateOptions(array $results, UserPreferences $filter): ?TorrentioResult { - $sizeLow = 000; - $sizeHigh = 4096; - - $bestMatches = []; - $matches = []; - - foreach ($results as $result) { - if (!in_array($userPreferences->language, $result->languages)) { - continue; + $matches = Map::from($results)->filter(function ($result) use ($filter) { + if (false === $this->validateFilterItems($result, $filter)) { + return false; } - if ($result->resolution === $userPreferences->resolution - && $result->codec === $userPreferences->codec - ) { - $bestMatches[] = $result; + if (false === $this->validateSize($result, $filter)) { + return false; } - if ($userPreferences->resolution === '2160p' - && $userPreferences->codec === $result->codec - && $result->resolution === '1080p' - ) { - $matches[] = $result; - } + return true; + }); - if ($userPreferences->codec === 'h264' - && $userPreferences->resolution === $result->resolution - && $result->codec === 'h265' - ) { - $matches[] = $result; - } - - if (($userPreferences->codec === null ) - && ($userPreferences->resolution === null )) { - $matches[] = $result; - } - } - - $sizeMatches = []; - - foreach ($bestMatches as $result) { - if (str_contains($result->size, 'GB')) { - $size = (int) trim(str_replace('GB', '', $result->size)) * 1024; - } else { - $size = (int) trim(str_replace('MB', '', $result->size)); - } - - if ($size > $sizeLow && $size < $sizeHigh) { - $sizeMatches[] = $result; - } - } - - if (!empty($sizeMatches)) { - return Map::from($sizeMatches)->usort(fn($a, $b) => $a->seeders <=> $b->seeders)->last(); - } - - foreach ($matches as $result) { - $size = 0; - if (str_contains($result->size, 'GB')) { - $size = (int) trim(str_replace('GB', '', $result->size)) * 1024; - } else { - $size = (int) trim(str_replace('MB', '', $result->size)); - } - - if ($size > $sizeLow && $size < $sizeHigh) { - $sizeMatches[] = $result; - } - } - - if (!empty($sizeMatches)) { - return Map::from($sizeMatches)->usort(fn($a, $b) => $a->seeders <=> $b->seeders)->last(); + if ($matches->count() > 0) { + return Map::from($matches)->usort(fn($a, $b) => $a->seeders <=> $b->seeders)->last(); } return null; } + + private function validateFilterItems(TorrentioResult $result, UserPreferences $filter): bool + { + if (array_intersect($filter->language, $result->languages) === []) { + return false; + } + + $valid = true; + + if (null !== $filter->resolution && !in_array($result->resolution, $filter->resolution)) { + $valid = false; + } + + if (null !== $filter->codec && in_array($result->codec, $filter->codec)) { + $valid = false; + } + + if (null !== $filter->quality && in_array($result->quality, $filter->quality)) { + $valid = false; + } + + if (null !== $filter->provider && in_array($result->provider, $filter->provider)) { + $valid = false; + } + + return $valid; + } + + private function validateSize(TorrentioResult $result, UserPreferences $filter): bool + { + $sizeLow = 000; + $sizeHigh = 4096; + + if (str_contains($result->size, 'GB')) { + $size = (int) trim(str_replace('GB', '', $result->size)) * 1024; + } else { + $size = (int) trim(str_replace('MB', '', $result->size)); + } + + if ($size > $sizeLow && $size < $sizeHigh) { + return true; + } + + return false; + } }