diff --git a/app/Models/Movie.php b/app/Models/Movie.php index 56aba9e..e94b2ee 100644 --- a/app/Models/Movie.php +++ b/app/Models/Movie.php @@ -62,4 +62,9 @@ class Movie extends Model { return $this->belongsToMany(ReleaseDate::class, 'movie_release_dates', 'movie_id', 'release_date_id'); } + + public function watchProviders() + { + return $this->belongsToMany(WatchProvider::class, 'movie_watch_providers', 'movie_id', 'watch_provider_id'); + } } diff --git a/app/Models/WatchProvider.php b/app/Models/WatchProvider.php new file mode 100644 index 0000000..64d5ffe --- /dev/null +++ b/app/Models/WatchProvider.php @@ -0,0 +1,16 @@ +removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_EXTERNAL_IDS); } + private function getWatchProviders($item) + { + $tmdbWatchProviders = $this->moviesApi->getWatchProviders($item->tmdb_id); + + if (empty($tmdbWatchProviders) || empty($tmdbWatchProviders['results'])) { + return; + } + + $movie = Movie::where('tmdb_id', $item->tmdb_id)->first(); + + $types = [ + 'flatrate', + 'rent', + 'buy', + ]; + + foreach ($tmdbWatchProviders['results'] as $locale => $tmdbWatchProvider) { + foreach ($types as $type) { + if (!isset($tmdbWatchProvider[$type])) { + continue; + } + $providerInfo = $tmdbWatchProvider[$type]; + + $existingWatchProvider = WatchProvider::where('tmdb_provider_id', $providerInfo['provider_id'])->first(); + + if(!$existingWatchProvider) { + $existingWatchProvider = WatchProvider::create([ + 'type' => $type, + 'logo_path' => $providerInfo['logo_path'], + 'tmdb_provider_id' => $providerInfo['provider_id'], + 'provider_name' => $providerInfo['provider_name'], + 'display_priority' => $providerInfo['display_priority'], + ]); + } + + } + + if ($movie->whereHas('watchProviders', function ($query) use ($movie) { + $query->where('movie_id', $movie->id); + })->exists()) { + echo "Movie has watch provider | "; + return; + } else { + echo "Movie does not have watch provider | "; + $movie->watchProviders()->attach($existingWatchProvider, [ + 'locale' => $locale, + 'tmdb_url' => $tmdbWatchProvider['link'], + ]); + } + + } + } + private function processMovieCollection(int $movieId, $belongsToCollection) { $movieCollection = MovieCollection::where('tmdb_id', $belongsToCollection['id'])->first(); diff --git a/database/migrations/2024_12_30_011210_create_watch_providers_table.php b/database/migrations/2024_12_30_011210_create_watch_providers_table.php new file mode 100644 index 0000000..79980d5 --- /dev/null +++ b/database/migrations/2024_12_30_011210_create_watch_providers_table.php @@ -0,0 +1,32 @@ +id(); + $table->string('type'); + $table->string('logo_path'); + $table->integer('tmdb_provider_id'); + $table->string('provider_name'); + $table->integer('display_priority'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('watch_providers'); + } +}; diff --git a/database/migrations/2024_12_30_011230_create_movie_watch_providers_table.php b/database/migrations/2024_12_30_011230_create_movie_watch_providers_table.php new file mode 100644 index 0000000..3f7b0d7 --- /dev/null +++ b/database/migrations/2024_12_30_011230_create_movie_watch_providers_table.php @@ -0,0 +1,31 @@ +id(); + $table->unsignedBigInteger('movie_id'); + $table->integer('watch_provider_id'); + $table->string('locale'); + $table->string('tmdb_url'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('movie_watch_providers'); + } +};