From 70b6a7325a659a2ea4a013227ab3025a319bcab7 Mon Sep 17 00:00:00 2001 From: DoarMihai Date: Wed, 1 Jan 2025 19:24:46 +0200 Subject: [PATCH] [#8] some fixes on movie processing --- .../ItemProcessing/FetchItemDataCommand.php | 10 ++- .../MovieProcessingService.php | 74 ++++++++++++++++--- ...12_30_010725_create_external_ids_table.php | 12 +-- 3 files changed, 77 insertions(+), 19 deletions(-) diff --git a/app/Console/Commands/ItemProcessing/FetchItemDataCommand.php b/app/Console/Commands/ItemProcessing/FetchItemDataCommand.php index c2402cd..a087ed5 100644 --- a/app/Console/Commands/ItemProcessing/FetchItemDataCommand.php +++ b/app/Console/Commands/ItemProcessing/FetchItemDataCommand.php @@ -47,7 +47,15 @@ class FetchItemDataCommand extends Command { return ItemToFetch::has('jobs') ->with([ - 'jobs' => fn ($jobs) => array_search($job, $jobs->first()->remaining_jobs, true), +// 'jobs' => fn ($jobs) => dd($jobs->first()->remaining_jobs) /*array_search($job, $jobs->first()->remaining_jobs, true)*/, + 'jobs' => function ($jobs) use ($job) { + $remainingJobs = $jobs->first()->remaining_jobs; + if (is_string($remainingJobs)) { + $remainingJobs = json_decode($jobs->first()->remaining_jobs, true); + } + return array_search($job, $remainingJobs, true); + }, + ]) ->where('type', $type) ->take($batch) diff --git a/app/Service/MediaProcessing/MovieProcessingService.php b/app/Service/MediaProcessing/MovieProcessingService.php index 03937b0..47e07e2 100644 --- a/app/Service/MediaProcessing/MovieProcessingService.php +++ b/app/Service/MediaProcessing/MovieProcessingService.php @@ -312,6 +312,10 @@ class MovieProcessingService implements ProcessingInterface $movie = Movie::where('tmdb_id', $item->tmdb_id)->first(); + if (!$movie) { + return; + } + $externalId = ExternalId::where('entity_id', $movie->id) ->where('entity_type', ExternalId::TYPE_MOVIE) ->first(); @@ -321,6 +325,9 @@ class MovieProcessingService implements ProcessingInterface $externalIds['movie_id'] = $movie->id; unset($externalIds['id']); + $externalIds['entity_id'] = $movie->id; + $externalIds['entity_type'] = ExternalId::TYPE_MOVIE; + ExternalId::create($externalIds); } @@ -337,6 +344,12 @@ class MovieProcessingService implements ProcessingInterface $movie = Movie::where('tmdb_id', $item->tmdb_id)->first(); + if (!$movie) { + $itemToFetch = $this->createJob($item->tmdb_id); + $this->getDetails($itemToFetch); + return; + } + $types = [ 'flatrate', 'rent', @@ -349,6 +362,11 @@ class MovieProcessingService implements ProcessingInterface continue; } $providerInfo = $tmdbWatchProvider[$type]; + $providerInfo = reset($providerInfo); + + if (!isset($providerInfo['provider_id'])) { + continue; + } $existingWatchProvider = WatchProvider::where('tmdb_provider_id', $providerInfo['provider_id'])->first(); @@ -362,17 +380,14 @@ class MovieProcessingService implements ProcessingInterface ]); } - } - - if ($movie->whereHas('watchProviders', function ($query) use ($movie) { - $query->where('movie_id', $movie->id); - })->exists()) { - return; - } else { - $movie->watchProviders()->attach($existingWatchProvider, [ - 'locale' => $locale, - 'tmdb_url' => $tmdbWatchProvider['link'], - ]); + if (!$movie->whereHas('watchProviders', function ($query) use ($movie) { + $query->where('movie_id', $movie->id); + })->exists()) { + $movie->watchProviders()->attach($existingWatchProvider, [ + 'locale' => $locale, + 'tmdb_url' => $tmdbWatchProvider['link'], + ]); + } } } @@ -402,7 +417,7 @@ class MovieProcessingService implements ProcessingInterface 'size' => $tmdbVideo['size'], 'type' => $tmdbVideo['type'], 'official' => $tmdbVideo['official'], - 'published_at' => $tmdbVideo['published_at'], + 'published_at' => $tmdbVideo['published_at'] ? new \DateTime($tmdbVideo['published_at']) : null, 'tmdb_id' => $tmdbVideo['id'], ]); } @@ -687,6 +702,11 @@ class MovieProcessingService implements ProcessingInterface $itemToFetchJobs = ItemToFetchJobs::where('tmdb_id', $tmdbId)->first(); $remainingJobs = $itemToFetchJobs->remaining_jobs; + + if (is_string($remainingJobs)) { + $remainingJobs = json_decode($remainingJobs, true); + } + $remainingJobKey = array_search($job, $remainingJobs); unset($remainingJobs[$remainingJobKey]); @@ -804,4 +824,34 @@ class MovieProcessingService implements ProcessingInterface } } } + + private function createJob($tmdbId) + { + $itemToFetch = ItemToFetch::where('tmdb_id', $tmdbId)->where('type', ItemToFetch::TYPE_MOVIE)->first(); + + if ($itemToFetch) { + return $itemToFetch; + } + + $movie = Movie::where('tmdb_id', $tmdbId)->first(); + if ($movie) { + return; + } + + $itemToFetch = ItemToFetch::create([ + 'tmdb_id' => $tmdbId, + 'type' => ItemToFetch::TYPE_MOVIE, + ]); + + $jobs = ItemToFetchJobs::JOBS; + $jobs[] = "Details"; + + ItemToFetchJobs::create([ + 'tmdb_id' => $tmdbId, + 'type' => ItemToFetch::TYPE_MOVIE, + 'remaining_jobs' => json_encode($jobs), + ]); + + return $itemToFetch; + } } diff --git a/database/migrations/2024_12_30_010725_create_external_ids_table.php b/database/migrations/2024_12_30_010725_create_external_ids_table.php index 563305b..8ac7aee 100644 --- a/database/migrations/2024_12_30_010725_create_external_ids_table.php +++ b/database/migrations/2024_12_30_010725_create_external_ids_table.php @@ -15,12 +15,12 @@ return new class extends Migration $table->id(); $table->integer('entity_id'); $table->string('entity_type'); - $table->integer('tmdb_id'); - $table->string('imdb_id'); - $table->string('wikidata_id'); - $table->string('facebook_id'); - $table->string('instagram_id'); - $table->string('twitter_id'); + $table->integer('tmdb_id')->nullable(); + $table->string('imdb_id')->nullable(); + $table->string('wikidata_id')->nullable(); + $table->string('facebook_id')->nullable(); + $table->string('instagram_id')->nullable(); + $table->string('twitter_id')->nullable(); $table->timestamps(); }); }