Compare commits

..

3 Commits

13 changed files with 181 additions and 72 deletions

View File

@@ -23,7 +23,7 @@
@apply bg-green-950 hover:bg-green-900 border-green-500 @apply bg-green-950 hover:bg-green-900 border-green-500
} }
.alert-warning { .alert-warning {
@apply bg-yellow-500/70 hover:bg-yellow-600 border-yellow-400 text-black @apply bg-yellow-500 hover:bg-yellow-600 border-yellow-400 text-black
} }
} }

View File

@@ -4,6 +4,15 @@
# Put parameters here that don't need to change on each machine where the app is deployed # Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration # https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters: parameters:
# App
app.url: '%env(APP_URL)%'
# Debrid Services
app.debrid.real_debrid.key: '%env(REAL_DEBRID_KEY)%'
# TMDB Key
app.meta_provider.tmdb.key: '%env(TMDB_API)%'
# Media # Media
media.base_path: '/var/download' media.base_path: '/var/download'
media.default_movies_dir: movies media.default_movies_dir: movies

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Base;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
final class ConfigResolver
{
private array $messages = [];
public function __construct(
#[Autowire(param: 'app.url')]
private readonly ?string $appUrl = null,
#[Autowire(param: 'app.debrid.real_debrid.key')]
private readonly ?string $realDebridApiKey = null,
#[Autowire(param: 'app.meta_provider.tmdb.key')]
private readonly ?string $tmdbApiKey = null,
#[Autowire(param: 'media.movies_path')]
private readonly ?string $moviesPath = null,
#[Autowire(param: 'media.tvshows.path')]
private readonly ?string $tvshowsPath = null,
) {}
public function validate(): bool
{
$valid = true;
if (null === $this->realDebridApiKey || "" === $this->realDebridApiKey) {
$this->messages[] = "Your Real Debrid API key is missing. Please set it to the 'REAL_DEBRID_KEY' environment variable.";
$valid = false;
}
if (null === $this->tmdbApiKey || "" === $this->tmdbApiKey) {
$this->messages[] = "Your TMDB API key is missing. Please set it to the 'TMDB_API' environment variable.";
$valid = false;
}
return $valid;
}
public function getMessages(): array
{
return $this->messages;
}
}

View File

@@ -4,13 +4,13 @@ namespace App\Monitor\Action\Handler;
use App\Download\Action\Command\DownloadMediaCommand; use App\Download\Action\Command\DownloadMediaCommand;
use App\Monitor\Action\Command\MonitorMovieCommand; use App\Monitor\Action\Command\MonitorMovieCommand;
use App\Monitor\Action\Result\MonitorMovieResult;
use App\Monitor\Action\Result\MonitorTvEpisodeResult; use App\Monitor\Action\Result\MonitorTvEpisodeResult;
use App\Monitor\Framework\Repository\MonitorRepository; use App\Monitor\Framework\Repository\MonitorRepository;
use App\Monitor\Service\MonitorOptionEvaluator; use App\Monitor\Service\MonitorOptionEvaluator;
use App\Tmdb\Tmdb; use App\Tmdb\Tmdb;
use App\Torrentio\Action\Command\GetTvShowOptionsCommand; use App\Torrentio\Action\Command\GetTvShowOptionsCommand;
use App\Torrentio\Action\Handler\GetTvShowOptionsHandler; use App\Torrentio\Action\Handler\GetTvShowOptionsHandler;
use App\User\Dto\UserPreferencesFactory;
use Carbon\Carbon; use Carbon\Carbon;
use DateTimeImmutable; use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
@@ -35,59 +35,65 @@ readonly class MonitorTvEpisodeHandler implements HandlerInterface
public function handle(CommandInterface $command): ResultInterface public function handle(CommandInterface $command): ResultInterface
{ {
$this->logger->info('> [MonitorTvEpisodeHandler] Executing MonitorTvEpisodeHandler'); try {
$monitor = $this->monitorRepository->find($command->movieMonitorId); $monitor = $this->monitorRepository->find($command->movieMonitorId);
$this->logger->info('> [MonitorTvEpisodeHandler] Executing MonitorTvEpisodeHandler for ' . $monitor->getTitle() . ' season ' . $monitor->getSeason() . ' episode ' . $monitor->getEpisode());
$episodeData = $this->tmdb->episodeDetails($monitor->getTmdbId(), $monitor->getSeason(), $monitor->getEpisode()); $episodeData = $this->tmdb->episodeDetails($monitor->getTmdbId(), $monitor->getSeason(), $monitor->getEpisode());
if (Carbon::createFromTimestamp($episodeData->episodeAirDate) > Carbon::now()) { if (Carbon::createFromTimestamp($episodeData->episodeAirDate) > Carbon::today('UTC')) {
$this->logger->info('> [MonitorTvEpisodeHandler] Episode has not aired yet, skipping for now'); $this->logger->info('> [MonitorTvEpisodeHandler] ...Episode has not aired yet, skipping for now');
return new MonitorTvEpisodeResult( return new MonitorTvEpisodeResult(
status: 'OK', status: 'OK',
result: [ result: [
'message' => 'No change', 'message' => 'No change',
'monitor' => $monitor, 'monitor' => $monitor,
] ]
);
}
$monitor->setStatus('In Progress');
$this->monitorRepository->getEntityManager()->flush();
$results = $this->getTvShowOptionsHandler->handle(
new GetTvShowOptionsCommand(
$monitor->getTmdbId(),
$monitor->getImdbId(),
$monitor->getSeason(),
$monitor->getEpisode()
)
); );
}
$monitor->setStatus('In Progress'); $this->logger->info('> [MonitorTvEpisodeHandler] ...Found ' . count($results->results) . ' total download options, beginning evaluation');
$this->monitorRepository->getEntityManager()->flush();
$this->logger->info('> [MonitorTvEpisodeHandler] Searching for "' . $monitor->getTitle() . '" season ' . $monitor->getSeason() . ' episode ' . $monitor->getEpisode() . ' download options'); $result = $this->monitorOptionEvaluator->evaluateOptions($monitor, $results->results);
$results = $this->getTvShowOptionsHandler->handle(
new GetTvShowOptionsCommand(
$monitor->getTmdbId(),
$monitor->getImdbId(),
$monitor->getSeason(),
$monitor->getEpisode()
)
);
$this->logger->info('> [MonitorTvEpisodeHandler] Found ' . count($results->results) . ' download options'); if (null !== $result) {
$this->logger->info('> [MonitorTvEpisodeHandler] ...Found 1 matching result found: dispatching DownloadMediaCommand for "' . $result->title . '"', ['filter' => UserPreferencesFactory::createFromUser($monitor->getUser())]);
$this->bus->dispatch(new DownloadMediaCommand(
$result->url,
$monitor->getTitle(),
$result->filename,
'tvshows',
$monitor->getImdbId(),
$monitor->getUser()->getId(),
));
$monitor->setStatus('Complete');
$monitor->setDownloadedAt(new DateTimeImmutable());
} else {
$this->logger->info('> [MonitorTvEpisodeHandler] ...Found 0 matching results found, monitor will run at next interval');
$monitor->setStatus('Active');
}
$result = $this->monitorOptionEvaluator->evaluateOptions($monitor, $results->results); $monitor->setLastSearch(new DateTimeImmutable());
$monitor->setSearchCount($monitor->getSearchCount() + 1);
if (null !== $result) { $this->entityManager->flush();
$this->logger->info('> [MonitorTvEpisodeHandler] 1 matching result found: dispatching DownloadMediaCommand for "' . $result->title . '"'); } catch (\Throwable $exception) {
$this->bus->dispatch(new DownloadMediaCommand( $this->logger->error('> [MonitorTvEpisodeHandler] ...Exception thrown: ' . $exception->getMessage());
$result->url, $this->logger->error($exception->getMessage());
$monitor->getTitle(),
$result->filename,
'tvshows',
$monitor->getImdbId(),
$monitor->getUser()->getId(),
));
$monitor->setStatus('Complete');
$monitor->setDownloadedAt(new DateTimeImmutable());
} else {
$this->logger->info('> [MonitorTvEpisodeHandler] 0 matching results found, monitor will run at next interval');
$monitor->setStatus('Active'); $monitor->setStatus('Active');
$this->monitorRepository->getEntityManager()->flush();
} }
$monitor->setLastSearch(new DateTimeImmutable());
$monitor->setSearchCount($monitor->getSearchCount() + 1);
$this->entityManager->flush();
return new MonitorTvEpisodeResult( return new MonitorTvEpisodeResult(
status: 'OK', status: 'OK',
result: [ result: [

View File

@@ -61,26 +61,27 @@ readonly class MonitorTvShowHandler implements HandlerInterface
// Dispatch Episode commands for each missing Episode // Dispatch Episode commands for each missing Episode
foreach ($episodesInShow as $episode) { foreach ($episodesInShow as $episode) {
// Only monitor future episodes // Only monitor future episodes
$this->logger->info('> [MonitorTvShowHandler] Evaluating "' . $monitor->getTitle() . '", season "' . $episode['season_number'] . '" episode "' . $episode['episode_number'] . '"');
$episodeInFuture = $this->episodeReleasedAfterMonitorCreated($monitor->getCreatedAt(), $episode); $episodeInFuture = $this->episodeReleasedAfterMonitorCreated($monitor->getCreatedAt(), $episode);
$this->logger->info('> [MonitorTvShowHandler] Episode released after monitor started for season ' . $episode['season_number'] . ' episode ' . $episode['episode_number'] . ' for title: ' . $monitor->getTitle() . ' ? ' . (true === $episodeInFuture ? 'YES' : 'NO')); $this->logger->info('> [MonitorTvShowHandler] ...Released after monitor started? ' . (true === $episodeInFuture ? 'YES' : 'NO'));
if (false === $episodeInFuture) { if (false === $episodeInFuture) {
$this->logger->info('> [MonitorTvShowHandler] Episode released after monitor started for title: ' . 'for season ' . $episode['season_number'] . ' episode ' . $episode['episode_number'] . ', skipping'); $this->logger->info('> [MonitorTvShowHandler] ...Skipping');
continue; continue;
} }
// Check if the episode is already downloaded // Check if the episode is already downloaded
$episodeExists = $this->episodeExists($episode, $downloadedEpisodes); $episodeExists = $this->episodeExists($episode, $downloadedEpisodes);
$this->logger->info('> [MonitorTvShowHandler] Episode exists for season ' . $episode['season_number'] . ' episode ' . $episode['episode_number'] . ' for title: ' . $monitor->getTitle() . ' ? ' . (true === $episodeExists ? 'YES' : 'NO')); $this->logger->info('> [MonitorTvShowHandler] ...Episode exists? ' . (true === $episodeExists ? 'YES' : 'NO'));
if (true === $episodeExists) { if (true === $episodeExists) {
$this->logger->info('> [MonitorTvShowHandler] Episode exists for title: ' . $monitor->getTitle() . ', skipping'); $this->logger->info('> [MonitorTvShowHandler] ...Skipping');
continue; continue;
} }
// Check for existing monitors // Check for existing monitors
$monitorExists = $this->monitorExists($monitor, $episode); $monitorExists = $this->monitorExists($monitor, $episode);
$this->logger->info('> [MonitorTvShowHandler] Monitor exists for season ' . $episode['season_number'] . ' episode ' . $episode['episode_number'] . ' for title: ' . $monitor->getTitle() . ' ? ' . (true === $monitorExists ? 'YES' : 'NO')); $this->logger->info('> [MonitorTvShowHandler] ...Monitor exists? ' . (true === $monitorExists ? 'YES' : 'NO'));
if (true === $monitorExists) { if (true === $monitorExists) {
$this->logger->info('> [MonitorTvShowHandler] Monitor exists for title: ' . $monitor->getTitle() . ', skipping'); $this->logger->info('> [MonitorTvShowHandler] ...Skipping');
continue; continue;
} }
@@ -106,7 +107,7 @@ readonly class MonitorTvShowHandler implements HandlerInterface
// Immediately run the monitor // Immediately run the monitor
$command = new MonitorTvEpisodeCommand($episodeMonitor->getId()); $command = new MonitorTvEpisodeCommand($episodeMonitor->getId());
$this->monitorTvEpisodeHandler->handle($command); $this->monitorTvEpisodeHandler->handle($command);
$this->logger->info('> [MonitorTvShowHandler] Dispatching MonitorTvEpisodeCommand for season ' . $episodeMonitor->getSeason() . ' episode ' . $episodeMonitor->getEpisode() . ' for title: ' . $monitor->getTitle()); $this->logger->info('> [MonitorTvShowHandler] ...Dispatching MonitorTvEpisodeCommand');
} }
} }

View File

@@ -0,0 +1,15 @@
<?php
namespace App\User\Dto;
class UserPreferences
{
public function __construct(
public readonly string $resolution,
public readonly string $codec,
public readonly string $language,
public readonly string $provider,
public readonly string $quality,
) {}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\User\Dto;
use App\User\Framework\Entity\User;
class UserPreferencesFactory
{
public static function createFromUser(User $user): UserPreferences
{
return new UserPreferences(
resolution: $user->getUserPreference('resolution')->getPreferenceValue(),
codec: $user->getUserPreference('codec')->getPreferenceValue(),
language: $user->getUserPreference('language')->getPreferenceValue(),
provider: $user->getUserPreference('provider')->getPreferenceValue(),
quality: $user->getUserPreference('quality')->getPreferenceValue(),
);
}
}

View File

@@ -98,7 +98,8 @@ class PreferencesController extends AbstractController
[ [
'preferences' => $this->preferencesRepository->findEnabled(), 'preferences' => $this->preferencesRepository->findEnabled(),
'languages' => $languages, 'languages' => $languages,
'providers' => ProviderList::$providers, 'providers' => ProviderList::getProviders(),
'qualities' => QualityList::getBaseQualities(),
'mediaPreferences' => $mediaPreferences, 'mediaPreferences' => $mediaPreferences,
'downloadPreferences' => $downloadPreferences, 'downloadPreferences' => $downloadPreferences,
] ]

View File

@@ -153,13 +153,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
return $this->userPreferences; return $this->userPreferences;
} }
public function getUserPreference(string $preferenceName) public function getUserPreference(string $preferenceName): ?UserPreference
{ {
foreach ($this->userPreferences as $userPreference) { foreach ($this->userPreferences as $userPreference) {
if ($userPreference->getPreference()->getName() === $preferenceName) { if ($userPreference->getPreference()->getName() === $preferenceName) {
return $userPreference->getPreference(); return $userPreference;
} }
} }
return null;
} }
public function hasUserPreference(string $preferenceName): bool public function hasUserPreference(string $preferenceName): bool

View File

@@ -2,6 +2,8 @@
namespace App\User\Framework\EventListener; namespace App\User\Framework\EventListener;
use App\Base\ConfigResolver;
use App\Base\Util\Broadcaster;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent; use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
@@ -10,12 +12,26 @@ final class LoginSuccessListener
{ {
public function __construct( public function __construct(
private readonly RequestStack $requestStack, private readonly RequestStack $requestStack,
private readonly ConfigResolver $configResolver,
private readonly Broadcaster $broadcaster,
) {} ) {}
#[AsEventListener(event: 'security.authentication.success')] #[AsEventListener(event: 'security.authentication.success', priority: 20)]
public function setMercureTopics(AuthenticationSuccessEvent $event): void public function setMercureTopics(AuthenticationSuccessEvent $event): void
{ {
// Set the unique Mercure topic name for the User's alerts // Set the unique Mercure topic name for the User's alerts
$this->requestStack->getSession()->set('mercure_alert_topic', 'alerts_' . uniqid()); $this->requestStack->getSession()->set('mercure_alert_topic', 'alerts_' . uniqid());
} }
#[AsEventListener(event: 'security.authentication.success', priority: 10)]
public function validateConfig(AuthenticationSuccessEvent $event): void
{
// Set the unique Mercure topic name for the User's alerts
$valid = $this->configResolver->validate();
if (false === $valid) {
foreach ($this->configResolver->getMessages() as $message) {
$this->requestStack->getSession()->getFlashBag()->add('warning', $message);
}
}
}
} }

View File

@@ -7,20 +7,9 @@
<path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5ZM9.5 4a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3ZM12 15H8a1 1 0 0 1 0-2h1v-3H8a1 1 0 0 1 0-2h2a1 1 0 0 1 1 1v4h1a1 1 0 0 1 0 2Z"/> <path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5ZM9.5 4a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3ZM12 15H8a1 1 0 0 1 0-2h1v-3H8a1 1 0 0 1 0-2h2a1 1 0 0 1 1 1v4h1a1 1 0 0 1 0 2Z"/>
</svg> </svg>
<span class="sr-only">Info</span> <span class="sr-only">Info</span>
<h3 class="text-lg font-medium">{{ title|default('') }}</h3> <h3 class="text-lg font-medium font-bold">{{ title|default('') }}</h3>
</div> </div>
<div class="mt-2 text-sm"> <div class="mt-2 text-sm w-[350px] font-bold">
{{ message }} {{ message }}
</div> </div>
{# <div class="flex">#}
{# <button type="button" class="text-white bg-green-800 hover:bg-green-900 focus:ring-4 focus:outline-none focus:ring-green-300 font-medium rounded-lg text-xs px-3 py-1.5 me-2 text-center inline-flex items-center dark:bg-green-600 dark:hover:bg-green-700 dark:focus:ring-green-800">#}
{# <svg class="me-2 h-3 w-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 14">#}
{# <path d="M10 0C4.612 0 0 5.336 0 7c0 1.742 3.546 7 10 7 6.454 0 10-5.258 10-7 0-1.664-4.612-7-10-7Zm0 10a3 3 0 1 1 0-6 3 3 0 0 1 0 6Z"/>#}
{# </svg>#}
{# View more#}
{# </button>#}
{# <button type="button" class="text-green-800 bg-transparent border border-green-800 hover:bg-green-900 hover:text-white focus:ring-4 focus:outline-none focus:ring-green-300 font-medium rounded-lg text-xs px-3 py-1.5 text-center dark:hover:bg-green-600 dark:border-green-600 dark:text-green-400 dark:hover:text-white dark:focus:ring-green-800" data-dismiss-target="#alert-additional-content-3" aria-label="Close">#}
{# Dismiss#}
{# </button>#}
{# </div>#}
</li> </li>

View File

@@ -28,7 +28,10 @@
</div> </div>
<div {{ turbo_stream_listen(app.session.get('mercure_alert_topic')) }} class="fixed z-40 top-10 right-10"> <div {{ turbo_stream_listen(app.session.get('mercure_alert_topic')) }} class="fixed z-40 top-10 right-10">
<div class="z-40"> <div class="z-40">
<ul id="alert_list"> <ul id="alert_list" class="flex flex-col gap-2">
{% for message in app.flashes('warning') %}
<twig:Alert :title="'Warning'" :message="message" :alert_id="''" type="warning" data-controller="alert" />
{% endfor %}
</ul> </ul>
</div> </div>
</div> </div>

View File

@@ -37,7 +37,7 @@
</button> </button>
<small class="py-1 px-1.5 mr-1 grow-0 font-bold bg-gray-700 rounded-lg font-normal text-white" title="Air date {{ episode['name'] }}"> <small class="py-1 px-1.5 mr-1 grow-0 font-bold bg-gray-700 rounded-lg font-normal text-white" title="Air date {{ episode['name'] }}">
{{ episode['air_date']|date }} {{ episode['air_date']|date(null, 'UTC') }}
</small> </small>
</div> </div>
</div> </div>