Add db, sync movies command
This commit is contained in:
63
src/Command/SyncMoviesCommand.php
Normal file
63
src/Command/SyncMoviesCommand.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ namespace App\Controller;
|
||||
use App\Gateway\TMDBGateway;
|
||||
use App\Model\Ltbxd\LtbxdMovie;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpClient\Exception\ClientException;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
@@ -21,16 +20,17 @@ class HomepageController extends AbstractController
|
||||
#[Route('/')]
|
||||
public function index(SerializerInterface $serializer): Response
|
||||
{
|
||||
$file = file_get_contents('files/watched.csv');
|
||||
$ltbxdMovies = $this->serializer->deserialize($file, LtbxdMovie::class.'[]', 'csv');
|
||||
/** @var LtbxdMovie $ltbxdMovie */
|
||||
$films = [];
|
||||
foreach ($ltbxdMovies as $ltbxdMovie) {
|
||||
// Search movie on TMDB
|
||||
$searchResult = $this->TMDBGateway->searchMovie($ltbxdMovie->getName());
|
||||
$films[] = $searchResult->getResults()[0];
|
||||
}
|
||||
dd($films);
|
||||
// $file = file_get_contents('files/watched.csv');
|
||||
// $ltbxdMovies = $this->serializer->deserialize($file, LtbxdMovie::class.'[]', 'csv');
|
||||
// /** @var LtbxdMovie $ltbxdMovie */
|
||||
// $films = [];
|
||||
// foreach ($ltbxdMovies as $ltbxdMovie) {
|
||||
// // Search movie on TMDB
|
||||
// $film = $this->TMDBGateway->searchMovie($ltbxdMovie->getName());
|
||||
// if ($film) {
|
||||
// $films[] = $film;
|
||||
// }
|
||||
// }
|
||||
|
||||
return $this->render('homepage/index.html.twig');
|
||||
}
|
||||
|
||||
65
src/Entity/Movie.php
Normal file
65
src/Entity/Movie.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\MovieRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: MovieRepository::class)]
|
||||
class Movie
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column]
|
||||
private ?int $tmdbId = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
private ?string $ltbxdRef = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
private ?string $title = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getTmdbId(): ?int
|
||||
{
|
||||
return $this->tmdbId;
|
||||
}
|
||||
|
||||
public function setTmdbId(int $tmdbId): static
|
||||
{
|
||||
$this->tmdbId = $tmdbId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLtbxdRef(): ?string
|
||||
{
|
||||
return $this->ltbxdRef;
|
||||
}
|
||||
|
||||
public function setLtbxdRef(string $ltbxdRef): static
|
||||
{
|
||||
$this->ltbxdRef = $ltbxdRef;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTitle(): ?string
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function setTitle(string $title): static
|
||||
{
|
||||
$this->title = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Gateway;
|
||||
|
||||
use App\Context\TMDB\MovieSearchContext;
|
||||
use App\Model\TMDB\TMDBMovie;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
@@ -17,7 +18,6 @@ class TMDBGateway
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $client,
|
||||
private readonly SerializerInterface $serializer,
|
||||
private readonly CacheInterface $cache,
|
||||
#[Autowire('%env(TMDB_API_TOKEN)%')]
|
||||
private readonly string $apiToken,
|
||||
#[Autowire('%env(TMDB_HOST)%')]
|
||||
@@ -25,20 +25,20 @@ class TMDBGateway
|
||||
) {
|
||||
}
|
||||
|
||||
public function searchMovie(string $movieName): ?MovieSearchContext
|
||||
public function searchMovie(string $movieName): ?TMDBMovie
|
||||
{
|
||||
$cacheKey = 'tmdb_movie.'.u($movieName)->snake();
|
||||
|
||||
return $this->cache->get($cacheKey, function (ItemInterface $item) use ($movieName) {
|
||||
$url = $this->host.self::SEARCH_URI.'?'.http_build_query(['query' => $movieName]);
|
||||
try {
|
||||
$response = $this->client->request('GET', $url, ['headers' => $this->getHeaders()]);
|
||||
$result = $response->getContent();
|
||||
return $this->serializer->deserialize($result, MovieSearchContext::class, 'json');
|
||||
} catch (\Throwable) {
|
||||
$url = $this->host.self::SEARCH_URI.'?'.http_build_query(['query' => $movieName]);
|
||||
try {
|
||||
$response = $this->client->request('GET', $url, ['headers' => $this->getHeaders()]);
|
||||
$result = $response->getContent();
|
||||
$searchContext = $this->serializer->deserialize($result, MovieSearchContext::class, 'json');
|
||||
if (empty($searchResult = $searchContext->getResults())) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
return reset($searchResult);
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function getHeaders(): array
|
||||
|
||||
@@ -36,4 +36,9 @@ class LtbxdMovie
|
||||
{
|
||||
return $this->ltbxdUri;
|
||||
}
|
||||
|
||||
public function getLtbxdRef(): string
|
||||
{
|
||||
return basename($this->ltbxdUri);
|
||||
}
|
||||
}
|
||||
|
||||
43
src/Repository/MovieRepository.php
Normal file
43
src/Repository/MovieRepository.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Movie;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Movie>
|
||||
*/
|
||||
class MovieRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Movie::class);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return Movie[] Returns an array of Movie objects
|
||||
// */
|
||||
// public function findByExampleField($value): array
|
||||
// {
|
||||
// return $this->createQueryBuilder('m')
|
||||
// ->andWhere('m.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->orderBy('m.id', 'ASC')
|
||||
// ->setMaxResults(10)
|
||||
// ->getQuery()
|
||||
// ->getResult()
|
||||
// ;
|
||||
// }
|
||||
|
||||
// public function findOneBySomeField($value): ?Movie
|
||||
// {
|
||||
// return $this->createQueryBuilder('m')
|
||||
// ->andWhere('m.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->getQuery()
|
||||
// ->getOneOrNullResult()
|
||||
// ;
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user