Skip to content

Commit ab4cc80

Browse files
committed
Update movie sets in update-data CLI command
1 parent eacc2f7 commit ab4cc80

8 files changed

Lines changed: 98 additions & 3 deletions

File tree

config/defaults.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ parameters:
99
tvdb.shows.language: eng
1010
scrobbler.use-queue: false
1111
show-fetch-interval: 86400
12+
movie-set-fetch-interval: 86400
1213
image-fetcher.storage-path: "%kernel.cache_dir%/images"
1314
image-fetcher.download-all-on-update: true
1415
user.enable-register: true

config/services.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ services:
2424
tracky\console\UpdateDataCommand:
2525
arguments:
2626
$showFetchInterval: "%show-fetch-interval%"
27+
$movieSetFetchInterval: "%movie-set-fetch-interval%"
2728
$downloadAllImages: "%image-fetcher.download-all-on-update%"
2829
tracky\controller\UserController:
2930
arguments:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace DoctrineMigrations;
3+
4+
use Doctrine\DBAL\Schema\Schema;
5+
use Doctrine\Migrations\AbstractMigration;
6+
7+
final class Version20260715190056 extends AbstractMigration
8+
{
9+
public function getDescription(): string
10+
{
11+
return "Add lastupdate column to moviesets";
12+
}
13+
14+
public function up(Schema $schema): void
15+
{
16+
$this->addSql("ALTER TABLE moviesets ADD lastUpdate DATETIME DEFAULT NULL");
17+
}
18+
19+
public function down(Schema $schema): void
20+
{
21+
$this->addSql("ALTER TABLE moviesets DROP lastUpdate");
22+
}
23+
}

src/main/php/tracky/console/FetchDataCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
120120
if ($this->downloadAllImages) {
121121
$output->writeln(sprintf("Fetching images for movie set %d (%s)", $id, $movieSet->getTitle()));
122122

123-
$movieSet->fetchPosterImage($this->imageFetcher);
123+
$movieSet->fetchPosterImages($this->imageFetcher, true);
124124
}
125125
break;
126126
default:

src/main/php/tracky/console/UpdateDataCommand.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@
99
use Symfony\Component\Console\Output\OutputInterface;
1010
use tracky\dataprovider\Helper;
1111
use tracky\ImageFetcher;
12+
use tracky\model\MovieSet;
1213
use tracky\model\Show;
14+
use tracky\orm\MovieSetRepository;
1315
use tracky\orm\ShowRepository;
1416

15-
#[AsCommand(name: "update-data", description: "Fetch new episodes and seasons for all shows needing an update")]
17+
#[AsCommand(name: "update-data", description: "Fetch updates of shows and movie sets needing an update")]
1618
class UpdateDataCommand extends Command
1719
{
1820
public function __construct(
1921
private readonly Helper $dataProviderHelper,
2022
private readonly ShowRepository $showRepository,
23+
private readonly MovieSetRepository $movieSetRepository,
2124
private readonly ImageFetcher $imageFetcher,
2225
private readonly EntityManagerInterface $entityManager,
2326
private readonly int $showFetchInterval,
27+
private readonly int $movieSetFetchInterval,
2428
private readonly bool $downloadAllImages
2529
)
2630
{
@@ -29,7 +33,7 @@ public function __construct(
2933

3034
protected function configure(): void
3135
{
32-
$this->addOption("force", "f", InputOption::VALUE_NONE, "Update all shows even if they don't need an update");
36+
$this->addOption("force", "f", InputOption::VALUE_NONE, "Update all shows and movie sets even if they don't need an update");
3337
}
3438

3539
public function execute(InputInterface $input, OutputInterface $output): int
@@ -55,6 +59,27 @@ public function execute(InputInterface $input, OutputInterface $output): int
5559
}
5660
}
5761

62+
/**
63+
* @var MovieSet
64+
*/
65+
foreach ($this->movieSetRepository->findAll() as $movieSet) {
66+
if (!$input->getOption("force") and !$movieSet->needsUpdate($this->movieSetFetchInterval)) {
67+
continue;
68+
}
69+
70+
$output->writeln(sprintf("Fetching data for movie set: %s", $movieSet->getTitle()));
71+
72+
$dataProvider = $this->dataProviderHelper->getProviderByEntry($movieSet);
73+
$dataProvider->fetchMovieSet($movieSet);
74+
75+
$this->entityManager->persist($movieSet);
76+
$this->entityManager->flush();
77+
78+
if ($this->downloadAllImages) {
79+
$movieSet->fetchPosterImages($this->imageFetcher, true);
80+
}
81+
}
82+
5883
return self::SUCCESS;
5984
}
6085
}

src/main/php/tracky/dataprovider/TMDB.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ public function fetchMovieSet(MovieSet $movieSet): bool
288288
$this->fetchMovie($movie);
289289
}
290290

291+
$movieSet->setLastUpdate(new DateTime);
292+
291293
return true;
292294
}
293295

src/main/php/tracky/dataprovider/TVDB.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ public function fetchMovieSet(MovieSet $movieSet): bool
403403
$movieSet->setPlot($translationData["overview"] ?? null);
404404
}
405405

406+
$movieSet->setLastUpdate(new DateTime);
407+
406408
return true;
407409
}
408410

src/main/php/tracky/model/MovieSet.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
namespace tracky\model;
33

44
use Doctrine\ORM\Mapping as ORM;
5+
use tracky\datetime\DateTime;
6+
use tracky\ImageFetcher;
57
use tracky\model\traits\DataProvider;
68
use tracky\model\traits\Plot;
79
use tracky\model\traits\PosterImage;
@@ -18,6 +20,9 @@ class MovieSet extends BaseEntity
1820
#[ORM\Column(type: "string")]
1921
private string $title;
2022

23+
#[ORM\Column(name: "lastUpdate", type: "datetime", nullable: true)]
24+
private ?DateTime $lastUpdate;
25+
2126
#[ORM\OneToMany(mappedBy: "movieSet", targetEntity: Movie::class, cascade: ["persist"])]
2227
#[ORM\OrderBy(["year" => "ASC"])]
2328
private mixed $movies = [];
@@ -33,6 +38,31 @@ public function setTitle(string $title): MovieSet
3338
return $this;
3439
}
3540

41+
public function setLastUpdate(?DateTime $lastUpdate): self
42+
{
43+
$this->lastUpdate = $lastUpdate;
44+
return $this;
45+
}
46+
47+
public function getLastUpdate(): ?DateTime
48+
{
49+
return $this->lastUpdate;
50+
}
51+
52+
public function needsUpdate(int $maxAge): bool
53+
{
54+
// Update if never updated before
55+
if ($this->getLastUpdate() === null) {
56+
return true;
57+
}
58+
59+
// Update if max age passed
60+
$now = new DateTime;
61+
$diff = $now->getTimestamp() - $this->getLastUpdate()->getTimestamp();
62+
63+
return $diff >= $maxAge;
64+
}
65+
3666
/**
3767
* @return Movie[]
3868
*/
@@ -67,4 +97,15 @@ public function getRuntime(): int
6797

6898
return $runtime;
6999
}
100+
101+
public function fetchPosterImages(ImageFetcher $imageFetcher, bool $includeMovies): void
102+
{
103+
$this->fetchPosterImage($imageFetcher);
104+
105+
if ($includeMovies) {
106+
foreach ($this->getMovies() as $movie) {
107+
$movie->fetchPosterImage($imageFetcher);
108+
}
109+
}
110+
}
70111
}

0 commit comments

Comments
 (0)