From 51a6ab6c7a88e3453f82e084a2b4877cf4dc6a4d Mon Sep 17 00:00:00 2001 From: DoarMihai Date: Mon, 30 Dec 2024 02:28:58 +0200 Subject: [PATCH] [#1] added the credits part --- app/Models/Movie.php | 10 ++ app/Models/Person.php | 34 ++++++ .../MovieProcessingService.php | 110 +++++++++++++++++- app/Service/TMDBApi/PersonApi.php | 12 ++ .../2024_12_30_000755_create_people_table.php | 42 +++++++ ...4_12_30_000853_create_movie_cast_table.php | 33 ++++++ ...4_12_30_000900_create_movie_crew_table.php | 33 ++++++ 7 files changed, 272 insertions(+), 2 deletions(-) create mode 100644 app/Models/Person.php create mode 100644 app/Service/TMDBApi/PersonApi.php create mode 100644 database/migrations/2024_12_30_000755_create_people_table.php create mode 100644 database/migrations/2024_12_30_000853_create_movie_cast_table.php create mode 100644 database/migrations/2024_12_30_000900_create_movie_crew_table.php diff --git a/app/Models/Movie.php b/app/Models/Movie.php index 3e3c76a..ffed14d 100644 --- a/app/Models/Movie.php +++ b/app/Models/Movie.php @@ -37,4 +37,14 @@ class Movie extends Model { return $this->belongsToMany(Genre::class, 'movie_genres', 'movie_id', 'genre_id'); } + + public function castList() + { + return $this->belongsToMany(Person::class, 'movie_cast', 'movie_id', 'person_id'); + } + + public function crewList() + { + return $this->belongsToMany(Person::class, 'movie_crew', 'movie_id', 'person_id'); + } } diff --git a/app/Models/Person.php b/app/Models/Person.php new file mode 100644 index 0000000..2fffe33 --- /dev/null +++ b/app/Models/Person.php @@ -0,0 +1,34 @@ +belongsToMany(Movie::class, 'movie_cast', 'person_id', 'movie_id'); + } + + public function moviesCrew() { + return $this->belongsToMany(Movie::class, 'movie_crew', 'person_id', 'movie_id'); + } +} diff --git a/app/Service/MediaProcessing/MovieProcessingService.php b/app/Service/MediaProcessing/MovieProcessingService.php index 02ec44b..2cf56c4 100644 --- a/app/Service/MediaProcessing/MovieProcessingService.php +++ b/app/Service/MediaProcessing/MovieProcessingService.php @@ -7,16 +7,22 @@ use App\Models\ItemToFetchJobs; use App\Models\Movie; use App\Models\MovieAlternativeTitle; use App\Models\MovieCollection; +use App\Models\Person; use App\Service\TMDBApi\MoviesApi; +use App\Service\TMDBApi\PersonApi; use Illuminate\Support\Str; class MovieProcessingService implements ProcessingInterface { private MoviesApi $moviesApi; + private PersonApi $personApi; - public function __construct(MoviesApi $moviesApi) - { + public function __construct( + MoviesApi $moviesApi, + PersonApi $personApi + ) { $this->moviesApi = $moviesApi; + $this->personApi = $personApi; } public function process($item, $job) @@ -178,11 +184,111 @@ class MovieProcessingService implements ProcessingInterface private function processCast($movie, array $castList) { + if (empty($castList)) { + return; + } + foreach ($castList as $castPerson) { + $person = Person::where('tmdb_id', $castPerson['id'])->first(); + + if (!$person) { + $personData = [ + 'adult' => $castPerson['adult'], + 'tmdb_id' => $castPerson['id'], + 'gender' => $castPerson['gender'], + 'name' => $castPerson['name'], + 'slug' => Str::slug($castPerson['name']), + 'popularity' => $castPerson['popularity'], + 'profile_path' => $castPerson['profile_path'], + ]; + + $tmdbPerson = $this->personApi->getPerson($castPerson['id']); + + if (!empty($tmdbPerson)) { + $personData = array_merge($personData, [ + 'biography' => $tmdbPerson['biography'], + 'birtday' => $tmdbPerson['birthday'], + 'deathday' => $tmdbPerson['deathday'], + 'homepage' => $tmdbPerson['homepage'], + 'imdb_id' => $tmdbPerson['imdb_id'], + 'known_for_department' => $tmdbPerson['known_for_department'] ? json_encode($tmdbPerson['known_for_department']) : null, + 'also_known_as' => $tmdbPerson['also_known_as'] ? json_encode($tmdbPerson['also_known_as']) : null, + 'place_of_birth' => $tmdbPerson['place_of_birth'], + ]); + } + + $person = Person::create($personData); + } + + if ($movie->whereHas('castList', function ($query) use ($person) { + $query->where('tmdb_id', '=', $person->tmdb_id); + })->exists()) { + echo "Person is in cast | "; + return; + } else { + echo "Person is not in cast |"; + $movie->castList()->attach($person, [ + 'known_for_department' => $castPerson['known_for_department'], + 'character' => $castPerson['character'], + 'tmdb_cast_id' => $castPerson['cast_id'], + 'order' => $castPerson['order'], + ]); + } + } } private function processCrew($movie, array $crewList) { + if (empty($crewList)) { + return; + } + foreach ($crewList as $crewPerson) { + $person = Person::where('tmdb_id', $crewPerson['id'])->first(); + + if (!$person) { + $personData = [ + 'adult' => $crewPerson['adult'], + 'tmdb_id' => $crewPerson['id'], + 'gender' => $crewPerson['gender'], + 'name' => $crewPerson['name'], + 'slug' => Str::slug($crewPerson['name']), + 'popularity' => $crewPerson['popularity'], + 'profile_path' => $crewPerson['profile_path'], + ]; + + $tmdbPerson = $this->personApi->getPerson($crewPerson['id']); + + if (!empty($tmdbPerson)) { + $personData = array_merge($personData, [ + 'biography' => $tmdbPerson['biography'], + 'birtday' => $tmdbPerson['birthday'], + 'deathday' => $tmdbPerson['deathday'], + 'homepage' => $tmdbPerson['homepage'], + 'imdb_id' => $tmdbPerson['imdb_id'], + 'known_for_department' => $tmdbPerson['known_for_department'] ? json_encode($tmdbPerson['known_for_department']) : null, + 'also_known_as' => $tmdbPerson['also_known_as'] ? json_encode($tmdbPerson['also_known_as']) : null, + 'place_of_birth' => $tmdbPerson['place_of_birth'], + ]); + } + + $person = Person::create($personData); + } + + if ($movie->whereHas('crewList', function ($query) use ($person) { + $query->where('tmdb_id', '=', $person->tmdb_id); + })->exists()) { + echo "Person is in crew | "; + return; + } else { + echo "Person is not in crew |"; + $movie->castList()->attach($person, [ + 'known_for_department' => json_encode($crewPerson['known_for_department']), + 'job' => $crewPerson['job'], + 'department' => $crewPerson['department'], + 'tmdb_cast_id' => $crewPerson['credit_id'], + ]); + } + } } } diff --git a/app/Service/TMDBApi/PersonApi.php b/app/Service/TMDBApi/PersonApi.php new file mode 100644 index 0000000..539449a --- /dev/null +++ b/app/Service/TMDBApi/PersonApi.php @@ -0,0 +1,12 @@ +get('person/' . $personId, $parameters, $headers); + } +} diff --git a/database/migrations/2024_12_30_000755_create_people_table.php b/database/migrations/2024_12_30_000755_create_people_table.php new file mode 100644 index 0000000..9c12301 --- /dev/null +++ b/database/migrations/2024_12_30_000755_create_people_table.php @@ -0,0 +1,42 @@ +id(); + $table->boolean('adult')->default(0); + $table->json('also_known_as')->nullable(); + $table->integer('tmdb_id'); + $table->text('biography')->nullable(); + $table->date('birtday')->nullable(); + $table->date('deathday')->nullable(); + $table->tinyInteger('gender')->default(0);// 0 not specified,1 female, 2 male, 3 non binary + $table->string('homepage', 500)->nullable(); + $table->string('imdb_id', 20)->nullable(); + $table->json('known_for_department'); + $table->string('name', 255); + $table->string('slug', 255); + $table->string('place_of_birth', 500)->nullable(); + $table->decimal('popularity', 10, 2)->default(0); + $table->text('profile_path')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('people'); + } +}; diff --git a/database/migrations/2024_12_30_000853_create_movie_cast_table.php b/database/migrations/2024_12_30_000853_create_movie_cast_table.php new file mode 100644 index 0000000..bcbcfdb --- /dev/null +++ b/database/migrations/2024_12_30_000853_create_movie_cast_table.php @@ -0,0 +1,33 @@ +id(); + $table->bigInteger('movie_id')->nullable(false); + $table->bigInteger('person_id')->nullable(false); + $table->string('known_for_department', 500); + $table->string('character', 500); + $table->string('tmdb_cast_id', 500); + $table->integer('order'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('movie_cast'); + } +}; diff --git a/database/migrations/2024_12_30_000900_create_movie_crew_table.php b/database/migrations/2024_12_30_000900_create_movie_crew_table.php new file mode 100644 index 0000000..0b84e8d --- /dev/null +++ b/database/migrations/2024_12_30_000900_create_movie_crew_table.php @@ -0,0 +1,33 @@ +id(); + $table->unsignedBigInteger('movie_id')->nullable(false); + $table->unsignedBigInteger('person_id')->nullable(false); + $table->string('known_for_department', 500); + $table->string('job', 500); + $table->string('department', 500); + $table->string('tmdb_cast_id', 500); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('movie_crew'); + } +};