diff --git a/app/Models/Image.php b/app/Models/Image.php new file mode 100644 index 0000000..17e721a --- /dev/null +++ b/app/Models/Image.php @@ -0,0 +1,19 @@ +belongsToMany(Person::class, 'movie_crew', 'movie_id', 'person_id'); } + + public function images() + { + return $this->belongsToMany(Image::class, 'movie_images', 'movie_id', 'image_id'); + } } diff --git a/app/Service/MediaProcessing/MovieProcessingService.php b/app/Service/MediaProcessing/MovieProcessingService.php index 2cf56c4..5466dad 100644 --- a/app/Service/MediaProcessing/MovieProcessingService.php +++ b/app/Service/MediaProcessing/MovieProcessingService.php @@ -3,6 +3,7 @@ namespace App\Service\MediaProcessing; use App\Models\Genre; +use App\Models\Image; use App\Models\ItemToFetchJobs; use App\Models\Movie; use App\Models\MovieAlternativeTitle; @@ -104,7 +105,48 @@ class MovieProcessingService implements ProcessingInterface $this->processCast($movie, $tmdbCredits['cast'] ?? []); $this->processCrew($movie, $tmdbCredits['crew'] ?? []); - $this->removeElementFromRemainingJobs($item->tmdb_id, ItemJob::JOB_CREDITS); + $this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_CREDITS); + } + + private function getImages($item) + { + $tmdbImages = $this->moviesApi->getImages($item->tmdb_id); + + if (empty($tmdbImages)) { + return; + } + + $movie = Movie::where('tmdb_id', $item->tmdb_id)->first(); + + foreach ($tmdbImages as $type => $images) { + if (!is_array($images)) { + continue; + } + foreach ($images as $image) { + $imageExists = Image::where('type', $type) + ->where('file_path', $image['file_path']) + ->first(); + + if (!$imageExists) { + $image['iso'] = $image['iso_639_1']; + $image['type'] = $type; + $imageExists = Image::create($image); + } + + if ($movie->whereHas('images', function ($query) use ($imageExists) { + $query->where('type', $imageExists->type) + ->where('file_path', $imageExists->file_path); + })->exists()) { + echo "Movie has image | "; + return; + } else { + echo "Movie does not have image | "; + $movie->images()->attach($imageExists); + } + } + } + + $this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_IMAGES); } private function processMovieCollection(int $movieId, $belongsToCollection) diff --git a/database/migrations/2024_12_30_003233_create_images_table.php b/database/migrations/2024_12_30_003233_create_images_table.php new file mode 100644 index 0000000..e4d6eb8 --- /dev/null +++ b/database/migrations/2024_12_30_003233_create_images_table.php @@ -0,0 +1,35 @@ +id(); + $table->decimal('aspect_ratio', 10, 2); + $table->integer('height'); + $table->string('iso'); + $table->text('file_path'); + $table->decimal('vote_average', 10, 2); + $table->integer('vote_count'); + $table->integer('width'); + $table->string('type'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('images'); + } +}; diff --git a/database/migrations/2024_12_30_003839_create_movie_images_table.php b/database/migrations/2024_12_30_003839_create_movie_images_table.php new file mode 100644 index 0000000..47f22de --- /dev/null +++ b/database/migrations/2024_12_30_003839_create_movie_images_table.php @@ -0,0 +1,29 @@ +id(); + $table->unsignedBigInteger('movie_id'); + $table->unsignedBigInteger('image_id'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('movie_images'); + } +};