678 lines
24 KiB
PHP
678 lines
24 KiB
PHP
<?php
|
|
|
|
namespace App\Service\MediaProcessing;
|
|
|
|
use App\Models\Comment;
|
|
use App\Models\ExternalId;
|
|
use App\Models\Genre;
|
|
use App\Models\Image;
|
|
use App\Models\ItemToFetch;
|
|
use App\Models\ItemToFetchJobs;
|
|
use App\Models\Keyword;
|
|
use App\Models\MediaList;
|
|
use App\Models\MediaListItem;
|
|
use App\Models\Movie;
|
|
use App\Models\MovieAlternativeTitle;
|
|
use App\Models\MovieCollection;
|
|
use App\Models\Person;
|
|
use App\Models\ReleaseDate;
|
|
use App\Models\Translation;
|
|
use App\Models\User;
|
|
use App\Models\Video;
|
|
use App\Models\WatchProvider;
|
|
use App\Service\TMDBApi\ListApi;
|
|
use App\Service\TMDBApi\MoviesApi;
|
|
use App\Service\TMDBApi\PersonApi;
|
|
use Illuminate\Support\Str;
|
|
|
|
class MovieProcessingService implements ProcessingInterface
|
|
{
|
|
private MoviesApi $moviesApi;
|
|
private PersonApi $personApi;
|
|
private ListApi $listApi;
|
|
|
|
public function __construct(
|
|
MoviesApi $moviesApi,
|
|
PersonApi $personApi,
|
|
ListApi $listApi
|
|
) {
|
|
$this->moviesApi = $moviesApi;
|
|
$this->personApi = $personApi;
|
|
$this->listApi = $listApi;
|
|
}
|
|
|
|
public function process($item, $job)
|
|
{
|
|
$methodName = "get".ucfirst($job);
|
|
|
|
if (!method_exists($this, $methodName)) {
|
|
// @todo maybe raise an exception here
|
|
return;
|
|
}
|
|
|
|
$this->$methodName($item);
|
|
}
|
|
|
|
private function getDetails($item)
|
|
{
|
|
$tmdbMovie = $this->moviesApi->getMovie($item->tmdb_id);
|
|
$tmdbMovie['tmdb_id'] = $tmdbMovie['id'];
|
|
|
|
$movie = Movie::where('tmdb_id', $tmdbMovie['tmdb_id'])->first();
|
|
|
|
if($movie) {
|
|
return;
|
|
}
|
|
|
|
$tmdbMovie['slug'] = Str::slug($tmdbMovie['title']);
|
|
|
|
$movieId = Movie::create($tmdbMovie);
|
|
|
|
if (!empty($tmdbMovie['belongs_to_collection'])) {
|
|
$this->processMovieCollection($movieId->id, $tmdbMovie['belongs_to_collection']);
|
|
}
|
|
|
|
if (!empty($tmdbMovie['genres'])) {
|
|
$this->processMovieGenres($movieId->id, $tmdbMovie['genres']);
|
|
}
|
|
|
|
$this->removeElementFromRemainingJobs($tmdbMovie['id'], 'Details');
|
|
}
|
|
|
|
private function getAlternativeTitles($item)
|
|
{
|
|
$alternativeTitles = $this->moviesApi->getAlternativeTitles($item->tmdb_id);
|
|
|
|
if (empty($alternativeTitles) || !isset($alternativeTitles['titles'])) {
|
|
return;
|
|
}
|
|
|
|
$movie = Movie::where('tmdb_id', $item->tmdb_id)->first();
|
|
|
|
foreach ($alternativeTitles['titles'] as $alternativeTitle) {
|
|
$alternativeTitleExists = MovieAlternativeTitle::where('movie_id', $movie->id)
|
|
->where('iso', $alternativeTitle['iso_3166_1'])
|
|
->first();
|
|
|
|
if (!$alternativeTitleExists) {
|
|
MovieAlternativeTitle::create([
|
|
'iso' => $alternativeTitle['iso_3166_1'],
|
|
'movie_id' => $movie->id,
|
|
'title' => $alternativeTitle['title'],
|
|
'type' => $alternativeTitle['type'],
|
|
]);
|
|
}
|
|
}
|
|
|
|
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_ALTERNVATIVE_TITLES);
|
|
}
|
|
|
|
private function getCredits($item)
|
|
{
|
|
$tmdbCredits = $this->moviesApi->getCredits($item->tmdb_id);
|
|
|
|
if (empty($tmdbCredits)) {
|
|
return;
|
|
}
|
|
|
|
$movie = Movie::where('tmdb_id', $item->tmdb_id)->first();
|
|
|
|
$this->processCast($movie, $tmdbCredits['cast'] ?? []);
|
|
$this->processCrew($movie, $tmdbCredits['crew'] ?? []);
|
|
|
|
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_CREDITS);
|
|
}
|
|
|
|
private function getImages($item)
|
|
{
|
|
$tmdbImages = $this->moviesApi->getImages($item->tmdb_id);
|
|
|
|
if (empty($tmdbImages)) {
|
|
return;
|
|
}
|
|
|
|
$movie = Movie::where('tmdb_id', $item->tmdb_id)->first();
|
|
|
|
foreach ($tmdbImages as $type => $images) {
|
|
if (!is_array($images)) {
|
|
continue;
|
|
}
|
|
foreach ($images as $image) {
|
|
$imageExists = Image::where('type', $type)
|
|
->where('file_path', $image['file_path'])
|
|
->first();
|
|
|
|
if (!$imageExists) {
|
|
$image['iso'] = $image['iso_639_1'];
|
|
$image['type'] = $type;
|
|
$imageExists = Image::create($image);
|
|
}
|
|
|
|
if ($movie->whereHas('images', function ($query) use ($imageExists) {
|
|
$query->where('type', $imageExists->type)
|
|
->where('file_path', $imageExists->file_path);
|
|
})->exists()) {
|
|
return;
|
|
} else {
|
|
$movie->images()->attach($imageExists);
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_IMAGES);
|
|
}
|
|
|
|
private function getKeywords($item)
|
|
{
|
|
$tmdbKeywords = $this->moviesApi->getKeywords($item->tmdb_id);
|
|
|
|
if (empty($tmdbKeywords) || !isset($tmdbKeywords['keywords'])) {
|
|
return;
|
|
}
|
|
|
|
$movie = Movie::where('tmdb_id', $item->tmdb_id)->first();
|
|
|
|
foreach ($tmdbKeywords['keywords'] as $tmdbKeyword) {
|
|
$existingKeyword = Keyword::where('tmdb_id', $tmdbKeyword['id'])->first();
|
|
|
|
if (!$existingKeyword) {
|
|
$existingKeyword = Keyword::create([
|
|
'name' => $tmdbKeyword['name'],
|
|
'tmdb_id' => $tmdbKeyword['id'],
|
|
]);
|
|
}
|
|
|
|
if ($movie->whereHas('keywords', function ($query) use ($existingKeyword) {
|
|
$query->where('tmdb_id', $existingKeyword->tmdb_id);
|
|
})->exists()) {
|
|
return;
|
|
} else {
|
|
$movie->keywords()->attach($existingKeyword);
|
|
}
|
|
}
|
|
|
|
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_KEYWORDS);
|
|
}
|
|
|
|
private function getReleases($item)
|
|
{
|
|
$tmdbReleases = $this->moviesApi->getReleases($item->tmdb_id);
|
|
|
|
if (empty($tmdbReleases) || !isset($tmdbReleases['countries'])) {
|
|
return;
|
|
}
|
|
|
|
$movie = Movie::where('tmdb_id', $item->tmdb_id)->first();
|
|
|
|
foreach ($tmdbReleases['countries'] as $tmdbRelease) {
|
|
$existingReleaseDate = ReleaseDate::where('iso_3166', $tmdbRelease['iso_3166_1'])
|
|
->where('release_date', $tmdbRelease['release_date'])
|
|
->where('certification', $tmdbRelease['certification'])
|
|
->first();
|
|
|
|
|
|
if (!$existingReleaseDate) {
|
|
$existingReleaseDate = ReleaseDate::create([
|
|
'iso_3166' => $tmdbRelease['iso_3166_1'],
|
|
'certification' => $tmdbRelease['certification'],
|
|
'descriptors' => $tmdbRelease['descriptors'] ? json_encode($tmdbRelease['descriptors']) : null,
|
|
'release_date' => $tmdbRelease['release_date'],
|
|
]);
|
|
}
|
|
|
|
if ($movie->whereHas('releaseDates', function ($query) use ($movie) {
|
|
$query->where('movie_id', $movie->id);
|
|
})->exists()) {
|
|
return;
|
|
} else {
|
|
$movie->releaseDates()->attach($existingReleaseDate);
|
|
}
|
|
}
|
|
|
|
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_RELEASES);
|
|
}
|
|
|
|
private function getTranslations($item)
|
|
{
|
|
$tmdbMovieTranslations = $this->moviesApi->getTranslations($item->tmdb_id);
|
|
|
|
if (!$tmdbMovieTranslations || !isset($tmdbMovieTranslations['translations'])) {
|
|
return;
|
|
}
|
|
|
|
$movie = Movie::where('tmdb_id', $item->tmdb_id)->first();
|
|
|
|
foreach ($tmdbMovieTranslations['translations'] as $tmdbTranslation) {
|
|
$existingTranslation = Translation::where('movie_id', $movie->id)
|
|
->where('name', $tmdbTranslation['name'])
|
|
->first();
|
|
|
|
if (!$existingTranslation) {
|
|
Translation::create([
|
|
'movie_id' => $movie->id,
|
|
'iso_3166' => $tmdbTranslation['iso_3166_1'],
|
|
'iso_639' => $tmdbTranslation['iso_639_1'],
|
|
'name' => $tmdbTranslation['name'],
|
|
'english_name' => $tmdbTranslation['english_name'],
|
|
'homepage' => $tmdbTranslation['data']['homepage'],
|
|
'overview' => $tmdbTranslation['data']['overview'],
|
|
'runtime' => $tmdbTranslation['data']['runtime'],
|
|
'tagline' => $tmdbTranslation['data']['tagline'],
|
|
'title' => $tmdbTranslation['data']['title'],
|
|
]);
|
|
}
|
|
}
|
|
|
|
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_TRANSLATIONS);
|
|
}
|
|
|
|
private function getReviews($item)
|
|
{
|
|
$tmdbMovieReviews = $this->moviesApi->getReviews($item->tmdb_id);
|
|
|
|
if (empty($tmdbMovieReviews) || !isset($tmdbMovieReviews['results'])){
|
|
return;
|
|
}
|
|
|
|
$movie = Movie::where('tmdb_id', $item->tmdb_id)->first();
|
|
|
|
foreach ($tmdbMovieReviews['results'] as $tmdbReview) {
|
|
$existingReview = Comment::where('entity_id', $movie->id)
|
|
->where('entity_type', Comment::TYPE_MOVIE)
|
|
->where('tmdb_id', $tmdbReview['id'])
|
|
->first();
|
|
|
|
if (!$existingReview) {
|
|
Comment::create([
|
|
'entity_id' => $movie->id,
|
|
'entity_type' => Comment::TYPE_MOVIE,
|
|
'author_name' => $tmdbReview['author'],
|
|
'author_avatar' => $tmdbReview['author_details']['avatar_path'],
|
|
'author_rating' => $tmdbReview['author_details']['rating'],
|
|
'content' => $tmdbReview['content'],
|
|
'published_at' => $tmdbReview['created_at'],
|
|
'tmdb_id' => $tmdbReview['id'],
|
|
'tmdb_url' => $tmdbReview['url'],
|
|
]);
|
|
}
|
|
}
|
|
|
|
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_REVIEWS);
|
|
}
|
|
|
|
private function getExternalIds($item)
|
|
{
|
|
$externalIds = $this->moviesApi->getExternalIds($item->tmdb_id);
|
|
|
|
if (empty($externalIds)) {
|
|
return;
|
|
}
|
|
|
|
$movie = Movie::where('tmdb_id', $item->tmdb_id)->first();
|
|
|
|
$externalId = ExternalId::where('entity_id', $movie->id)
|
|
->where('entity_type', ExternalId::TYPE_MOVIE)
|
|
->first();
|
|
|
|
if (!$externalId) {
|
|
$externalIds['tmdb_id'] = $externalIds['id'];
|
|
$externalIds['movie_id'] = $movie->id;
|
|
unset($externalIds['id']);
|
|
|
|
ExternalId::create($externalIds);
|
|
}
|
|
|
|
$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()) {
|
|
return;
|
|
} else {
|
|
$movie->watchProviders()->attach($existingWatchProvider, [
|
|
'locale' => $locale,
|
|
'tmdb_url' => $tmdbWatchProvider['link'],
|
|
]);
|
|
}
|
|
}
|
|
|
|
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_WATCH_PROVIDERS);
|
|
}
|
|
|
|
private function getVideos($item)
|
|
{
|
|
$movie = Movie::where('tmdb_id', $item->tmdb_id)->first();
|
|
|
|
$tmdbVideos = $this->moviesApi->getVideos($item->tmdb_id);
|
|
|
|
if (empty($tmdbVideos) || empty($tmdbVideos['results'])) {
|
|
return;
|
|
}
|
|
|
|
foreach ($tmdbVideos['results'] as $tmdbVideo) {
|
|
$videoExists = Video::where('tmdb_id', $tmdbVideo['id'])->first();
|
|
|
|
if (!$videoExists) {
|
|
$videoExists = Video::create([
|
|
'iso_639' => $tmdbVideo['iso_639_1'],
|
|
'iso_3166' => $tmdbVideo['iso_3166_1'],
|
|
'name' => $tmdbVideo['name'],
|
|
'key' => $tmdbVideo['key'],
|
|
'site' => $tmdbVideo['site'],
|
|
'size' => $tmdbVideo['size'],
|
|
'type' => $tmdbVideo['type'],
|
|
'official' => $tmdbVideo['official'],
|
|
'published_at' => $tmdbVideo['published_at'],
|
|
'tmdb_id' => $tmdbVideo['id'],
|
|
]);
|
|
}
|
|
|
|
if ($movie->whereHas('videos', function ($query) use ($movie) {
|
|
$query->where('movie_id', $movie->id);
|
|
})->exists()) {
|
|
return;
|
|
} else {
|
|
$movie->videos()->attach($videoExists);
|
|
}
|
|
}
|
|
|
|
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_VIDEOS);
|
|
}
|
|
|
|
public function getLists($item)
|
|
{
|
|
$tmdbLists = $this->moviesApi->getLists($item->tmdb_id);
|
|
|
|
if (empty($tmdbLists) || empty($tmdbLists['results'])) {
|
|
return;
|
|
}
|
|
|
|
foreach ($tmdbLists['results'] as $tmdbList) {
|
|
$list = MediaList::where('tmdb_id', $tmdbList['id'])->first();
|
|
$tmdbList['tmdb_id'] = $tmdbList['id'];
|
|
unset($tmdbList['id']);
|
|
|
|
if (!$list) {
|
|
$list = MediaList::create($tmdbList);
|
|
}
|
|
|
|
$listDetails = $this->listApi->details($tmdbList['tmdb_id']);
|
|
|
|
if (empty($listDetails)) {
|
|
continue;
|
|
}
|
|
|
|
$user = User::where('name', $listDetails['created_by'])->first();
|
|
|
|
if (!$user) {
|
|
$user = User::create([
|
|
'name' => $listDetails['created_by'],
|
|
'slug' => Str::slug($listDetails['created_by']),
|
|
'email' => $listDetails['created_by'].'@nowhere.wtf',
|
|
'password' => 'no_password_neeeded_here',
|
|
'is_dummy_account' => true,
|
|
]);
|
|
}
|
|
|
|
if (!$user->whereHas('mediaLists', function ($query) use ($list) {
|
|
$query->where('media_list_id', $list->id);
|
|
})->exists()) {
|
|
$user->mediaLists()->attach($list);
|
|
}
|
|
|
|
foreach ($tmdbList['items'] as $tmdbListItem) {
|
|
$mediaListItem = MediaListItem::where('media_list_id', $list->id)
|
|
->where('tmdb_id', $tmdbListItem['id'])
|
|
->first();
|
|
|
|
if (!$mediaListItem) {
|
|
$movie = Movie::where('tmdb_id', $item->tmdb_id)->first();
|
|
|
|
if (!$movie) {
|
|
$itemToFetch = ItemToFetch::where('tmdb_id', $item->tmdb_id)->first();
|
|
|
|
if (!$itemToFetch) {
|
|
$itemToFetch = ItemToFetch::create([
|
|
'tmdb_id' => $item->tmdb_id,
|
|
'type' => ItemToFetch::TYPE_MOVIE,
|
|
]);
|
|
|
|
$jobs = ItemToFetchJobs::JOBS;
|
|
$jobs[] = "Details";
|
|
|
|
ItemToFetchJobs::create([
|
|
'tmdb_id' => $item->tmdb_id,
|
|
'type' => ItemToFetch::TYPE_MOVIE,
|
|
'remaining_jobs' => json_encode($jobs),
|
|
]);
|
|
}
|
|
|
|
MediaListItem::create([
|
|
'media_list_id' => $list->id,
|
|
'item_type' => ItemToFetch::TYPE_MOVIE,
|
|
'tmdb_id' => $item->tmdb_id,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_LISTS);
|
|
}
|
|
|
|
private function processMovieCollection(int $movieId, $belongsToCollection)
|
|
{
|
|
$movieCollection = MovieCollection::where('tmdb_id', $belongsToCollection['id'])->first();
|
|
|
|
if (!$movieCollection) {
|
|
$movieCollection = MovieCollection::create([
|
|
'name' => $belongsToCollection['name'],
|
|
'slug' => Str::slug($belongsToCollection['name']),
|
|
'poster_path' => $belongsToCollection['poster_path'],
|
|
'backdrop_path' => $belongsToCollection['backdrop_path'],
|
|
'tmdb_id' => $belongsToCollection['id'],
|
|
]);
|
|
}
|
|
|
|
$movie = Movie::find($movieId);
|
|
|
|
if ($movie->whereHas('collections', function ($query) use ($movieCollection) {
|
|
$query->where('tmdb_id', '=', $movieCollection->tmdb_id);
|
|
})->exists()) {
|
|
return;
|
|
} else {
|
|
$movie->collections()->attach($movieCollection);
|
|
}
|
|
|
|
}
|
|
|
|
private function processMovieGenres(int $movieId, array $tmdbGenres)
|
|
{
|
|
foreach ($tmdbGenres as $tmdbGenre) {
|
|
$genre = Genre::where('name', $tmdbGenre['name'])
|
|
->where('tmdb_id', $tmdbGenre['id'])
|
|
->first();
|
|
|
|
if (!$genre) {
|
|
$genre = Genre::create([
|
|
'name' => $tmdbGenre['name'],
|
|
'slug' => Str::slug($tmdbGenre['name']),
|
|
'tmdb_id' => $tmdbGenre['id'],
|
|
'type' => Genre::TYPE_MOVIE,
|
|
]);
|
|
}
|
|
|
|
$movie = Movie::find($movieId);
|
|
|
|
if ($movie->whereHas('genres', function ($query) use ($genre) {
|
|
$query->where('tmdb_id', '=', $genre->tmdb_id);
|
|
})->exists()) {
|
|
return;
|
|
} else {
|
|
$movie->genres()->attach($genre);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function removeElementFromRemainingJobs(int $tmdbId, string $job)
|
|
{
|
|
$itemToFetchJobs = ItemToFetchJobs::where('tmdb_id', $tmdbId)->first();
|
|
|
|
$remainingJobs = $itemToFetchJobs->remaining_jobs;
|
|
$remainingJobKey = array_search($job, $remainingJobs);
|
|
|
|
unset($remainingJobs[$remainingJobKey]);
|
|
|
|
if (empty($remainingJobs)) {
|
|
$itemToFetchJobs->delete();
|
|
}
|
|
|
|
$itemToFetchJobs->remaining_jobs = $remainingJobs;
|
|
$itemToFetchJobs->save();
|
|
}
|
|
|
|
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()) {
|
|
return;
|
|
} else {
|
|
$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()) {
|
|
return;
|
|
} else {
|
|
$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'],
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|