[#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');
|
||||
}
|
||||
|
||||
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\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'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user