moviesApi = $moviesApi; $this->personApi = $personApi; $this->listApi = $listApi; } public static function getBatch(int $batch, string $job) { return ItemToFetchJobs::leftJoin('movies', function ($join) { $join->on('movies.tmdb_id', '=', 'item_to_fetch_jobs.tmdb_id'); }) ->whereRaw("FIND_IN_SET('".$job."', item_to_fetch_jobs.jobs)") ->where('item_to_fetch_jobs.type', 'movie') ->whereNotNull('movies.id') ->take($batch) ->get(); } public function process($item, $job) { $methodName = "get".ucfirst($job); if (!method_exists($this, $methodName)) { // @todo maybe raise an exception here return; } echo "Processing {$methodName} ".$item->tmdb_id." (".$item->id.") \n"; $this->$methodName($item); // maybe we should sleep for a while so we do not spam the tmdb api sleep(1); } private function getDetails($item) { $tmdbMovie = $this->moviesApi->getMovie($item->tmdb_id); if (isset($tmdbMovie['status_code']) && $tmdbMovie['status_code'] == 34) { echo "[ApiError-Could not be found]\n]"; return; } $tmdbMovie['tmdb_id'] = $tmdbMovie['id']; Log::channel('fetch_export_items_details')->info("Processing TmdbId ".$tmdbMovie['tmdb_id']); $tmdbMovie['slug'] = Str::slug($tmdbMovie['title'].'-'.$tmdbMovie['tmdb_id']); $tmdbMovie['release_date'] = $tmdbMovie['release_date'] ? new \DateTime($tmdbMovie['release_date']) : null; $movie = Movie::where('tmdb_id', $tmdbMovie['tmdb_id'])->first(); if(!$movie) { $movie = Movie::create($tmdbMovie); } $movieId = $movie->id; // $movieId = Movie::create($tmdbMovie); if (!empty($tmdbMovie['belongs_to_collection'])) { Log::channel('fetch_export_items_details')->info("Processing collections for ".$tmdbMovie['tmdb_id']); $this->processMovieCollection($movieId, $tmdbMovie['belongs_to_collection']); } if (!empty($tmdbMovie['genres'])) { Log::channel('fetch_export_items_details')->info("Processing genres for ".$tmdbMovie['tmdb_id']); $this->processMovieGenres($movieId, $tmdbMovie['genres']); } if (!empty($tmdbMovie['production_companies'])) { Log::channel('fetch_export_items_details')->info("Processing production companies ".$tmdbMovie['tmdb_id']); $this->processProductionCompanies($movieId, $tmdbMovie['production_companies']); } if (!empty($tmdbMovie['production_countries'])) { Log::channel('fetch_export_items_details')->info("Processing production countries ".$tmdbMovie['tmdb_id']); $this->processProductionCountries($movieId, $tmdbMovie['production_countries']); } $this->removeElementFromRemainingJobs($tmdbMovie['id'], 'Details'); } private function getAlternativeTitles($item) { $alternativeTitles = $this->moviesApi->getAlternativeTitles($item->tmdb_id); Log::channel('fetch_export_items_alternative_titles')->info("Processing TmdbId ".$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) { echo "Credits for TMDBID ".$item->tmdb_id." \n"; Log::channel('fetch_export_items_credits')->info("Credits for TMDBID ".$item->tmdb_id); $tmdbCredits = $this->moviesApi->getCredits($item->tmdb_id); if (empty($tmdbCredits)) { echo "Empty for ".$item->tmdb_id." \n"; return; } $movie = Movie::where('tmdb_id', $item->tmdb_id)->first(); if (null === $movie) { echo "No movie for TMDB id ".$item->tmdb_id."\n"; } Log::channel('fetch_export_items_credits')->info("Processing cast for TMDBID ".$item->tmdb_id); $this->processCast($movie, $tmdbCredits['cast'] ?? []); Log::channel('fetch_export_items_credits')->info("Processing crew for TMDBID ".$item->tmdb_id); $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'])){ echo "Nope"; 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(); // @todo create user $user = User::where('name', $tmdbReview['author'])->first(); if (!$user) { echo "Create user ".$tmdbReview['author']; $user = User::create([ 'name' => $tmdbReview['author'], 'slug' => Str::slug($tmdbReview['author']), 'email' => $tmdbReview['author'].'@nowhere.wtf', 'password' => 'no_password_neeeded_here', 'is_dummy_account' => true, ]); } if (!$existingReview) { echo "Create comment by user ".$tmdbReview['author']." on movie ".$movie->title; Comment::create([ 'entity_id' => $movie->id, 'entity_type' => Comment::TYPE_MOVIE, 'user_id' => $user->id, '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'] ? new \DateTime($tmdbReview['created_at']) : null, '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(); if (!$movie) { return; } $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']); $externalIds['entity_id'] = $movie->id; $externalIds['entity_type'] = ExternalId::TYPE_MOVIE; 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(); if (!$movie) { $itemToFetch = $this->createJob($item->tmdb_id); $this->getDetails($itemToFetch); return; } $types = [ 'flatrate', 'rent', 'buy', ]; foreach ($tmdbWatchProviders['results'] as $locale => $tmdbWatchProvider) { foreach ($types as $type) { if (!isset($tmdbWatchProvider[$type])) { continue; } $providerInfo = $tmdbWatchProvider[$type]; $providerInfo = reset($providerInfo); if (!isset($providerInfo['provider_id'])) { continue; } $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()) { $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'] ? new \DateTime($tmdbVideo['published_at']) : null, '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 ($listDetails['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 getRecommendations($item) { $tmdbRecommendations = $this->moviesApi->getRecommendations($item->tmdb_id); if (empty($tmdbRecommendations) || empty($tmdbRecommendations['results'])) { return; } $initialMovie = Movie::where('tmdb_id', $item->tmdb_id)->first(); foreach ($tmdbRecommendations['results'] as $tmdbRecommendation) { $itemToFetch = ItemToFetch::where('tmdb_id', $tmdbRecommendation['id'])->where('type', ItemToFetch::TYPE_MOVIE)->first(); if ($itemToFetch) { $this->getDetails($itemToFetch); continue; } $movie = Movie::where('tmdb_id', $tmdbRecommendation['id'])->first(); if ($movie) { $recommendationLink = MovieRecommendation::where('movie_id', $initialMovie->id) ->where('recommended_movie_id', $movie->id) ->first(); if (!$recommendationLink) { MovieRecommendation::create([ 'movie_id' => $initialMovie->id, 'recommended_movie_id' => $movie->id, ]); } continue; } else { $itemToFetch = ItemToFetch::create([ 'tmdb_id' => $tmdbRecommendation['id'], 'type' => ItemToFetch::TYPE_MOVIE, ]); $jobs = ItemToFetchJobs::JOBS; $jobs[] = "Details"; ItemToFetchJobs::create([ 'tmdb_id' => $tmdbRecommendation['id'], 'type' => ItemToFetch::TYPE_MOVIE, 'remaining_jobs' => implode(',', $jobs), 'jobs' => implode(',', $jobs), ]); $this->getDetails($itemToFetch); } // $movie = Movie::where('tmdb_id', $item->tmdb_id)->first(); $recommendedMovie = Movie::where('tmdb_id', $tmdbRecommendation['id'])->first(); MovieRecommendation::create([ 'movie_id' => $initialMovie->id, 'recommended_movie_id' => $recommendedMovie->id, ]); } $this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_RECOMMNDATION); } private function getSimilar($item) { $tmdbSimilarMovies = $this->moviesApi->getSimilar($item->tmdb_id); if (empty($tmdbSimilarMovies) || empty($tmdbSimilarMovies['results'])) { return; } $initialMovie = Movie::where('tmdb_id', $item->tmdb_id)->first(); foreach ($tmdbSimilarMovies['results'] as $tmdbSimilarMovie) { $itemToFetch = ItemToFetch::where('tmdb_id', $tmdbSimilarMovie['id'])->where('type', ItemToFetch::TYPE_MOVIE)->first(); if ($itemToFetch) { $this->getDetails($itemToFetch); continue; } $similarMovie = Movie::where('tmdb_id', $tmdbSimilarMovie['id'])->first(); if ($similarMovie) { $similarLink = MovieSimilar::where('movie_id', $initialMovie->id) ->where('similar_movie_id', $similarMovie->id) ->first(); if (!$similarLink) { MovieSimilar::create([ 'movie_id' => $initialMovie->id, 'similar_movie_id' => $similarMovie->id, ]); } continue; } $itemToFetch = ItemToFetch::create([ 'tmdb_id' => $tmdbSimilarMovie['id'], 'type' => ItemToFetch::TYPE_MOVIE, ]); $jobs = ItemToFetchJobs::JOBS; $jobs[] = "Details"; ItemToFetchJobs::create([ 'tmdb_id' => $tmdbSimilarMovie['id'], 'type' => ItemToFetch::TYPE_MOVIE, 'remaining_jobs' => implode(',', $jobs), 'jobs' => implode(',', $jobs), ]); $this->getDetails($itemToFetch); $movie = Movie::where('tmdb_id', $item->tmdb_id)->first(); $similarMovie = Movie::where('tmdb_id', $tmdbSimilarMovie['id'])->first(); MovieSimilar::create([ 'movie_id' => $movie->id, 'similar_movie_id' => $similarMovie->id, ]); } $this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_SIMILAR); } private function processMovieCollection(int $movieId, $belongsToCollection) { dd($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(); if (!$itemToFetchJobs) { return; } $remainingJobs = $itemToFetchJobs->jobs; if (is_string($remainingJobs)) { $remainingJobs = explode(',', $remainingJobs); } else { dd($remainingJobs); return; } $remainingJobKey = array_search($job, $remainingJobs); unset($remainingJobs[$remainingJobKey]); if (empty($remainingJobs)) { $itemToFetchJobs->delete(); } // $itemToFetchJobs->remaining_jobs = $remainingJobs; $itemToFetchJobs->jobs = implode(',', $remainingJobs); $itemToFetchJobs->save(); } private function processCast($movie, array $castList) { if (empty($castList) || $movie === null) { 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)) { $knownForDepartment = $tmdbPerson['known_for_department'] ?? null; if ($knownForDepartment) { $knownForDepartment = json_encode($knownForDepartment); } $personData = array_merge($personData, [ 'biography' => $tmdbPerson['biography'], 'birtday' => $tmdbPerson['birthday'], 'deathday' => $tmdbPerson['deathday'], 'homepage' => $tmdbPerson['homepage'], 'imdb_id' => $tmdbPerson['imdb_id'], 'known_for_department' => $knownForDepartment, '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('person_id', '=', $person->id); })->exists()) { $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) || $movie === null) { 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('person_id', '=', $person->id); })->exists()) { $movie->crewList()->attach($person, [ 'known_for_department' => json_encode($crewPerson['known_for_department']), 'job' => $crewPerson['job'], 'department' => $crewPerson['department'], 'tmdb_cast_id' => $crewPerson['credit_id'], ]); } } } private function processProductionCompanies(int $movieId, array $productionCompanies) { $movie = Movie::find($movieId); foreach ($productionCompanies as $productionCompany) { $existingProductionCompany = ProductionCompany::where('tmdb_id', $productionCompany['id'])->first(); if (!$existingProductionCompany) { $existingProductionCompany = ProductionCompany::create([ 'tmdb_id' => $productionCompany['id'], 'logo_path' => $productionCompany['logo_path'], 'name' => $productionCompany['name'], 'origin_country' => $productionCompany['origin_country'], ]); } if ($movie->whereHas('productionCompanies', function ($query) use ($existingProductionCompany) { $query->where('production_company_id', '=', $existingProductionCompany->id); })->exists()) { return; } else { $movie->productionCompanies()->attach($existingProductionCompany); } } } private function processProductionCountries(int $movieId, array $productionCountries) { $movie = Movie::find($movieId); foreach ($productionCountries as $productionCountry) { $existingProductionCountry = ProductionCountry::where('iso_3166', $productionCountry['iso_3166_1'])->first(); if (!$existingProductionCountry) { $existingProductionCountry = ProductionCountry::create([ 'iso_3166' => $productionCountry['iso_3166_1'], 'name' => $productionCountry['name'], ]); } if ($movie->whereHas('productionCountries', function ($query) use ($existingProductionCountry) { $query->where('production_country_id', '=', $existingProductionCountry->id); })->exists()) { return; } else { $movie->productionCountries()->attach($existingProductionCountry); } } } private function createJob($tmdbId) { $itemToFetch = ItemToFetch::where('tmdb_id', $tmdbId)->where('type', ItemToFetch::TYPE_MOVIE)->first(); if ($itemToFetch) { return $itemToFetch; } $movie = Movie::where('tmdb_id', $tmdbId)->first(); if ($movie) { return; } $itemToFetch = ItemToFetch::create([ 'tmdb_id' => $tmdbId, 'type' => ItemToFetch::TYPE_MOVIE, ]); $jobs = ItemToFetchJobs::JOBS; $jobs[] = "Details"; ItemToFetchJobs::create([ 'tmdb_id' => $tmdbId, 'type' => ItemToFetch::TYPE_MOVIE, 'remaining_jobs' => json_encode($jobs), 'jobs' => implode(',', $jobs), ]); return $itemToFetch; } }