sync actor roles
This commit is contained in:
135
src/Command/SyncDataCommand.php
Normal file
135
src/Command/SyncDataCommand.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\Actor;
|
||||
use App\Entity\Movie;
|
||||
use App\Entity\MovieRole;
|
||||
use App\Exception\GatewayException;
|
||||
use App\Gateway\TMDBGateway;
|
||||
use App\Model\Ltbxd\LtbxdMovie;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Attribute\Option;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Serializer\Exception\ExceptionInterface;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
|
||||
#[AsCommand('app:sync-data')]
|
||||
class SyncDataCommand
|
||||
{
|
||||
public function __construct (
|
||||
private readonly SerializerInterface $serializer,
|
||||
private readonly TMDBGateway $TMDBGateway,
|
||||
private readonly EntityManagerInterface $em,
|
||||
) {}
|
||||
|
||||
public function __invoke(
|
||||
OutputInterface $output,
|
||||
#[Option(name: 'skip-films-sync', shortcut: 'f')]
|
||||
bool $skipFilmsSync = false,
|
||||
): int
|
||||
{
|
||||
try {
|
||||
$films = $this->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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\Movie;
|
||||
use App\Gateway\TMDBGateway;
|
||||
use App\Model\Ltbxd\LtbxdMovie;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Serializer\Exception\ExceptionInterface;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
|
||||
#[AsCommand('app:sync-movies')]
|
||||
class SyncMoviesCommand
|
||||
{
|
||||
public function __construct (
|
||||
private readonly SerializerInterface $serializer,
|
||||
private readonly TMDBGateway $TMDBGateway,
|
||||
private readonly EntityManagerInterface $em,
|
||||
) {}
|
||||
|
||||
public function __invoke(OutputInterface $output): int
|
||||
{
|
||||
$file = file_get_contents('public/files/watched.csv');
|
||||
try {
|
||||
$ltbxdMovies = $this->serializer->deserialize($file, LtbxdMovie::class.'[]', 'csv');
|
||||
} catch (ExceptionInterface $e) {
|
||||
$output->writeln($e->getMessage());
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$i = 0;
|
||||
/** @var LtbxdMovie $ltbxdMovie */
|
||||
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
|
||||
$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);
|
||||
++$i;
|
||||
}
|
||||
|
||||
if (0 === $i % 50) {
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,9 @@ class Actor
|
||||
#[ORM\OneToMany(targetEntity: MovieRole::class, mappedBy: 'actor')]
|
||||
private Collection $movieRoles;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
private ?int $tmdbId = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->movieRoles = new ArrayCollection();
|
||||
@@ -90,4 +93,16 @@ class Actor
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTmdbId(): ?int
|
||||
{
|
||||
return $this->tmdbId;
|
||||
}
|
||||
|
||||
public function setTmdbId(?int $tmdbId): static
|
||||
{
|
||||
$this->tmdbId = $tmdbId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user