diff --git a/config/parameters.yml b/config/parameters.yml index 8ba57f5..24d5e21 100644 --- a/config/parameters.yml +++ b/config/parameters.yml @@ -7,3 +7,5 @@ parameters: postgres_password: "pwd" tmdb_host: "https://api.themoviedb.org/3" + + ltbxd_watched_file: "%kernel.project_dir%/public/files/ltbxd/watched.csv" diff --git a/public/files/watched.csv b/public/files/ltbxd/watched.csv similarity index 100% rename from public/files/watched.csv rename to public/files/ltbxd/watched.csv diff --git a/src/Command/SyncFilmsCommands.php b/src/Command/SyncFilmsCommands.php new file mode 100644 index 0000000..4b5b250 --- /dev/null +++ b/src/Command/SyncFilmsCommands.php @@ -0,0 +1,72 @@ +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; + } +} diff --git a/src/Gateway/LtbxdGateway.php b/src/Gateway/LtbxdGateway.php new file mode 100644 index 0000000..c64f5d2 --- /dev/null +++ b/src/Gateway/LtbxdGateway.php @@ -0,0 +1,37 @@ +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); + } + } +} diff --git a/src/Gateway/TMDBGateway.php b/src/Gateway/TMDBGateway.php index d2576a8..28bf041 100644 --- a/src/Gateway/TMDBGateway.php +++ b/src/Gateway/TMDBGateway.php @@ -8,24 +8,21 @@ use App\Exception\GatewayException; use App\Model\TMDB\TMDBMovie; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Serializer\SerializerInterface; -use Symfony\Contracts\Cache\CacheInterface; -use Symfony\Contracts\Cache\ItemInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; 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 MOVIE_CREDITS_URI = '/movie/{id}/credits'; public function __construct( - private readonly HttpClientInterface $client, - private readonly SerializerInterface $serializer, + private HttpClientInterface $client, + private SerializerInterface $serializer, #[Autowire('%env(TMDB_API_TOKEN)%')] - private readonly string $apiToken, + private string $apiToken, #[Autowire('%tmdb_host%')] - private readonly string $host, + private string $host, ) { }