diff --git a/src/Search/Framework/Command/SearchCommand.php b/src/Search/Framework/Command/SearchCommand.php new file mode 100644 index 0000000..909da44 --- /dev/null +++ b/src/Search/Framework/Command/SearchCommand.php @@ -0,0 +1,194 @@ +searchHandler = $searchHandler; + $this->getTvShowOptionsHandler = $getTvShowOptionsHandler; + $this->getMovieOptionsHandler = $getMovieOptionsHandler; + $this->userRepository = $userRepository; + $this->downloadRepository = $downloadRepository; + $this->downloadMediaHandler = $downloadMediaHandler; + } + + protected function configure() + { + $this->addArgument('term', InputArgument::REQUIRED); + } + + public function run(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + $command = new CommandInput($input->getArgument('term')); + + $mediaOptions = $this->searchHandler->handle($command); + $mediaChoice = $this->askToChooseMediaOption($io, $output, $mediaOptions); + + $downloadOptions = $this->fetchDownloadOptions($mediaChoice); + $downloadChoice = $this->askToChooseDownloadOption($io, $output, $downloadOptions); + + $confirmation = $this->askToConfirmDownload($io, $output, $downloadChoice); + + if (true === $confirmation) { + $this->submitDownload($io, $mediaChoice, $downloadChoice); + } else { + $io->success('No results found.'); + } + + + return Command::SUCCESS; + } + + private function askToChooseMediaOption(SymfonyStyle $io, OutputInterface $output, SearchResult $result): TmdbResult + { + $table = new Table($output) + ->setHeaders(['ID', 'Title', 'Year', 'IMDb ID', 'Description']) + ->setRows( + Map::from($result->results) + ->map(fn ($result, $index) => [$index, $result->title, $result->year, $result->imdbId, substr($result->description, 0, 80) . '...']) + ->toArray() + ); + + $table->render(); + + $question = new Question('Enter the ID of the correct result: '); + $choiceId = $io->askQuestion($question); + $choice = $result->results[$choiceId]; + + $io->info('You chose: ' . $choice->title); + $io->table( + ['ID', 'Title', 'Year', 'IMDb ID', 'Description'], + [ + [$choiceId, $choice->title, $choice->year, $choice->imdbId, substr($choice->description, 0, 80) . '...'], + ] + ); + + return $choice; + } + + private function fetchDownloadOptions(TmdbResult $result): array + { + $handlers = [ + 'movies' => $this->getMovieOptionsHandler, + 'tvshows' => $this->getTvShowOptionsHandler, + ]; + + $handler = $handlers[$result->mediaType]; + + if ("movies" === $result->mediaType) { + $command = new GetMovieOptionsCommand( + $result->tmdbId, + $result->imdbId + ); + } else { + $command = new GetTvShowOptionsCommand( + $result->tmdbId, + $result->imdbId, + 1, + 1 + ); + } + + $result = $handler->handle($command); + return $result->results; + } + + private function askToChooseDownloadOption(SymfonyStyle $io, OutputInterface $output, array $options): TorrentioResult + { + $table = new Table($output) + ->setHeaders(['ID', 'Size', 'Resolution', 'Codec', 'Seeders', 'Provider', 'Language']) + ->setRows( + Map::from($options) + ->map(fn (TorrentioResult $result, $index) => [$index, $result->size, $result->resolution, $result->codec, $result->seeders, $result->provider, implode(', ', $result->languages)]) + ->toArray() + ); + $table->render(); + + $question = new Question('Enter the ID of the item to download: '); + $choiceId = $io->askQuestion($question); + $choice = $options[$choiceId]; + $io->info('You chose: ' . $choice->title); + + return $choice; + } + + private function askToConfirmDownload(SymfonyStyle $io, OutputInterface $output, TorrentioResult $downloadOption): bool + { + $question = new ChoiceQuestion('Are you sure you want to download the above file?', ['yes', 'no']); + $choice = $io->askQuestion($question); + return $choice === 'yes'; + } + + private function submitDownload(SymfonyStyle $io, TmdbResult $mediaChoice, TorrentioResult $downloadOption): void + { + $io->writeln('> Adding download record'); + $user = $this->userRepository->find(1); + $download = $this->downloadRepository->insert( + $user, + $downloadOption->url, + $downloadOption->title, + $downloadOption->filename, + $mediaChoice->imdbId, + $mediaChoice->mediaType, + ); + + $io->writeln('> Download record added: ' . $download->getId()); + $downloadCommand = new DownloadMediaCommand( + $download->getUrl(), + $download->getTitle(), + $download->getFilename(), + $download->getMEdiaType(), + $download->getImdbId(), + $download->getUser()->getId(), + $download->getId() + ); + + $io->writeln('> Submitting download to queue'); + $this->downloadMediaHandler->handle($downloadCommand); + + $io->writeln('> Download added to queue'); + } +}