-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from leepeuker/improve-trakt-import
Improve logging and refactor trakt import
- Loading branch information
Showing
7 changed files
with
179 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
db/migrations/sqlite/20221228172042_AddPrimaryIndexToMovieUserWatchDatesTable.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use Phinx\Migration\AbstractMigration; | ||
|
||
final class AddPrimaryIndexToMovieUserWatchDatesTable extends AbstractMigration | ||
{ | ||
public function down() : void | ||
{ | ||
$this->execute( | ||
<<<SQL | ||
CREATE TABLE `tmp_movie_user_watch_dates` ( | ||
`movie_id` INTEGER NOT NULL, | ||
`user_id` INTEGER NOT NULL, | ||
`watched_at` TEXT DEFAULT NULL, | ||
`plays` INTEGER DEFAULT 1, | ||
FOREIGN KEY (`movie_id`) REFERENCES movie (`id`) ON DELETE CASCADE, | ||
FOREIGN KEY (`user_id`) REFERENCES user (`id`) ON DELETE CASCADE | ||
) | ||
SQL, | ||
); | ||
$this->execute('INSERT INTO `tmp_movie_user_watch_dates` (movie_id, user_id, watched_at, plays) SELECT * FROM movie_user_watch_dates'); | ||
$this->execute('DROP TABLE `movie_user_watch_dates`'); | ||
$this->execute('ALTER TABLE `tmp_movie_user_watch_dates` RENAME TO `movie_user_watch_dates`'); | ||
} | ||
|
||
public function up() : void | ||
{ | ||
$this->execute( | ||
<<<SQL | ||
CREATE TABLE `tmp_movie_user_watch_dates` ( | ||
`movie_id` INTEGER NOT NULL, | ||
`user_id` INTEGER NOT NULL, | ||
`watched_at` TEXT DEFAULT NULL, | ||
`plays` INTEGER DEFAULT 1, | ||
FOREIGN KEY (`movie_id`) REFERENCES movie (`id`) ON DELETE CASCADE, | ||
FOREIGN KEY (`user_id`) REFERENCES user (`id`) ON DELETE CASCADE, | ||
PRIMARY KEY (`movie_id`, `user_id`, `watched_at`) | ||
) | ||
SQL, | ||
); | ||
$this->execute('REPLACE INTO `tmp_movie_user_watch_dates` (movie_id, user_id, watched_at, plays) SELECT * FROM movie_user_watch_dates'); | ||
$this->execute('DROP TABLE `movie_user_watch_dates`'); | ||
$this->execute('ALTER TABLE `tmp_movie_user_watch_dates` RENAME TO `movie_user_watch_dates`'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Movary\Service\Trakt; | ||
|
||
use Movary\Api\Trakt\ValueObject\TraktMovie; | ||
use Movary\Domain\Movie\MovieApi; | ||
use Movary\Domain\Movie\MovieEntity; | ||
use Movary\Service\Tmdb\SyncMovie; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class MovieImporter | ||
{ | ||
public function __construct( | ||
private readonly MovieApi $movieApi, | ||
private readonly LoggerInterface $logger, | ||
private readonly SyncMovie $tmdbMovieSync, | ||
) { | ||
} | ||
|
||
public function importMovie(TraktMovie $traktMovie) : MovieEntity | ||
{ | ||
$traktId = $traktMovie->getTraktId(); | ||
$tmdbId = $traktMovie->getTmdbId(); | ||
|
||
$movie = $this->movieApi->findByTraktId($traktId); | ||
|
||
if ($movie !== null) { | ||
return $movie; | ||
} | ||
|
||
$movie = $this->movieApi->findByTmdbId($tmdbId); | ||
|
||
if ($movie !== null) { | ||
$this->movieApi->updateTraktId($movie->getId(), $traktId); | ||
|
||
return $this->movieApi->fetchByTraktId($traktId); | ||
} | ||
|
||
$movie = $this->tmdbMovieSync->syncMovie($tmdbId); | ||
$this->movieApi->updateTraktId($movie->getId(), $traktId); | ||
|
||
$this->logger->info('Trakt history import: Added new movie: ' . $movie->getTitle()); | ||
|
||
return $this->movieApi->fetchByTraktId($traktId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters