diff --git a/src/Command/SyncCastsCommand.php b/src/Command/SyncCastsCommand.php new file mode 100644 index 0000000..b4b9864 --- /dev/null +++ b/src/Command/SyncCastsCommand.php @@ -0,0 +1,68 @@ +em->getRepository(Movie::class)->findAll() as $film) { + try { + $creditsContext = $this->TMDBGateway->getMovieCredits($film->getTmdbId()); + } catch (GatewayException $e) { + $output->writeln('/!\ '.$e->getMessage()); + continue; + } + + if (!empty($creditsContext->cast)) { + $output->writeln('Syncing cast for '.$film->getTitle()); + } + foreach ($creditsContext->cast as $actorModel) { + // Get existing or create new + $actor = $this->em->getRepository(Actor::class)->findOneBy(['tmdbId' => $actorModel->id]); + if (!$actor instanceof Actor) { + $output->writeln('* New actor found: '.$actorModel->name); + + $actor = new Actor() + ->setPopularity($actorModel->popularity) + ->setName($actorModel->name) + ->setTmdbId($actorModel->id) + ; + + $this->em->persist($actor); + } + + // Get or create the role + if (0 < $this->em->getRepository(MovieRole::class)->count(['actor' => $actor, 'movie' => $film])) { + $actor->addMovieRole(new MovieRole() + ->setMovie($film) + ->setCharacter($actorModel->character) + ); + } + } + + $this->em->flush(); + } + } +} diff --git a/src/Command/SyncDataCommand.php b/src/Command/SyncDataCommand.php deleted file mode 100644 index 41a257b..0000000 --- a/src/Command/SyncDataCommand.php +++ /dev/null @@ -1,135 +0,0 @@ -syncMovies($output); - } catch (\Exception $e) { - $output->writeln('/!\ '.$e->getMessage()); - $output->writeln('/!\ '.$e->getPrevious()->getMessage()); - - return Command::FAILURE; - } - - if (!$skipFilmsSync) { - $this->syncActors($output, $films); - } - - // awards, quotes, complete roles - - return Command::SUCCESS; - } - - /** - * @return Movie[] - * @throws \Exception - */ - private function syncMovies(OutputInterface $output): array - { - $file = file_get_contents('public/files/watched.csv'); - try { - $ltbxdMovies = $this->serializer->deserialize($file, LtbxdMovie::class.'[]', 'csv'); - } catch (ExceptionInterface $e) { - throw new \Exception('Error while deserializing Letterboxd data', previous: $e); - } - - $films = []; - /** @var LtbxdMovie $ltbxdMovie */ - foreach ($ltbxdMovies as $ltbxdMovie) { - // If the movie already exists, skip - if (($film = $this->em->getRepository(Movie::class)->findOneBy(['ltbxdRef' => $ltbxdMovie->getLtbxdRef()])) instanceof Movie) { - $films[] = $film; - continue; - } - - // Search movie on TMDB - $film = $this->TMDBGateway->searchMovie($ltbxdMovie->getName()); - if ($film) { - $output->writeln('* Found '.$ltbxdMovie->getName()); - - $filmEntity = new Movie() - ->setLtbxdRef($ltbxdMovie->getLtbxdRef()) - ->setTitle($ltbxdMovie->getName()) - ->setTmdbId($film->getId()) - ; - $this->em->persist($filmEntity); - $films[] = $film; - } - - if (0 === \count($films) % 50) { - $this->em->flush(); - } - } - - $this->em->flush(); - - return $films; - } - - private function syncActors(OutputInterface $output, array $films): void - { - foreach ($films as $film) { - try { - $creditsContext = $this->TMDBGateway->getMovieCredits($film->getTmdbId()); - } catch (GatewayException $e) { - $output->writeln('/!\ '.$e->getMessage()); - continue; - } - - foreach ($creditsContext->cast as $actorModel) { - // Get existing or create new - $actor = $this->em->getRepository(Actor::class)->findOneBy(['tmdbId' => $actorModel->id]); - if (!$actor instanceof Actor) { - $output->writeln('* New actor found: '.$actorModel->name); - - $actor = new Actor() - ->setPopularity($actorModel->popularity) - ->setName($actorModel->name) - ->setTmdbId($actorModel->id) - ; - - $this->em->persist($actor); - } - - // Get or create the role - if (0 < $this->em->getRepository(MovieRole::class)->count(['actor' => $actor, 'movie' => $film])) { - $actor->addMovieRole(new MovieRole() - ->setMovie($film) - ->setCharacter($actorModel->character) - ); - } - } - - $this->em->flush(); - } - } -}