Compare commits

..

2 Commits

Author SHA1 Message Date
Brock H Caldwell
759f64ea22 fix(DownloadSeasonHandler): actually captures season/episode numbers 2026-02-08 21:51:02 -06:00
Brock H Caldwell
cc88660c07 fix(DownloadSeasonHandler): captures episode id
Some checks failed
SonarQube Scan / SonarQube Trigger (push) Failing after 13s
2026-02-08 15:11:14 -06:00

View File

@@ -10,6 +10,8 @@ use App\Download\Action\Command\DownloadSeasonCommand;
use App\Download\Action\Result\DownloadMediaResult;
use App\Download\Action\Result\DownloadSeasonResult;
use App\Download\DownloadOptionEvaluator;
use App\Download\Framework\Entity\Download;
use App\Download\Framework\Repository\DownloadRepository;
use App\Tmdb\TmdbClient;
use App\Torrentio\Action\Command\GetTvShowOptionsCommand;
use App\Torrentio\Action\Handler\GetTvShowOptionsHandler;
@@ -26,13 +28,13 @@ use Symfony\Component\Messenger\MessageBusInterface;
readonly class DownloadSeasonHandler implements HandlerInterface
{
public function __construct(
private MediaFiles $mediaFiles,
private LoggerInterface $logger,
private TmdbClient $tmdb,
private MessageBusInterface $bus,
private MediaFiles $mediaFiles,
private LoggerInterface $logger,
private TmdbClient $tmdb,
private MessageBusInterface $bus,
private DownloadOptionEvaluator $downloadOptionEvaluator,
private GetTvShowOptionsHandler $getTvShowOptionsHandler,
private UserRepository $userRepository,
private UserRepository $userRepository, private DownloadRepository $downloadRepository,
) {}
public function handle(CommandInterface $command): ResultInterface
@@ -68,13 +70,16 @@ readonly class DownloadSeasonHandler implements HandlerInterface
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->episodeNumber);
$download = $this->createDownload($command, $result->url, $series->title, $result->filename, $episode->episodeNumber);
$this->logger->info('> [DownloadTvSeasonHandler] ......Created Download entity with id ' . $download->getId());
$downloadCommand = new DownloadMediaCommand(
$result->url,
$series->title,
$result->filename,
'tvshows',
$command->imdbId,
$command->userId,
$download->getUrl(),
$download->getTitle(),
$download->getFilename(),
$download->getMediaType(),
$download->getImdbId(),
$download->getUser()->getId(),
$download->getId()
);
$this->bus->dispatch($downloadCommand);
$downloadCommands[] = $downloadCommand;
@@ -90,19 +95,27 @@ readonly class DownloadSeasonHandler implements HandlerInterface
);
}
private function getDownloadedEpisodes(string $title)
private function createDownload(DownloadSeasonCommand $command, string $url, string $title, string $filename, int $episodeNumber): Download
{
// 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->episodeNumber
&& null !== $episode->season
)
->rekey(fn($episode) => $episode->episodeNumber);
$this->logger->info('> [MonitorTvSeasonHandler] Found ' . count($downloadedEpisodes) . ' downloaded episodes for title: ' . $monitor->getTitle());
$download = new Download();
$download->setUrl($url);
$download->setTitle($title);
$download->setFilename($filename);
$download->setImdbId($command->imdbId);
$download->setMediaType(MediaType::TvShow->value);
$download->setEpisodeId($this->getEpisodeNumber($command->season, $episodeNumber));
$download->setUser($this->userRepository->find($command->userId));
$this->downloadRepository->getEntityManager()->persist($download);
$this->downloadRepository->getEntityManager()->flush();
return $download;
}
private function getEpisodeNumber(int $season, int $episode): string
{
return sprintf(
"S%sE%s",
str_pad($season, 2, "0", STR_PAD_LEFT),
str_pad($episode, 2, "0", STR_PAD_LEFT)
);
}
}