sync actor roles
This commit is contained in:
1
.idea/ltbxd-actorle.iml
generated
1
.idea/ltbxd-actorle.iml
generated
@@ -129,6 +129,7 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/twig/extra-bundle" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/twig/twig" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/webmozart/assert" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/var/cache" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
|
||||
39
migrations/Version20260115201004.php
Normal file
39
migrations/Version20260115201004.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
final class Version20260115201004 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
// this up() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('CREATE TABLE actor (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, name VARCHAR(255) NOT NULL, popularity DOUBLE PRECISION DEFAULT NULL, tmdb_id INT DEFAULT NULL, PRIMARY KEY (id))');
|
||||
$this->addSql('CREATE TABLE movie_role (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, character VARCHAR(255) NOT NULL, actor_id INT DEFAULT NULL, movie_id INT DEFAULT NULL, PRIMARY KEY (id))');
|
||||
$this->addSql('CREATE INDEX IDX_A40FAB6710DAF24A ON movie_role (actor_id)');
|
||||
$this->addSql('CREATE INDEX IDX_A40FAB678F93B6FC ON movie_role (movie_id)');
|
||||
$this->addSql('ALTER TABLE movie_role ADD CONSTRAINT FK_A40FAB6710DAF24A FOREIGN KEY (actor_id) REFERENCES actor (id)');
|
||||
$this->addSql('ALTER TABLE movie_role ADD CONSTRAINT FK_A40FAB678F93B6FC FOREIGN KEY (movie_id) REFERENCES movie (id)');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('ALTER TABLE movie_role DROP CONSTRAINT FK_A40FAB6710DAF24A');
|
||||
$this->addSql('ALTER TABLE movie_role DROP CONSTRAINT FK_A40FAB678F93B6FC');
|
||||
$this->addSql('DROP TABLE actor');
|
||||
$this->addSql('DROP TABLE movie_role');
|
||||
}
|
||||
}
|
||||
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