From 60ee3bb7595211f2ac6f434aac35ce111c3c7c51 Mon Sep 17 00:00:00 2001 From: DoarMihai Date: Wed, 8 Jan 2025 01:50:31 +0200 Subject: [PATCH] [#2] content ratings --- app/Models/ContentRating.php | 18 +++++++++++ app/Models/Show.php | 5 ++++ .../MediaProcessing/ShowProcessingService.php | 30 +++++++++++++++++++ ...07_233631_create_content_ratings_table.php | 30 +++++++++++++++++++ ...3646_create_show_content_ratings_table.php | 29 ++++++++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 app/Models/ContentRating.php create mode 100644 database/migrations/2025_01_07_233631_create_content_ratings_table.php create mode 100644 database/migrations/2025_01_07_233646_create_show_content_ratings_table.php diff --git a/app/Models/ContentRating.php b/app/Models/ContentRating.php new file mode 100644 index 0000000..deae84d --- /dev/null +++ b/app/Models/ContentRating.php @@ -0,0 +1,18 @@ + 'array', + ]; +} diff --git a/app/Models/Show.php b/app/Models/Show.php index 2855d24..c563678 100644 --- a/app/Models/Show.php +++ b/app/Models/Show.php @@ -65,4 +65,9 @@ class Show extends Model { return $this->belongsToMany(WatchProvider::class, 'show_watch_providers', 'show_id', 'watch_provider_id'); } + + public function contentRatings() + { + return $this->belongsToMany(ContentRating::class, 'show_content_ratings', 'show_id', 'content_rating_id'); + } } diff --git a/app/Service/MediaProcessing/ShowProcessingService.php b/app/Service/MediaProcessing/ShowProcessingService.php index 04a145e..e4244fd 100644 --- a/app/Service/MediaProcessing/ShowProcessingService.php +++ b/app/Service/MediaProcessing/ShowProcessingService.php @@ -3,6 +3,7 @@ namespace App\Service\MediaProcessing; use App\Models\Comment; +use App\Models\ContentRating; use App\Models\Episode; use App\Models\ExternalId; use App\Models\Image; @@ -491,7 +492,36 @@ class ShowProcessingService implements ProcessingInterface private function getContentRatings($item) { + $tmdbContentRatings = $this->showsApi->getContentRatings($item->tmdb_id); + if (empty($tmdbContentRatings) || empty($tmdbContentRatings['results'])) { + return; + } + + $show = Show::where('tmdb_id', $item->tmdb_id)->first(); + + foreach ($tmdbContentRatings['results'] as $contentRating) { + $existingContentRating = ContentRating::where('iso_3166', $contentRating['iso_3166_1']) + ->where('rating', $contentRating['rating']) + ->first(); + + if (!$existingContentRating) { + $existingContentRating = ContentRating::create([ + 'descriptors' => $contentRating['descriptors'], + 'iso_3166' => $contentRating['iso_3166_1'], + 'rating' => $contentRating['rating'], + ]); + } + + // attach if not attached + if (!$show->whereHas('contentRatings', function ($query) use ($show) { + $query->where('show_id', $show->id); + })->exists()) { + $show->contentRatings()->attach($existingContentRating); + } + } + + $this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_CONTENT_RATINGS); } private function processSeasons(Show $show, array $seasons) diff --git a/database/migrations/2025_01_07_233631_create_content_ratings_table.php b/database/migrations/2025_01_07_233631_create_content_ratings_table.php new file mode 100644 index 0000000..08451ab --- /dev/null +++ b/database/migrations/2025_01_07_233631_create_content_ratings_table.php @@ -0,0 +1,30 @@ +id(); + $table->json('descriptors')->default(null); + $table->string('iso_3166', 10)->default(null); + $table->string('rating', 50)->default(null); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('content_ratings'); + } +}; diff --git a/database/migrations/2025_01_07_233646_create_show_content_ratings_table.php b/database/migrations/2025_01_07_233646_create_show_content_ratings_table.php new file mode 100644 index 0000000..172584b --- /dev/null +++ b/database/migrations/2025_01_07_233646_create_show_content_ratings_table.php @@ -0,0 +1,29 @@ +id(); + $table->unsignedBigInteger('show_id'); + $table->unsignedBigInteger('content_rating_id'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('show_content_ratings'); + } +};