107 lines
4.5 KiB
PHP
107 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Download\Action\Handler;
|
|
|
|
use Aimeos\Map;
|
|
use App\Base\Service\MediaFiles;
|
|
use App\Download\Action\Command\DownloadMediaCommand;
|
|
use App\Download\Action\Command\DownloadSeasonCommand;
|
|
use App\Download\Action\Result\DownloadMediaResult;
|
|
use App\Download\Action\Result\DownloadSeasonResult;
|
|
use App\Download\DownloadOptionEvaluator;
|
|
use App\Tmdb\Tmdb;
|
|
use App\Torrentio\Action\Command\GetTvShowOptionsCommand;
|
|
use App\Torrentio\Action\Handler\GetTvShowOptionsHandler;
|
|
use App\User\Dto\UserPreferencesFactory;
|
|
use App\User\Framework\Repository\UserRepository;
|
|
use App\Base\Util\PTN;
|
|
use OneToMany\RichBundle\Contract\CommandInterface;
|
|
use OneToMany\RichBundle\Contract\HandlerInterface;
|
|
use OneToMany\RichBundle\Contract\ResultInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\Messenger\MessageBusInterface;
|
|
|
|
/** @implements HandlerInterface<DownloadSeasonCommand, DownloadMediaResult> */
|
|
readonly class DownloadSeasonHandler implements HandlerInterface
|
|
{
|
|
public function __construct(
|
|
private MediaFiles $mediaFiles,
|
|
private LoggerInterface $logger,
|
|
private Tmdb $tmdb,
|
|
private MessageBusInterface $bus,
|
|
private DownloadOptionEvaluator $downloadOptionEvaluator,
|
|
private GetTvShowOptionsHandler $getTvShowOptionsHandler,
|
|
private UserRepository $userRepository,
|
|
) {}
|
|
|
|
public function handle(CommandInterface $command): ResultInterface
|
|
{
|
|
$series = $this->tmdb->mediaDetails($command->imdbId, $command->mediaType);
|
|
$this->logger->info('> [DownloadTvSeasonHandler] Executing DownloadTvSeasonHandler for "' . $series->title . '" season ' . $command->season);
|
|
|
|
$episodesInSeason = Map::from($series->episodes[$command->season]);
|
|
$this->logger->info('> [DownloadTvSeasonHandler] ...Found ' . count($episodesInSeason) . ' episodes in season ' . $command->season);
|
|
|
|
$downloadCommands = [];
|
|
foreach ($episodesInSeason as $episode) {
|
|
$this->logger->info('> [DownloadTvSeasonHandler] ...Evaluating episode ' . $episode['episode_number']);
|
|
|
|
$results = $this->getTvShowOptionsHandler->handle(
|
|
new GetTvShowOptionsCommand(
|
|
$series->tmdbId,
|
|
$command->imdbId,
|
|
$command->season,
|
|
$episode['episode_number']
|
|
)
|
|
);
|
|
|
|
$this->logger->info('> [DownloadTvSeasonHandler] ......Found ' . count($results->results) . ' total download options, beginning evaluation');
|
|
|
|
$userPreferences = UserPreferencesFactory::createFromUser(
|
|
$this->userRepository->findOneBy(['id' => $command->userId])
|
|
);
|
|
|
|
$result = $this->downloadOptionEvaluator->evaluateOptions($results->results, $userPreferences);
|
|
|
|
if (null !== $result) {
|
|
$this->logger->info('> [DownloadTvSeasonHandler] ......Found 1 matching result');
|
|
$this->logger->info('> [DownloadTvSeasonHandler] ......Dispatching DownloadMediaCommand for "' . $series->title . '" season ' . $command->season . ' episode ' . $episode['episode_number']);
|
|
$downloadCommand = new DownloadMediaCommand(
|
|
$result->url,
|
|
$series->title,
|
|
$result->filename,
|
|
'tvshows',
|
|
$command->imdbId,
|
|
$command->userId,
|
|
);
|
|
$this->bus->dispatch($downloadCommand);
|
|
$downloadCommands[] = $downloadCommand;
|
|
} else {
|
|
$this->logger->info('> [DownloadTvSeasonHandler] ......Found 0 matching results');
|
|
}
|
|
}
|
|
|
|
return new DownloadSeasonResult(
|
|
status: 200,
|
|
message: 'Success',
|
|
data: ['downloads' => $downloadCommands],
|
|
);
|
|
}
|
|
|
|
private function getDownloadedEpisodes(string $title)
|
|
{
|
|
// Check current episodes
|
|
$downloadedEpisodes = $this->mediaFiles
|
|
->getEpisodes($title)
|
|
->map(fn($episode) => (object) (new PTN())->parse($episode))
|
|
->filter(fn ($episode) =>
|
|
property_exists($episode, 'episode')
|
|
&& property_exists($episode, 'season')
|
|
&& null !== $episode->episode
|
|
&& null !== $episode->season
|
|
)
|
|
->rekey(fn($episode) => $episode->episode);
|
|
$this->logger->info('> [MonitorTvSeasonHandler] Found ' . count($downloadedEpisodes) . ' downloaded episodes for title: ' . $monitor->getTitle());
|
|
}
|
|
}
|