From a9c6abe06bcb65fc0d8d8c76c1d5d627d9f44712 Mon Sep 17 00:00:00 2001 From: Brock H Caldwell Date: Thu, 22 May 2025 20:48:08 -0500 Subject: [PATCH] feat: download option cache on search --- compose.yml | 2 +- config/packages/messenger.yaml | 1 + docs/examples/compose.yml | 2 ++ src/Controller/SearchController.php | 38 ++++++++++++++++++++++++++--- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/compose.yml b/compose.yml index 56db54f..53c2996 100755 --- a/compose.yml +++ b/compose.yml @@ -39,7 +39,7 @@ services: - ./:/app - ./var/downloads/movies:/var/download/movies - ./var/downloads/tvshows:/var/download/tvshows - command: -v --time-limit=3600 --limit=10 + command: -vvv --time-limit=3600 env_file: - .env depends_on: diff --git a/config/packages/messenger.yaml b/config/packages/messenger.yaml index b7726bf..0a1bb6a 100644 --- a/config/packages/messenger.yaml +++ b/config/packages/messenger.yaml @@ -29,6 +29,7 @@ framework: 'App\Monitor\Action\Command\MonitorTvSeasonCommand': async 'App\Monitor\Action\Command\MonitorTvShowCommand': async 'App\Monitor\Action\Command\MonitorMovieCommand': async + 'App\Torrentio\Action\Command\GetTvShowOptionsCommand': async # when@test: # framework: diff --git a/docs/examples/compose.yml b/docs/examples/compose.yml index a1857c1..bb0c220 100644 --- a/docs/examples/compose.yml +++ b/docs/examples/compose.yml @@ -30,6 +30,7 @@ services: command: -v --time-limit=3600 --limit=10 env_file: - .env + restart: always depends_on: app: condition: service_healthy @@ -46,6 +47,7 @@ services: command: -vv --time-limit=3600 env_file: - .env + restart: always depends_on: app: condition: service_healthy diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index c260f39..8ab5a4f 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -6,18 +6,20 @@ use App\Search\Action\Handler\GetMediaInfoHandler; use App\Search\Action\Handler\SearchHandler; use App\Search\Action\Input\GetMediaInfoInput; use App\Search\Action\Input\SearchInput; -use Carbon\Carbon; +use App\Tmdb\TmdbResult; +use App\Torrentio\Action\Command\GetMovieOptionsCommand; +use App\Torrentio\Action\Command\GetTvShowOptionsCommand; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Routing\Attribute\Route; -use Symfony\Contracts\Cache\CacheInterface; -use Symfony\Contracts\Cache\ItemInterface; final class SearchController extends AbstractController { public function __construct( private SearchHandler $searchHandler, private GetMediaInfoHandler $getMediaInfoHandler, + private MessageBusInterface $bus, ) {} #[Route('/search', name: 'app_search', methods: ['GET'])] @@ -36,6 +38,8 @@ final class SearchController extends AbstractController GetMediaInfoInput $input, ): Response { $result = $this->getMediaInfoHandler->handle($input->toCommand()); + $this->warmDownloadOptionCache($result->media); + return $this->render('search/result.html.twig', [ 'results' => $result, 'filter' => [ @@ -48,4 +52,32 @@ final class SearchController extends AbstractController ] ]); } + + private function warmDownloadOptionCache(TmdbResult $result) + { + if ($result->mediaType === 'tvshows') { + // dispatches a job to get the download options + // for each episode and load them in cache + foreach ($result->episodes as $season => $episodes) { + // Only do the first 2 seasons, so we reduce + // getting rate-limited by Torrentio + if ($season > 2) { + return; + } + foreach ($episodes as $episode) { + $this->bus->dispatch(new GetTvShowOptionsCommand( + tmdbId: $result->tmdbId, + imdbId: $result->imdbId, + season: $season, + episode: $episode['episode_number'], + )); + } + } + } elseif ($result->mediaType === 'movies') { + $this->bus->dispatch(new GetMovieOptionsCommand( + $result->tmdbId, + $result->imdbId, + )); + } + } }