wip sync cast
This commit is contained in:
68
src/Command/SyncCastsCommand.php
Normal file
68
src/Command/SyncCastsCommand.php
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
<?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-casts')]
|
||||||
|
readonly class SyncCastsCommand
|
||||||
|
{
|
||||||
|
public function __construct (
|
||||||
|
private readonly SerializerInterface $serializer,
|
||||||
|
private readonly TMDBGateway $TMDBGateway,
|
||||||
|
private readonly EntityManagerInterface $em,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function __invoke(OutputInterface $output): int
|
||||||
|
{
|
||||||
|
foreach ($this->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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,135 +0,0 @@
|
|||||||
<?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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user