Skip to content

Commit

Permalink
Added Jellyfin import
Browse files Browse the repository at this point in the history
  • Loading branch information
JVT038 committed Aug 5, 2023
1 parent f3f76d1 commit a6402a7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions bin/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
$application->add($container->get(Movary\Command\JellyfinCacheDelete::class));
$application->add($container->get(Movary\Command\JellyfinCacheRefresh::class));
$application->add($container->get(Movary\Command\JellyfinExport::class));
$application->add($container->get(Movary\Command\JellyfinImport::class));

$application->run();
38 changes: 38 additions & 0 deletions src/Api/Jellyfin/JellyfinApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Movary\Api\Jellyfin\Cache\JellyfinCache;
use Movary\Api\Jellyfin\Dto\JellyfinAccessToken;
use Movary\Api\Jellyfin\Dto\JellyfinAuthenticationData;
use Movary\Api\Jellyfin\Dto\JellyfinMovieDto;
use Movary\Api\Jellyfin\Dto\JellyfinMovieDtoList;
use Movary\Api\Jellyfin\Dto\JellyfinUser;
use Movary\Api\Jellyfin\Dto\JellyfinUserId;
use Movary\Api\Jellyfin\Exception\JellyfinInvalidAuthentication;
Expand Down Expand Up @@ -74,6 +76,42 @@ public function fetchJellyfinServerInfo(Url $jellyfinServerUrl, JellyfinAccessTo
return $this->jellyfinClient->get($url, jellyfinAccessToken: $jellyfinAccessToken);
}

public function fetchWatchedMovies(int $userId) : ?JellyfinMovieDtoList
{
$jellyfinAuthentication = $this->userApi->findJellyfinAuthentication($userId);
if ($jellyfinAuthentication === null) {
throw JellyfinInvalidAuthentication::create();
}

$relativeUrl = RelativeUrl::create('/Users/' . $jellyfinAuthentication->getUserId() . '/Items');
$url = $jellyfinAuthentication->getServerUrl()->appendRelativeUrl($relativeUrl);
$query = [
'IncludeItemTypes' => 'Movie',
'Filters' => 'isPlayed',
'hasTmdbId' => 'True',
'recursive' => 'True',
'fields' => 'ProviderIds'
];

$response = $this->jellyfinClient->get($url, $query, $jellyfinAuthentication->getAccessToken(), 5);
if($response === null) {
return null;
}

$watchedMoviesList = JellyfinMovieDtoList::create();
foreach($response['Items'] as $movie) {
$jellyfinMovie = JellyfinMovieDto::create(
(string)$jellyfinAuthentication->getUserId(),
$movie['Id'],
(int)$movie['ProviderIds']['Tmdb'],
true,
Date::createFromString($movie['UserData']['LastPlayedDate'])
);
$watchedMoviesList->add($jellyfinMovie);
}
return $watchedMoviesList;
}

public function fetchJellyfinServerInfoPublic(Url $jellyfinServerUrl) : ?array
{
$url = $jellyfinServerUrl->appendRelativeUrl(RelativeUrl::create('/system/info/public'));
Expand Down
5 changes: 2 additions & 3 deletions src/Command/JellyfinImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Movary\Domain\Movie\History\MovieHistoryApi;
use Movary\JobQueue\JobQueueApi;
use Movary\Service\Jellyfin\JellyfinMoviesExporter;
use Movary\Service\Jellyfin\JellyfinMoviesImporter;
use Movary\ValueObject\JobStatus;
use Psr\Log\LoggerInterface;
Expand All @@ -30,7 +29,7 @@ public function __construct(

protected function configure() : void
{
$this->setDescription('Import Movary watch dates as plays to Jellyfin.')
$this->setDescription('Import Movary watch dates as plays from Jellyfin.')
->addArgument(self::OPTION_NAME_USER_ID, InputArgument::REQUIRED, 'Id of user.');
}

Expand All @@ -43,7 +42,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
try {
$this->generateOutput($output, 'Importing movie plays from Jellyfin...');

$this->jellyfinMoviesImporter->importMoviesToJellyfin($userId);
$this->jellyfinMoviesImporter->importMoviesFromJellyfin($userId);
} catch (Throwable $t) {
$this->generateOutput($output, 'ERROR: Could not complete Jellyfin import.');
$this->logger->error('Could not complete Jellyfin import', ['exception' => $t]);
Expand Down
26 changes: 23 additions & 3 deletions src/Service/Jellyfin/JellyfinMoviesImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@

use Movary\Api\Jellyfin\JellyfinApi;
use Movary\Domain\Movie\History\MovieHistoryApi;
use Movary\Domain\Movie\MovieApi;
use Movary\JobQueue\JobEntity;
use Movary\Service\Tmdb\SyncMovie;
use Psr\Log\LoggerInterface;
use RuntimeException;

class JellyfinMoviesImporter
{
public function __construct(
private readonly MovieHistoryApi $movieHistoryApi,
private readonly MovieApi $movieApi,
private readonly SyncMovie $tmdbMovieSyncService,
private readonly JellyfinApi $jellyfinApi,
private readonly LoggerInterface $logger,
) {
}

Expand All @@ -22,11 +28,25 @@ public function executeJob(JobEntity $job) : void
throw new RuntimeException('Missing userId');
}

$this->importMoviesToJellyfin($userId);
$this->importMoviesFromJellyfin($userId);
}

public function importMoviesToJellyfin(int $userId)
public function importMoviesFromJellyfin(int $userId)
{
// TODO
$watchedMoviesList = $this->jellyfinApi->fetchWatchedMovies($userId);
foreach($watchedMoviesList as $watchedMovie) {
$movie = $this->movieApi->findByTmdbId($watchedMovie->getTmdbId());
if($movie === null) {
$movie = $this->tmdbMovieSyncService->syncMovie($watchedMovie->getTmdbId());
$this->logger->debug('Jellyfin: Missing movie created during import', ['movieId' => $movie->getId(), 'moveTitle' => $movie->getTitle()]);
}

$this->movieApi->increaseHistoryPlaysForMovieOnDate($movie->getId(), $userId, $watchedMovie->getLastWatchDate());
$this->logger->info('Jellyfin: Movie watch date imported', [
'movieId' => $movie->getId(),
'moveTitle' => $movie->getTitle(),
'watchDate' => $watchedMovie->getLastWatchDate()
]);
}
}
}

0 comments on commit a6402a7

Please sign in to comment.