[#1] watch providers

This commit is contained in:
2024-12-30 03:19:01 +02:00
parent 89f80b7937
commit c16b36a6da
5 changed files with 138 additions and 0 deletions
+5
View File
@@ -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');
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class WatchProvider extends Model
{
protected $fillable = [
'type',
'logo_path',
'tmdb_provider_id',
'provider_name',
'display_priority',
];
}
@@ -14,6 +14,7 @@ use App\Models\MovieCollection;
use App\Models\Person;
use App\Models\ReleaseDate;
use App\Models\Translation;
use App\Models\WatchProvider;
use App\Service\TMDBApi\MoviesApi;
use App\Service\TMDBApi\PersonApi;
use Illuminate\Support\Str;
@@ -324,6 +325,59 @@ class MovieProcessingService implements ProcessingInterface
$this->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();