enhance sync films
This commit is contained in:
@@ -7,3 +7,5 @@ parameters:
|
|||||||
postgres_password: "pwd"
|
postgres_password: "pwd"
|
||||||
|
|
||||||
tmdb_host: "https://api.themoviedb.org/3"
|
tmdb_host: "https://api.themoviedb.org/3"
|
||||||
|
|
||||||
|
ltbxd_watched_file: "%kernel.project_dir%/public/files/ltbxd/watched.csv"
|
||||||
|
|||||||
72
src/Command/SyncFilmsCommands.php
Normal file
72
src/Command/SyncFilmsCommands.php
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Command;
|
||||||
|
|
||||||
|
use App\Entity\Movie;
|
||||||
|
use App\Exception\GatewayException;
|
||||||
|
use App\Gateway\LtbxdGateway;
|
||||||
|
use App\Gateway\TMDBGateway;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
|
#[AsCommand('app:sync-films')]
|
||||||
|
readonly class SyncFilmsCommands
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private LtbxdGateway $ltbxdGateway,
|
||||||
|
private TMDBGateway $TMDBGateway,
|
||||||
|
private EntityManagerInterface $em,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function __invoke(OutputInterface $output): int
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$ltbxdMovies = $this->ltbxdGateway->parseFile();
|
||||||
|
} catch (GatewayException $e) {
|
||||||
|
$output->writeln('/!\ '.$e->getMessage());
|
||||||
|
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
$i = 0;
|
||||||
|
foreach ($ltbxdMovies as $ltbxdMovie) {
|
||||||
|
// If the movie already exists, skip
|
||||||
|
if (0 < $this->em->getRepository(Movie::class)->count(['ltbxdRef' => $ltbxdMovie->getLtbxdRef()])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search movie on TMDB
|
||||||
|
try {
|
||||||
|
$film = $this->TMDBGateway->searchMovie($ltbxdMovie->getName());
|
||||||
|
} catch (GatewayException $e) {
|
||||||
|
$output->writeln('/!\ '.$e->getMessage());
|
||||||
|
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($film) {
|
||||||
|
$output->writeln('* Found '.$ltbxdMovie->getName());
|
||||||
|
|
||||||
|
$filmEntity = new Movie()
|
||||||
|
->setLtbxdRef($ltbxdMovie->getLtbxdRef())
|
||||||
|
->setTitle($ltbxdMovie->getName())
|
||||||
|
->setTmdbId($film->getId())
|
||||||
|
;
|
||||||
|
$this->em->persist($filmEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
++$i;
|
||||||
|
if (0 === $i % 50) {
|
||||||
|
$this->em->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
$output->writeln('Films synced');
|
||||||
|
|
||||||
|
return Command::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
37
src/Gateway/LtbxdGateway.php
Normal file
37
src/Gateway/LtbxdGateway.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Gateway;
|
||||||
|
|
||||||
|
use App\Exception\GatewayException;
|
||||||
|
use App\Model\Ltbxd\LtbxdMovie;
|
||||||
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
|
use Symfony\Component\Serializer\Exception\ExceptionInterface;
|
||||||
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
|
|
||||||
|
readonly class LtbxdGateway
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private SerializerInterface $serializer,
|
||||||
|
#[Autowire('%ltbxd_watched_file%')]
|
||||||
|
private string $fileDir,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return LtbxdMovie[]
|
||||||
|
* @throws GatewayException
|
||||||
|
*/
|
||||||
|
public function parseFile(): array
|
||||||
|
{
|
||||||
|
if (!file_exists($this->fileDir)) {
|
||||||
|
throw new GatewayException(sprintf('Could not find file %s', $this->fileDir));
|
||||||
|
}
|
||||||
|
|
||||||
|
$fileContent = file_get_contents($this->fileDir);
|
||||||
|
|
||||||
|
try {
|
||||||
|
return $this->serializer->deserialize($fileContent, LtbxdMovie::class.'[]', 'csv');
|
||||||
|
} catch (ExceptionInterface $e) {
|
||||||
|
throw new GatewayException('Error while deserializing Letterboxd data', previous: $e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,24 +8,21 @@ use App\Exception\GatewayException;
|
|||||||
use App\Model\TMDB\TMDBMovie;
|
use App\Model\TMDB\TMDBMovie;
|
||||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
use Symfony\Component\Serializer\SerializerInterface;
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
use Symfony\Contracts\Cache\CacheInterface;
|
|
||||||
use Symfony\Contracts\Cache\ItemInterface;
|
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||||
use function Symfony\Component\String\u;
|
|
||||||
|
|
||||||
class TMDBGateway
|
readonly class TMDBGateway
|
||||||
{
|
{
|
||||||
private const string SEARCH_URI = '/search/movie';
|
private const string SEARCH_URI = '/search/movie';
|
||||||
private const string MOVIE_CREDITS_URI = '/movie/{id}/credits';
|
private const string MOVIE_CREDITS_URI = '/movie/{id}/credits';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly HttpClientInterface $client,
|
private HttpClientInterface $client,
|
||||||
private readonly SerializerInterface $serializer,
|
private SerializerInterface $serializer,
|
||||||
#[Autowire('%env(TMDB_API_TOKEN)%')]
|
#[Autowire('%env(TMDB_API_TOKEN)%')]
|
||||||
private readonly string $apiToken,
|
private string $apiToken,
|
||||||
#[Autowire('%tmdb_host%')]
|
#[Autowire('%tmdb_host%')]
|
||||||
private readonly string $host,
|
private string $host,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user