*/ public function middleware(): array { return [ Skip::when(cache()->has("tmdb-movie-scraper:{$this->id}")), new WithoutOverlapping((string) $this->id)->dontRelease()->expireAfter(30), new RateLimited(GlobalRateLimit::TMDB), ]; } /** * Determine the time at which the job should timeout. */ public function retryUntil(): DateTime { return now()->addDay(); } public function handle(): void { // Movie $movieScraper = new Client\Movie($this->id); if ($movieScraper->getMovie() === null) { return; } $movie = TmdbMovie::updateOrCreate(['id' => $this->id], $movieScraper->getMovie()); // Genres TmdbGenre::upsert($movieScraper->getGenres(), 'id'); $movie->genres()->sync(array_unique(array_column($movieScraper->getGenres(), 'id'))); // Companies $companies = []; foreach ($movieScraper->data['production_companies'] ?? [] as $company) { $companies[] = (new Client\Company($company['id']))->getCompany(); } TmdbCompany::upsert($companies, 'id'); $movie->companies()->sync(array_unique(array_column($companies, 'id'))); // Collection if ($movieScraper->data['belongs_to_collection'] !== null) { $collection = (new Client\Collection($movieScraper->data['belongs_to_collection']['id']))->getCollection(); TmdbCollection::upsert($collection, 'id'); $movie->collections()->sync([$collection['id']]); } // People $credits = $movieScraper->getCredits(); $people = []; $cache = []; foreach (array_unique(array_column($credits, 'tmdb_person_id')) as $personId) { // TMDB caches their api responses for 8 hours, so don't abuse them $cacheKey = "tmdb-person-scraper:{$personId}"; if (cache()->has($cacheKey)) { continue; } $people[] = (new Client\Person($personId))->getPerson(); $cache[$cacheKey] = now(); } TmdbPerson::upsert($people, 'id'); if ($cache !== []) { cache()->put($cache, 8 * 3600); } TmdbCredit::where('tmdb_movie_id', '=', $this->id)->delete(); TmdbCredit::upsert($credits, ['tmdb_person_id', 'tmdb_movie_id', 'tmdb_tv_id', 'occupation_id', 'character']); // Recommendations $movie->recommendedMovies()->sync(array_unique(array_column($movieScraper->getRecommendations(), 'recommended_tmdb_movie_id'))); Torrent::query() ->where('tmdb_movie_id', '=', $this->id) ->whereRelation('category', 'movie_meta', '=', true) ->searchable(); // TMDB caches their api responses for 8 hours, so don't abuse them cache()->put("tmdb-movie-scraper:{$this->id}", now(), 8 * 3600); } }