[#1] added the credits part
This commit is contained in:
@@ -37,4 +37,14 @@ class Movie extends Model
|
|||||||
{
|
{
|
||||||
return $this->belongsToMany(Genre::class, 'movie_genres', 'movie_id', 'genre_id');
|
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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Person extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = [
|
||||||
|
'adult',
|
||||||
|
'also_known_as',
|
||||||
|
'tmdb_id',
|
||||||
|
'biography',
|
||||||
|
'birtday',
|
||||||
|
'deathday',
|
||||||
|
'gender',
|
||||||
|
'homepage',
|
||||||
|
'imdb_id',
|
||||||
|
'known_for_department',
|
||||||
|
'name',
|
||||||
|
'slug',
|
||||||
|
'place_of_birth',
|
||||||
|
'popularity',
|
||||||
|
'profile_path',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function moviesCast() {
|
||||||
|
return $this->belongsToMany(Movie::class, 'movie_cast', 'person_id', 'movie_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function moviesCrew() {
|
||||||
|
return $this->belongsToMany(Movie::class, 'movie_crew', 'person_id', 'movie_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,16 +7,22 @@ use App\Models\ItemToFetchJobs;
|
|||||||
use App\Models\Movie;
|
use App\Models\Movie;
|
||||||
use App\Models\MovieAlternativeTitle;
|
use App\Models\MovieAlternativeTitle;
|
||||||
use App\Models\MovieCollection;
|
use App\Models\MovieCollection;
|
||||||
|
use App\Models\Person;
|
||||||
use App\Service\TMDBApi\MoviesApi;
|
use App\Service\TMDBApi\MoviesApi;
|
||||||
|
use App\Service\TMDBApi\PersonApi;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
class MovieProcessingService implements ProcessingInterface
|
class MovieProcessingService implements ProcessingInterface
|
||||||
{
|
{
|
||||||
private MoviesApi $moviesApi;
|
private MoviesApi $moviesApi;
|
||||||
|
private PersonApi $personApi;
|
||||||
|
|
||||||
public function __construct(MoviesApi $moviesApi)
|
public function __construct(
|
||||||
{
|
MoviesApi $moviesApi,
|
||||||
|
PersonApi $personApi
|
||||||
|
) {
|
||||||
$this->moviesApi = $moviesApi;
|
$this->moviesApi = $moviesApi;
|
||||||
|
$this->personApi = $personApi;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function process($item, $job)
|
public function process($item, $job)
|
||||||
@@ -178,11 +184,111 @@ class MovieProcessingService implements ProcessingInterface
|
|||||||
|
|
||||||
private function processCast($movie, array $castList)
|
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)
|
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'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Service\TMDBApi;
|
||||||
|
|
||||||
|
class PersonApi extends AbstractApiService
|
||||||
|
{
|
||||||
|
public function getPerson($personId, array $parameters = [], array $headers = [])
|
||||||
|
{
|
||||||
|
sleep(1);
|
||||||
|
return $this->get('person/' . $personId, $parameters, $headers);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('people', function (Blueprint $table) {
|
||||||
|
$table->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');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('movie_cast', function (Blueprint $table) {
|
||||||
|
$table->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');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('movie_crew', function (Blueprint $table) {
|
||||||
|
$table->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');
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user