update: modularize tmdb service arrays and add types

This commit is contained in:
Roardom
2023-09-04 03:37:27 +00:00
parent 9c0426d4ca
commit a837f59eae
10 changed files with 1234 additions and 348 deletions
+18 -95
View File
@@ -13,7 +13,6 @@
namespace App\Jobs;
use App\Enums\Occupations;
use App\Models\Collection;
use App\Models\Company;
use App\Models\Credit;
@@ -22,7 +21,6 @@ use App\Models\Movie;
use App\Models\Person;
use App\Models\Recommendation;
use App\Services\Tmdb\Client;
use App\Services\Tmdb\TMDB;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@@ -39,70 +37,40 @@ class ProcessMovieJob implements ShouldQueue
/**
* ProcessMovieJob constructor.
*/
public function __construct(public $movie)
public function __construct(public int $id)
{
}
public function handle(): void
{
$tmdb = new TMDB();
$movie = Movie::find((int) $this->movie['id']);
$movie = Movie::find($this->id);
// Movie
$movieScraper = new Client\Movie($this->id);
Movie::updateOrCreate(['id' => $this->id], $movieScraper->getMovie());
// Genres
$genres = [];
$genre_ids = [];
foreach ($this->movie['genres'] as $genre) {
$genre_ids[] = $genre['id'];
$genres[] = [
'id' => $genre['id'],
'name' => $genre['name']
];
}
Genre::upsert($genres, 'id');
$movie->genres()->sync(array_unique($genre_ids));
Genre::upsert($movieScraper->getGenres(), 'id');
$movie->genres()->sync(array_unique(array_column($movieScraper->getGenres(), 'id')));
// Companies
$companies = [];
$company_ids = [];
foreach ($this->movie['production_companies'] ?? [] as $company) {
$company = (new Client\Company($company['id']))->getData();
$company_ids[] = $company['id'];
$companies[] = [
'id' => $company['id'],
'description' => $company['description'] ?? null,
'headquarters' => $company['headquarters'] ?? null,
'homepage' => $company['homepage'] ?? null,
'logo' => $tmdb->image('logo', $company),
'name' => $company['name'] ?? null,
'origin_country' => $company['origin_country'],
];
$companies[] = (new Client\Company($company['id']))->getCompany();
}
Company::upsert($companies, 'id');
$movie->companies()->sync(array_unique($company_ids));
$movie->companies()->sync(array_unique(array_column($companies, 'id')));
// Collection
if (isset($this->movie['belongs_to_collection']['id'])) {
$collection = (new Client\Collection($this->movie['belongs_to_collection']['id']))->getData();
$titleSort = addslashes(str_replace(['The ', 'An ', 'A ', '"'], [''], (string) $collection['name']));
$collection = [
'id' => $collection['id'],
'name' => $collection['name'] ?? null,
'name_sort' => $titleSort,
'parts' => is_countable($collection['parts']) ? \count($collection['parts']) : 0,
'overview' => $collection['overview'] ?? null,
'poster' => $tmdb->image('poster', $collection),
'backdrop' => $tmdb->image('backdrop', $collection),
];
$collection = (new Client\Collection($this->movie['belongs_to_collection']['id']))->getCollection();
Collection::upsert($collection, 'id');
$movie->collection()->sync([$collection['id']]);
@@ -110,64 +78,19 @@ class ProcessMovieJob implements ShouldQueue
// People
$people_ids = [];
$credits = [];
foreach ($this->movie['credits']['cast'] ?? [] as $person) {
$credits[] = [
'movie_id' => $this->movie['id'],
'person_id' => $person['id'],
'occupation_id' => Occupations::ACTOR->value,
'character' => $person['character'] ?? '',
'order' => $person['order'] ?? null
];
$people_ids[] = $person['id'];
}
foreach ($this->movie['credits']['crew'] ?? [] as $person) {
$job = Occupations::from_tmdb_job($person['job']);
if ($job !== null) {
$credits[] = [
'movie_id' => $this->movie['id'],
'person_id' => $person['id'],
'occupation_id' => $job->value,
'character' => null,
'order' => null
];
$people_ids[] = $person['id'];
}
}
$credits = $movieScraper->getCredits();
$people = [];
foreach (array_unique($people_ids) as $person_id) {
$person = (new Client\Person($person_id))->getData();
$people[] = $tmdb->person_array($person);
foreach (array_unique(array_column($credits, 'person_id')) as $person_id) {
$people[] = (new Client\Person($person_id))->getPerson();
}
Person::upsert($people, 'id');
Credit::where('movie_id', '=', $this->movie['id'])->delete();
Credit::where('movie_id', '=', $this->id)->delete();
Credit::upsert($credits, ['person_id', 'movie_id', 'tv_id', 'occupation_id', 'character']);
// Recommendations
$movie_ids = Movie::select('id')->findMany(array_column($this->movie['recommendations']['results'] ?? [], 'id'))->pluck('id');
$recommendations = [];
foreach ($this->movie['recommendations']['results'] ?? [] as $recommendation) {
if ($movie_ids->contains($recommendation['id'])) {
$recommendations[] = [
'recommendation_movie_id' => $recommendation['id'],
'movie_id' => $this->movie['id'],
'title' => $recommendation['title'],
'vote_average' => $recommendation['vote_average'],
'poster' => $tmdb->image('poster', $recommendation),
'release_date' => $recommendation['release_date'],
];
}
}
Recommendation::upsert($recommendations, ['recommendation_movie_id', 'movie_id']);
Recommendation::upsert($movieScraper->getRecommendations(), ['recommendation_movie_id', 'movie_id']);
}
}
+23 -145
View File
@@ -14,7 +14,6 @@
namespace App\Jobs;
use App\Enums\Occupations;
use App\Models\Company;
use App\Models\Credit;
use App\Models\Episode;
@@ -25,13 +24,11 @@ use App\Models\Recommendation;
use App\Models\Season;
use App\Models\Tv;
use App\Services\Tmdb\Client;
use App\Services\Tmdb\TMDB;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Str;
class ProcessTvJob implements ShouldQueue
{
@@ -43,137 +40,58 @@ class ProcessTvJob implements ShouldQueue
/**
* ProcessTvJob Constructor.
*/
public function __construct(public $tv, public $id)
public function __construct(public $id)
{
}
public function handle(): void
{
$tmdb = new TMDB();
$tv = Tv::find((int) $this->tv['id']);
$tv = Tv::find($this->id);
// Tv
$tvScraper = new Client\TV($this->id);
Tv::updateOrCreate(['id' => $this->id], $tvScraper->getTv());
// Companies
$companies = [];
$company_ids = [];
foreach ($this->tv['production_companies'] ?? [] as $company) {
$companies[] = [
'id' => $company['id'],
'description' => $tmdb->ifExists('description', $company),
'name' => $company['name'],
'headquarters' => $tmdb->ifExists('headquarters', $company),
'homepage' => $tmdb->ifExists('homepage', $company),
'logo' => $tmdb->image('logo', $company),
'origin_country' => $tmdb->ifExists('origin_country', $company),
];
$company_ids[] = $company['id'];
$companies[] = (new Client\Company($company['id']))->getCompany();
}
Company::upsert($companies, 'id');
$tv->companies()->sync(array_unique($company_ids));
$tv->companies()->sync(array_unique(array_column($companies, 'id')));
// Networks
$networks = [];
$network_ids = [];
foreach ($this->tv['networks'] ?? [] as $network) {
$network = (new Client\Network($network['id']))->getData();
if (isset($network['id'], $network['name'])) {
if (isset($network['images']['logos'][0]) && \array_key_exists('file_path', $network['images']['logos'][0])) {
$logo = 'https://image.tmdb.org/t/p/original'.$network['images']['logos'][0]['file_path'];
} else {
$logo = null;
}
$networks[] = [
'id' => $network['id'],
'headquarters' => $tmdb->ifExists('headquarters', $network),
'homepage' => $tmdb->ifExists('homepage', $network),
'logo' => $logo,
'name' => $network['name'],
'origin_country' => $network['origin_country'],
];
$network_ids[] = $network['id'];
}
$networks[] = (new Client\Network($network['id']))->getNetwork();
}
Network::upsert($networks, 'id');
$tv->networks()->sync(array_unique($network_ids));
$tv->networks()->sync(array_unique(array_column($networks, 'id')));
// Genres
$genres = [];
$genre_ids = [];
foreach ($this->tv['genres'] as $genre) {
$genres[] = [
'id' => $genre['id'],
'name' => $genre['name']
];
$genre_ids[] = $genre['id'];
}
Genre::upsert($genres, 'id');
$tv->genres()->sync(array_unique($genre_ids));
Genre::upsert($tvScraper->getGenres(), 'id');
$tv->genres()->sync(array_unique(array_column($tvScraper->getGenres(), 'id')));
// People
$credits = [];
$people_ids = [];
foreach ($this->tv['aggregate_credits']['cast'] ?? [] as $person) {
foreach ($person['roles'] as $role) {
$credits[] = [
'tv_id' => $this->tv['id'],
'person_id' => $person['id'],
'occupation_id' => Occupations::ACTOR->value,
'character' => $role['character'] ?? '',
'order' => $person['order'] ?? null
];
$people_ids[] = $person['id'];
}
}
foreach ($this->tv['aggregate_credits']['crew'] ?? [] as $person) {
foreach ($person['jobs'] as $job) {
$occupation = Occupations::from_tmdb_job($job['job']);
if ($occupation !== null) {
$credits[] = [
'tv_id' => $this->tv['id'],
'person_id' => $person['id'],
'occupation_id' => $occupation->value,
'character' => null,
'order' => null,
];
$people_ids[] = $person['id'];
}
}
}
foreach ($this->tv['created_by'] ?? [] as $person) {
$credits[] = [
'tv_id' => $this->tv['id'],
'person_id' => $person['id'],
'occupation_id' => Occupations::CREATOR->value,
'character' => null,
'order' => null,
];
$people_ids[] = $person['id'];
}
$credits = $tvScraper->getCredits();
$people = [];
foreach (array_unique($people_ids) as $person_id) {
$person = (new Client\Person($person_id))->getData();
$people[] = $tmdb->person_array($person);
foreach (array_unique(array_column($credits, 'person_id')) as $person_id) {
$people[] = (new Client\Person($person_id))->getPerson();
}
Person::upsert($people, 'id');
Credit::where('tv_id', '=', $this->tv['id'])->delete();
Credit::where('tv_id', '=', $this->id)->delete();
Credit::upsert($credits, ['person_id', 'movie_id', 'tv_id', 'occupation_id', 'character']);
// Seasons and episodes
@@ -181,35 +99,11 @@ class ProcessTvJob implements ShouldQueue
$seasons = [];
$episodes = [];
foreach ($this->tv['seasons'] as $season) {
$season = (new Client\Season($this->id, $season['season_number']))->getData();
foreach ($tvScraper->getSeasons() as $season) {
$seasonScraper = new Client\Season($this->id, $season['season_number']);
$seasons[] = [
'id' => $season['id'],
'air_date' => $tmdb->ifExists('air_date', $season),
'poster' => $tmdb->image('poster', $season),
'name' => $tmdb->ifExists('name', $season),
'overview' => $tmdb->ifExists('overview', $season),
'season_number' => $season['season_number'],
'tv_id' => $this->id,
];
foreach ($season['episodes'] as $episode) {
$episodes[] = [
'id' => $episode['id'],
'tv_id' => $this->id,
'air_date' => $tmdb->ifExists('air_date', $episode),
'name' => Str::limit($tmdb->ifExists('name', $episode), 200),
'episode_number' => $episode['episode_number'],
'overview' => $tmdb->ifExists('overview', $episode),
'still' => $tmdb->image('still', $episode),
'production_code' => $episode['production_code'],
'season_number' => $episode['season_number'],
'vote_average' => $episode['vote_average'],
'vote_count' => $episode['vote_count'],
'season_id' => $season['id'],
];
}
$seasons[] = $seasonScraper->getSeason();
array_push($episodes, ...$seasonScraper->getEpisodes());
}
Season::upsert($seasons, 'id');
@@ -217,22 +111,6 @@ class ProcessTvJob implements ShouldQueue
// Recommendations
$tv_ids = Tv::select('id')->findMany(array_column($this->tv['recommendations']['results'] ?? [], 'id'))->pluck('id');
$recommendations = [];
foreach ($this->tv['recommendations']['results'] ?? [] as $recommendation) {
if ($tv_ids->contains($recommendation['id'])) {
$recommendations[] = [
'recommendation_tv_id' => $recommendation['id'],
'tv_id' => $this->tv['id'],
'title' => $recommendation['name'],
'vote_average' => $recommendation['vote_average'],
'poster' => $tmdb->image('poster', $recommendation),
'first_air_date' => $recommendation['first_air_date'],
];
}
}
Recommendation::upsert($recommendations, ['recommendation_tv_id', 'tv_id']);
Recommendation::upsert($tvScraper->getRecommendations(), ['recommendation_tv_id', 'tv_id']);
}
}
+58 -1
View File
@@ -13,13 +13,44 @@
namespace App\Services\Tmdb\Client;
use App\Services\Tmdb\TMDB;
use Illuminate\Support\Facades\Http;
class Collection
{
/** @var array<mixed>|mixed */
/**
* @var array{
* id: ?int,
* name: ?string,
* overview: ?string,
* poster_path: ?string,
* backdrop_path: ?string,
* parts: ?array<
* int<0, max>,
* array{
* adult: ?boolean,
* backdrop_path: ?string,
* id: ?int,
* title: ?string,
* original_language: ?string,
* original_title: ?string,
* overview: ?string,
* poster_path: ?string,
* media_type: ?string,
* genre_ids: array<int>,
* popularity: ?float,
* release_date: ?string,
* video: ?boolean,
* vote_average: ?float,
* vote_count: ?int,
* },
* >,
* }
*/
public mixed $data;
public TMDB $tmdb;
public function __construct(int $id)
{
$this->data = Http::acceptJson()
@@ -30,10 +61,36 @@ class Collection
'append_to_response' => 'videos,images,credits',
])
->json();
$this->tmdb = new TMDB();
}
public function getData(): mixed
{
return $this->data;
}
/**
* @return array{
* id: int,
* name: ?string,
* name_sort: string,
* parts: int,
* overview: ?string,
* poster: ?string,
* backdrop: ?string,
* }
*/
public function getCollection(): array
{
return [
'id' => $this->data['id'],
'name' => $this->data['name'] ?? null,
'name_sort' => addslashes(str_replace(['The ', 'An ', 'A ', '"'], [''], $this->data['name'])),
'parts' => is_countable($this->data['parts']) ? \count($this->data['parts']) : 0,
'overview' => $this->data['overview'] ?? null,
'poster' => $this->tmdb->image('poster', $this->data),
'backdrop' => $this->tmdb->image('backdrop', $this->data),
];
}
}
+39 -1
View File
@@ -13,13 +13,27 @@
namespace App\Services\Tmdb\Client;
use App\Services\Tmdb\TMDB;
use Illuminate\Support\Facades\Http;
class Company
{
/** @var array<mixed>|mixed */
/**
* @var array{
* description: ?string,
* headquarters: ?string,
* homepage: ?string,
* id: ?int,
* logo_path: ?string,
* name: ?string,
* origin_country: ?string,
* parent_company: ?string,
* }
*/
public mixed $data;
public TMDB $tmdb;
public function __construct(int $id)
{
$this->data = Http::acceptJson()
@@ -36,4 +50,28 @@ class Company
{
return $this->data;
}
/**
* @return array{
* id: ?int,
* description: ?string,
* headquarters: ?string,
* homepage: ?string,
* logo: ?string,
* name: ?string,
* origin_country: string,
* }
*/
public function getCompany(): array
{
return [
'id' => $this->data['id'],
'description' => $this->data['description'] ?? null,
'headquarters' => $this->data['headquarters'] ?? null,
'homepage' => $this->data['homepage'] ?? null,
'logo' => $this->tmdb->image('logo', $this->data),
'name' => $this->data['name'] ?? null,
'origin_country' => $this->data['origin_country'],
];
}
}
+372 -3
View File
@@ -14,12 +14,216 @@
namespace App\Services\Tmdb\Client;
use JsonException;
use App\Enums\Occupations;
use App\Services\Tmdb\TMDB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use DateTime;
class Movie
{
/** @var array<mixed>|mixed */
public mixed $data;
/**
* @var null|array{
* adult: ?bool,
* backdrop_path: ?string,
* belongs_to_collection: ?string,
* budget: ?int,
* genres: ?array<
* int<0, max>,
* array{
* id: ?int,
* name: ?string,
* },
* >,
* homepage: ?string,
* id: ?int,
* imdb_id: ?string,
* original_language: ?string,
* original_title: ?string,
* overview: ?string,
* popularity: ?float,
* poster_path: ?string,
* production_companies: ?array<
* int,
* array{
* id: int,
* logo_path: ?string,
* name: ?string,
* origin_country: ?string,
* },
* >,
* production_countries: ?array<
* int<0, max>,
* array{
* iso_3166_1: ?string,
* name: ?string,
* },
* >,
* release_date: ?string,
* revenue: ?int,
* runtime: ?int,
* spoken_languages: ?array<
* int<0, max>,
* array{
* english_name: ?string,
* iso_639_1: ?string,
* name: ?string,
* },
* >,
* status: ?string,
* tagline: ?string,
* title: ?string,
* vote_average: ?float,
* vote_count: ?int,
* credits: ?array{
* id: ?int,
* cast: ?array<
* int<0, max>,
* array{
* adult: ?bool,
* gender: ?int,
* id: ?int,
* known_for_department: ?string,
* name: ?string,
* original_name: ?string,
* popularity: ?float,
* profile_path: ?string,
* cast_id: ?int,
* character: ?string,
* credit_id: ?string,
* order: ?int,
* },
* >,
* crew: ?array<
* int<0, max>,
* array{
* adult: ?bool,
* gender: ?int,
* id: ?int,
* known_for_department: ?string,
* name: ?string,
* original_name: ?string,
* popularity: ?float,
* profile_path: ?string,
* credit_id: ?string,
* department: ?string,
* job: ?string,
* },
* >,
* },
* videos: ?array{
* id: ?int,
* results: ?array<
* int<0, max>,
* ?array{
* iso_639_1: ?string,
* iso_3166_1: ?string,
* name: ?string,
* key: ?string,
* site: ?string,
* size: ?string,
* type: ?string,
* official: ?bool,
* published_at: ?string,
* id: ?string,
* },
* >,
* },
* images: ?array{
* backdrops: ?array<
* int<0, max>,
* array{
* aspect_ratio: ?float,
* height: ?int,
* iso_639_1: ?string,
* file_path: ?string,
* vote_average: ?float,
* vote_count: ?int,
* width: ?int,
* },
* >,
* id: ?int,
* logos: ?array<
* int<0, max>,
* array{
* aspect_ratio: ?float,
* height: ?int,
* iso_639_1: ?string,
* file_path: ?string,
* vote_average: ?float,
* vote_count: ?int,
* width: ?int,
* },
* >,
* posters: ?array<
* int<0, max>,
* array{
* aspect_ratio: ?float,
* height: ?int,
* iso_639_1: ?string,
* file_path: ?string,
* vote_average: ?float,
* vote_count: ?int,
* width: ?int,
* },
* >,
* },
* external_ids: ?array{
* id: ?int,
* imdb_id: ?string,
* wikidata_id: ?string,
* facebook_id: ?string,
* instagram_id: ?string,
* twitter_id: ?string,
* },
* keywords: ?array{
* id: ?int,
* keywords: ?array<
* int<0, max>,
* ?array{
* id: ?int,
* name: ?string,
* },
* >,
* },
* recommendations: ?array{
* page: ?int,
* results: ?array{
* adult: ?boolean,
* backdrop_path: ?string,
* id: ?int,
* title: ?string,
* original_language: ?string,
* original_name: ?string,
* overview: ?string,
* poster_path: ?string,
* media_type: ?string,
* genre_ids: ?array<int>,
* popularity: ?float,
* first_air_date: ?string,
* vote_average: ?float,
* vote_count: ?int,
* origin_country: ?array<string>,
* },
* total_pages: ?int,
* total_results: ?int,
* },
* alternative_titles: ?array{
* id: ?int,
* results: ?array<
* int<0, max>,
* array{
* iso_3166_1: ?string,
* title: ?string,
* type: ?string,
* },
* >,
* }
* }
*/
public null|array $data;
public TMDB $tmdb;
/**
* @throws \GuzzleHttp\Exception\GuzzleException
@@ -35,13 +239,178 @@ class Movie
'append_to_response' => 'videos,images,credits,external_ids,keywords,recommendations,alternative_titles',
])
->json();
$this->tmdb = new TMDB();
}
public function getData(): mixed
public function getData(): ?array
{
return $this->data;
}
/**
* @return null|array{
* adult: bool,
* backdrop: string,
* budget: int,
* homepage: string,
* imdb_id: string,
* original_language: string,
* original_title: string,
* overview: string,
* popularity: float,
* poster: string,
* release_date: string,
* revenue: int,
* runtime: int,
* status: string,
* tagline: string,
* title: string,
* title_sort: string,
* vote_average: float,
* vote_count: int,
* }
*/
public function getMovie(): ?array
{
if (\array_key_exists('title', $this->data)) {
$re = '/((?<namesort>.*)(?<seperator>\:|and)(?<remaining>.*)|(?<name>.*))/m';
preg_match($re, (string) $this->data['title'], $matches);
$year = (new DateTime($this->data['release_date']))->format('Y');
$titleSort = addslashes(str_replace(
['The ', 'An ', 'A ', '"'],
[''],
Str::limit($matches['namesort'] ? $matches['namesort'].' '.$year : $this->data['title'], 100)
));
return [
'adult' => $this->data['adult'] ?? 0,
'backdrop' => $this->tmdb->image('backdrop', $this->data),
'budget' => $this->data['budget'] ?? null,
'homepage' => $this->data['homepage'] ?? null,
'imdb_id' => substr($this->data['imdb_id'] ?? '', 2),
'original_language' => $this->data['original_language'] ?? null,
'original_title' => $this->data['original_title'] ?? null,
'overview' => $this->data['overview'] ?? null,
'popularity' => $this->data['popularity'] ?? null,
'poster' => $this->tmdb->image('poster', $this->data),
'release_date' => $this->tmdb->ifExists('release_date', $this->data),
'revenue' => $this->data['revenue'] ?? null,
'runtime' => $this->data['runtime'] ?? null,
'status' => $this->data['status'] ?? null,
'tagline' => $this->data['tagline'] ?? null,
'title' => Str::limit($this->data['title'], 200),
'title_sort' => $titleSort,
'vote_average' => $this->data['vote_average'] ?? null,
'vote_count' => $this->data['vote_count'] ?? null,
];
}
return null;
}
/**
* @return array<int, array{
* id: int,
* name: string,
* }>
*/
public function getGenres(): array
{
$genres = [];
foreach ($this->data['genres'] as $genre) {
$genres[] = [
'id' => $genre['id'],
'name' => $genre['name'],
];
}
return $genres;
}
/**
* @return array<
* int<0, max>,
* array{
* movie_id: int,
* person_id: int,
* occupation_id: int,
* character: ?string,
* order: ?int,
* },
* >
*/
public function getCredits(): array
{
$credits = [];
foreach ($this->data['credits']['cast'] ?? [] as $person) {
$credits[] = [
'movie_id' => $this->data['id'],
'person_id' => $person['id'],
'occupation_id' => Occupations::ACTOR->value,
'character' => $person['character'] ?? '',
'order' => $person['order'] ?? null
];
}
foreach ($this->data['credits']['crew'] ?? [] as $person) {
$job = Occupations::from_tmdb_job($person['job']);
if ($job !== null) {
$credits[] = [
'movie_id' => $this->data['id'],
'person_id' => $person['id'],
'occupation_id' => $job->value,
'character' => null,
'order' => null
];
}
}
return $credits;
}
/**
* @return array<
* int<0, max>,
* array{
* recommendation_movie_id: int,
* movie_id: int,
* title: string,
* vote_average: float,
* poster: string,
* release_date: string,
* }
* >
*/
public function getRecommendations(): array
{
$movie_ids = \App\Models\Movie::query()
->select('id')
->findMany(array_column($this->data['recommendations']['results'] ?? [], 'id'))
->pluck('id');
$recommendations = [];
foreach ($this->data['recommendations']['results'] ?? [] as $recommendation) {
if ($movie_ids->contains($recommendation['id'])) {
$recommendations[] = [
'recommendation_movie_id' => $recommendation['id'],
'movie_id' => $this->data['id'],
'title' => $recommendation['title'],
'vote_average' => $recommendation['vote_average'],
'poster' => $this->tmdb->image('poster', $recommendation),
'release_date' => $recommendation['release_date'],
];
}
}
return $recommendations;
}
public function get_trailer(): ?string
{
if (!empty($this->data['videos']['results'])) {
+55 -1
View File
@@ -13,13 +13,40 @@
namespace App\Services\Tmdb\Client;
use App\Services\Tmdb\TMDB;
use Illuminate\Support\Facades\Http;
class Network
{
/** @var array<mixed>|mixed */
/**
* @var array{
* headquarters: ?string,
* homepage: ?string,
* id: ?int,
* logo_path: ?string,
* name: ?string,
* origin_country: ?string,
* images: ?array{
* id: ?int,
* logos: ?array<
* int<0, max>,
* array{
* aspect_ratio: ?float,
* file_path: ?string,
* height: ?int,
* id: ?string,
* file_type: ?string,
* vote_average: ?float,
* vote_count: ?int,
* },
* >,
* }
* }
*/
public mixed $data;
public TMDB $tmdb;
public function __construct(int $id)
{
$this->data = Http::acceptJson()
@@ -30,10 +57,37 @@ class Network
'append_to_response' => 'images',
])
->json();
$this->tmdb = new TMDB();
}
public function getData(): mixed
{
return $this->data;
}
/**
* @return array
*/
public function getNetwork(): ?array
{
if (isset($this->data['id'], $this->data['name'])) {
if (isset($this->data['images']['logos'][0]) && \array_key_exists('file_path', $this->data['images']['logos'][0])) {
$logo = 'https://image.tmdb.org/t/p/original'.$this->data['images']['logos'][0]['file_path'];
} else {
$logo = null;
}
return [
'id' => $this->data['id'],
'headquarters' => $this->tmdb->ifExists('headquarters', $this->data),
'homepage' => $this->tmdb->ifExists('homepage', $this->data),
'logo' => $logo,
'name' => $this->data['name'],
'origin_country' => $this->data['origin_country'],
];
}
return null;
}
}
+60 -2
View File
@@ -13,12 +13,32 @@
namespace App\Services\Tmdb\Client;
use App\Services\Tmdb\TMDB;
use Illuminate\Support\Facades\Http;
class Person
{
/** @var array<mixed>|mixed */
public mixed $data;
/**
* @var array{
* adult: ?boolean,
* also_known_as: ?array<string>,
* biography: ?string,
* birthday: ?string,
* deathday: ?string,
* gender: ?int,
* homepage: ?string,
* id: ?int,
* imdb_id: ?string,
* known_for_department: ?string,
* name: ?string,
* place_of_birth: ?string,
* popularity: ?float,
* profile_path: ?string,
* }
*/
public array $data;
public TMDB $tmdb;
public function __construct(int $id)
{
@@ -30,10 +50,48 @@ class Person
'append_to_response' => 'images,credits',
])
->json();
$this->tmdb = new TMDB();
}
public function getData(): mixed
{
return $this->data;
}
/**
* @return array{
* id: ?int,
* birthday: ?string,
* known_for_department: ?string,
* deathday: ?string,
* name: ?string,
* gender: ?int,
* biography: ?string,
* popularity: ?float,
* place_of_birth: ?string,
* still: ?string,
* adult: ?bool,
* imdb_id: ?string,
* homepage: ?string,
* }
*/
public function getPerson(): array
{
return [
'id' => $this->data['id'] ?? null,
'birthday' => $this->data['birthday'] ?? null,
'known_for_department' => $this->data['known_for_department'] ?? null,
'deathday' => $this->data['deathday'] ?? null,
'name' => $this->data['name'] ?? null,
'gender' => $this->data['gender'] ?? null,
'biography' => $this->data['biography'] ?? null,
'popularity' => $this->data['popularity'] ?? null,
'place_of_birth' => $this->data['place_of_birth'] ?? null,
'still' => $this->tmdb->image('profile', $this->data),
'adult' => $this->data['adult'] ?? null,
'imdb_id' => $this->data['imdb_id'] ?? null,
'homepage' => $this->data['homepage'] ?? null,
];
}
}
+133 -4
View File
@@ -13,27 +13,156 @@
namespace App\Services\Tmdb\Client;
use App\Services\Tmdb\TMDB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
class Season
{
/** @var array<mixed>|mixed */
public mixed $data;
/**
* @var array{
* _id: ?string,
* air_date: ?string,
* episodes: ?array<
* int<0, max>,
* array{
* air_date: ?string,
* episode_number: ?int,
* id: ?int,
* name: ?string,
* overview: ?string,
* production_code: ?string,
* runtime: ?int,
* season_number: ?int,
* show_id: ?int,
* still_path: ?string,
* vote_average: ?float,
* vote_count: ?int,
* crew: ?array<
* int<0, max>,
* array{
* department: ?string,
* job: ?string,
* credit_id: ?string,
* adult: ?bool,
* gender: ?int,
* id: ?int,
* known_for_department: ?string,
* name: ?string,
* original_name: ?string,
* popularity: ?float,
* profile_path: ?string,
* },
* >,
* guest_stars: ?array<
* int<0, max>,
* array{
* character: ?string,
* credit_id: ?string,
* order: ?string,
* adult: ?bool,
* gender: ?int,
* id: ?int,
* known_for_department: ?string,
* name: ?string,
* original_name: ?string,
* popularity: ?float,
* profile_path: ?string,
* },
* >,
* },
* >,
* name: ?string,
* overview: ?string,
* id: ?int,
* poster_path: ?string,
* season_number: ?int,
* vote_average: ?float,
* }
*/
public array $data;
public function __construct(int $id, int $seasonId)
public TMDB $tmdb;
public function __construct(public int $tvId, public int $seasonId)
{
$this->data = Http::acceptJson()
->withUrlParameters(['tvId' => $id, 'seasonId' => $seasonId])
->withUrlParameters(['tvId' => $tvId, 'seasonId' => $seasonId])
->get('https://api.TheMovieDB.org/3/tv/{tvId}/{seasonId}', [
'api_key' => config('api-keys.tmdb'),
'language' => config('app.meta_locale'),
'append_to_response' => 'videos,images,credits,external_ids',
])
->json();
$this->tmdb = new TMDB();
}
public function getData(): mixed
{
return $this->data;
}
/**
* @return array{
* id: int,
* air_date: ?string,
* poster: ?string,
* name: ?string,
* overview: ?string,
* season_number: int,
* tv_id: int,
* }
*/
public function getSeason(): array
{
return [
'id' => $this->seasonId,
'air_date' => $this->data['air_date'] ?? null,
'poster' => $this->tmdb->image('poster', $this->data),
'name' => $this->data['name'] ?? null,
'overview' => $this->data['overview'] ?? null,
'season_number' => $this->data['season_number'],
'tv_id' => $this->tvId,
];
}
/**
* @return array{
* id: int,
* tv_id: int,
* air_date: ?string,
* name: ?string,
* episode_number: ?int,
* overview: ?string,
* still: ?string,
* production_code: ?string,
* season_number: ?int,
* vote_average: ?float,
* vote_count: ?int,
* season_id: int,
*/
public function getEpisodes(): array
{
$episodes = [];
foreach ($this->data['episodes'] as $episode) {
$episodes[] = [
'id' => $episode['id'],
'tv_id' => $this->tvId,
'air_date' => $this->tmdb->ifExists('air_date', $episode),
'name' => Str::limit($this->tmdb->ifExists('name', $episode), 200),
'episode_number' => $episode['episode_number'],
'overview' => $this->tmdb->ifExists('overview', $episode),
'still' => $this->tmdb->image('still', $episode),
'production_code' => $episode['production_code'],
'season_number' => $episode['season_number'],
'vote_average' => $episode['vote_average'],
'vote_count' => $episode['vote_count'],
'season_id' => $this->seasonId,
];
}
return $episodes;
}
}
+471 -2
View File
@@ -14,12 +14,282 @@
namespace App\Services\Tmdb\Client;
use JsonException;
use App\Enums\Occupations;
use App\Services\Tmdb\TMDB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
class TV
{
/** @var array<mixed>|mixed */
public mixed $data;
/**
* @var array{
* adult: ?bool,
* backdrop_path: ?string,
* created_by: ?array<
* int<0, max>,
* array{
* id: ?int,
* credit_id: ?string,
* name: ?string,
* gender: ?int,
* profile_path: ?string,
* },
* >,
* episode_run_time: ?array<int>,
* first_air_date: ?string,
* genres: ?array<
* int<0, max>,
* array{
* id: ?int,
* name: ?string,
* },
* >,
* homepage: ?string,
* id: ?int,
* in_production: ?bool,
* languages: ?array<string>,
* last_air_date: ?string,
* last_episode_to_air: ?array{
* id: ?int,
* name: ?string,
* overview: ?string,
* vote_average: ?float,
* vote_count: ?int,
* air_date: ?string,
* episode_number: ?int,
* production_code: ?string,
* runtime: ?int,
* season_number: ?int,
* show_id: ?int,
* still_path: ?string,
* },
* name: ?string,
* next_episode_to_air: ?string,
* networks: ?array<
* int<0, max>,
* array{
* id: ?int,
* logo_path: ?string,
* name: ?string,
* origin_country: ?string,
* },
* >,
* number_of_episodes: ?int,
* number_of_seasons: ?int,
* origin_country: ?array<string>,
* original_language: ?string,
* original_name: ?string,
* overview: ?string,
* popularity: ?float,
* poster_path: ?string,
* production_companies: ?array<
* int,
* array{
* id: int,
* logo_path: ?string,
* name: ?string,
* origin_country: ?string,
* },
* >,
* production_countries: ?array<
* int<0, max>,
* array{
* iso_3166_1: ?string,
* name: ?string,
* },
* >,
* seasons: ?array<
* int<0, max>,
* array{
* air_date: ?string,
* episode_count: ?int,
* id: ?int,
* name: ?string,
* overview: ?string,
* poster_path: ?string,
* season_number: ?int,
* vote_average: ?int,
* }
* >,
* spoken_languages: ?array<
* int<0, max>,
* array{
* english_name: ?string,
* iso_639_1: ?string,
* name: ?string,
* },
* >,
* status: ?string,
* tagline: ?string,
* type: ?string,
* vote_average: ?float,
* vote_count: ?int,
* aggregate_credits: ?array{
* cast: ?array<
* int<0, max>,
* array{
* adult: ?bool,
* gender: ?int,
* id: ?int,
* known_for_department: ?string,
* name: ?string,
* original_name: ?string,
* popularity: ?float,
* profile_path: ?string,
* roles: ?array<
* int<0, max>,
* array{
* credit_id: ?string,
* character: ?string,
* episode_count: ?int,
* },
* >,
* total_episode_count: ?int,
* order: ?int,
* },
* >,
* crew: ?array<
* int<0, max>,
* array{
* adult: ?bool,
* gender: ?int,
* id: ?int,
* known_for_department: ?string,
* name: ?string,
* original_name: ?string,
* popularity: ?float,
* profile_path: ?string,
* jobs: ?array<
* int<0, max>,
* array{
* credit_id: ?string,
* job: ?string,
* episode_count: ?int,
* },
* >,
* department: ?string,
* total_episode_count: ?int,
* },
* >,
* id: ?int,
* },
* videos: ?array{
* id: ?int,
* results: ?array<
* int<0, max>,
* ?array{
* iso_639_1: ?string,
* iso_3166_1: ?string,
* name: ?string,
* key: ?string,
* site: ?string,
* size: ?string,
* type: ?string,
* official: ?bool,
* published_at: ?string,
* id: ?string,
* },
* >,
* },
* images: ?array{
* backdrops: ?array<
* int<0, max>,
* array{
* aspect_ratio: ?float,
* height: ?int,
* iso_639_1: ?string,
* file_path: ?string,
* vote_average: ?float,
* vote_count: ?int,
* width: ?int,
* },
* >,
* id: ?int,
* logos: ?array<
* int<0, max>,
* array{
* aspect_ratio: ?float,
* height: ?int,
* iso_639_1: ?string,
* file_path: ?string,
* vote_average: ?float,
* vote_count: ?int,
* width: ?int,
* },
* >,
* posters: ?array<
* int<0, max>,
* array{
* aspect_ratio: ?float,
* height: ?int,
* iso_639_1: ?string,
* file_path: ?string,
* vote_average: ?float,
* vote_count: ?int,
* width: ?int,
* },
* >,
* },
* external_ids: ?array{
* id: ?int,
* imdb_id: ?string,
* freebase_mid: ?string,
* freebase_id: ?string,
* tvdb_id: ?int,
* tvrage_id: ?int,
* wikidata_id: ?string,
* facebook_id: ?string,
* instagram_id: ?string,
* twitter_id: ?string,
* },
* keywords: ?array{
* id: ?int,
* results: ?array<
* int<0, max>,
* ?array{
* id: ?int,
* name: ?string,
* },
* >,
* },
* recommendations: ?array{
* page: ?int,
* results: ?array{
* adult: ?boolean,
* backdrop_path: ?string,
* id: ?int,
* name: ?string,
* original_language: ?string,
* original_name: ?string,
* overview: ?string,
* poster_path: ?string,
* media_type: ?string,
* genre_ids: ?array<int>,
* popularity: ?float,
* first_air_date: ?string,
* vote_average: ?float,
* vote_count: ?int,
* origin_country: ?array<string>,
* },
* total_pages: ?int,
* total_results: ?int,
* },
* alternative_titles: ?array{
* id: ?int,
* results: ?array<
* int<0, max>,
* array{
* iso_3166_1: ?string,
* title: ?string,
* type: ?string,
* },
* >,
* }
* }
*/
public array $data;
public TMDB $tmdb;
/**
* @throws \GuzzleHttp\Exception\GuzzleException
@@ -35,6 +305,8 @@ class TV
'append_to_response' => 'videos,images,aggregate_credits,external_ids,keywords,recommendations,alternative_titles',
])
->json();
$this->tmdb = new TMDB();
}
public function getData(): mixed
@@ -42,6 +314,203 @@ class TV
return $this->data;
}
/**
* @return ?array{
* backdrop: ?string,
* episode_run_time: ?array<int>,
* first_air_date: ?string,
* homepage: ?string,
* imdb_id: string,
* tvdb_id: string,
* in_production: ?bool,
* last_air_date: ?string,
* name: string,
* name_sort: string,
* number_of_episodes: ?int,
* number_of_seasons: ?int,
* origin_country: ?array<string>,
* original_language: ?string,
* original_name: ?string,
* overview: ?string,
* popularity: ?float,
* poster: ?string,
* status: ?string,
* vote_average: ?float,
* vote_count: ?int,
* }
*/
public function getTv(): ?array
{
if (isset($this->data['id'])) {
return [
'backdrop' => $this->tmdb->image('backdrop', $this->data),
'episode_run_time' => $this->tmdb->ifHasItems('episode_run_time', $this->data),
'first_air_date' => $this->tmdb->ifExists('first_air_date', $this->data),
'homepage' => $this->data['homepage'],
'imdb_id' => substr($this->data['external_ids']['imdb_id'] ?? '', 2),
'tvdb_id' => $this->data['external_ids']['tvdb_id'] ?? '',
'in_production' => $this->data['in_production'],
'last_air_date' => $this->data['last_air_date'],
'name' => Str::limit($this->data['name'], 200),
'name_sort' => addslashes(str_replace(['The ', 'An ', 'A ', '"'], [''], Str::limit($this->data['name'], 100))),
'number_of_episodes' => $this->data['number_of_episodes'],
'number_of_seasons' => $this->data['number_of_seasons'],
'origin_country' => $this->tmdb->ifHasItems('origin_country', $this->data),
'original_language' => $this->data['original_language'],
'original_name' => $this->data['original_name'],
'overview' => $this->data['overview'],
'popularity' => $this->data['popularity'],
'poster' => $this->tmdb->image('poster', $this->data),
'status' => $this->data['status'],
'vote_average' => $this->data['vote_average'],
'vote_count' => $this->data['vote_count'],
];
}
return null;
}
/**
* @return array<int, array{
* id: int,
* name: string,
* }>
*/
public function getGenres(): array
{
$genres = [];
foreach ($this->data['genres'] as $genre) {
$genres[] = [
'id' => $genre['id'],
'name' => $genre['name'],
];
}
return $genres;
}
/**
* @return array<
* int<0, max>,
* array{
* tv_id: int,
* person_id: int,
* occupation_id: int,
* character: ?string,
* order: ?int,
* },
* >
*/
public function getCredits(): array
{
$credits = [];
foreach ($this->data['aggregate_credits']['cast'] ?? [] as $person) {
foreach ($person['roles'] as $role) {
$credits[] = [
'tv_id' => $this->data['id'],
'person_id' => $person['id'],
'occupation_id' => Occupations::ACTOR->value,
'character' => $role['character'] ?? '',
'order' => $person['order'] ?? null
];
}
}
foreach ($this->data['aggregate_credits']['crew'] ?? [] as $person) {
foreach ($person['jobs'] as $job) {
$occupation = Occupations::from_tmdb_job($job['job']);
if ($occupation !== null) {
$credits[] = [
'tv_id' => $this->data['id'],
'person_id' => $person['id'],
'occupation_id' => $occupation->value,
'character' => null,
'order' => null,
];
}
}
}
foreach ($this->data['created_by'] ?? [] as $person) {
$credits[] = [
'tv_id' => $this->data['id'],
'person_id' => $person['id'],
'occupation_id' => Occupations::CREATOR->value,
'character' => null,
'order' => null,
];
}
return $credits;
}
/**
* @return array<
* int<0, max>,
* array{
* id: int,
* season_number: int,
* },
* >
*/
public function getSeasons(): array
{
$seasons = [];
foreach ($this->data['seasons'] as $season) {
if ($season['season_number'] !== null) {
$seasons[] = [
'id' => $season['id'],
'season_number' => $season['season_number'],
];
}
}
return $seasons;
}
/**
* @return array<
* int<0, max>,
* array{
* recommendation_tv_id: int,
* tv_id: int,
* title: string,
* vote_average: float,
* poster: string,
* release_date: string,
* }
* >
*/
public function getRecommendations(): array
{
$tv_ids = \App\Models\Tv::query()
->select('id')
->findMany(array_column($this->data['recommendations']['results'] ?? [], 'id'))
->pluck('id');
$recommendations = [];
foreach ($this->data['recommendations']['results'] ?? [] as $recommendation) {
if ($tv_ids->contains($recommendation['id'])) {
$recommendations[] = [
'recommendation_tv_id' => $recommendation['id'],
'tv_id' => $this->data['id'],
'title' => $recommendation['title'],
'vote_average' => $recommendation['vote_average'],
'poster' => $this->tmdb->image('poster', $recommendation),
'release_date' => $recommendation['release_date'],
'first_air_date' => $recommendation['first_air_date'],
];
}
}
return $recommendations;
}
public function get_trailer(): ?string
{
if (!empty($this->data['videos']['results'])) {
+5 -94
View File
@@ -15,22 +15,15 @@ namespace App\Services\Tmdb;
use App\Jobs\ProcessMovieJob;
use App\Jobs\ProcessTvJob;
use App\Models\Movie;
use App\Models\Tv;
use DateTime;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Http\Request;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Str;
class TMDBScraper implements ShouldQueue
{
use SerializesModels;
/**
* @var mixed|array|string|null
*/
public $id;
public int $id;
public function __construct(Request $request = null)
{
@@ -39,95 +32,13 @@ class TMDBScraper implements ShouldQueue
}
}
public function tv($id = null): void
public function tv(int $id): void
{
if ($id == null) {
$id = $this->id;
}
$tmdb = new TMDB();
$tv = (new Client\TV($id))->getData();
if (isset($tv['id'])) {
$array = [
'backdrop' => $tmdb->image('backdrop', $tv),
'episode_run_time' => $tmdb->ifHasItems('episode_run_time', $tv),
'first_air_date' => $tmdb->ifExists('first_air_date', $tv),
'homepage' => $tv['homepage'],
'imdb_id' => substr($tv['external_ids']['imdb_id'] ?? '', 2),
'tvdb_id' => $tv['external_ids']['tvdb_id'] ?? '',
'in_production' => $tv['in_production'],
'last_air_date' => $tmdb->ifExists('last_air_date', $tv),
'name' => Str::limit($tv['name'], 200),
'name_sort' => addslashes(str_replace(['The ', 'An ', 'A ', '"'], [''], Str::limit($tv['name'], 100))),
'number_of_episodes' => $tv['number_of_episodes'],
'number_of_seasons' => $tv['number_of_seasons'],
'origin_country' => $tmdb->ifHasItems('origin_country', $tv),
'original_language' => $tv['original_language'],
'original_name' => $tv['original_name'],
'overview' => $tv['overview'],
'popularity' => $tv['popularity'],
'poster' => $tmdb->image('poster', $tv),
'status' => $tv['status'],
'vote_average' => $tv['vote_average'],
'vote_count' => $tv['vote_count'],
];
Tv::updateOrCreate(['id' => $id], $array);
ProcessTvJob::dispatch($tv, $id);
//return ['message' => 'Tv with id: ' . $id . ' Has been added to the database, But episodes are loaded with the queue'];
}
ProcessTvJob::dispatch($id);
}
public function movie($id = null): void
public function movie(int $id): void
{
if ($id == null) {
$id = $this->id;
}
$tmdb = new TMDB();
$movie = (new Client\Movie($id))->getData();
if (\array_key_exists('title', $movie)) {
$re = '/((?<namesort>.*)(?<seperator>\:|and)(?<remaining>.*)|(?<name>.*))/m';
preg_match($re, (string) $movie['title'], $matches);
$year = (new DateTime($movie['release_date']))->format('Y');
$titleSort = addslashes(str_replace(
['The ', 'An ', 'A ', '"'],
[''],
Str::limit($matches['namesort'] ? $matches['namesort'].' '.$year : $movie['title'], 100)
));
$array = [
'adult' => $movie['adult'] ?? 0,
'backdrop' => $tmdb->image('backdrop', $movie),
'budget' => $movie['budget'] ?? null,
'homepage' => $movie['homepage'] ?? null,
'imdb_id' => substr($movie['imdb_id'] ?? '', 2),
'original_language' => $movie['original_language'] ?? null,
'original_title' => $movie['original_title'] ?? null,
'overview' => $movie['overview'] ?? null,
'popularity' => $movie['popularity'] ?? null,
'poster' => $tmdb->image('poster', $movie),
'release_date' => $tmdb->ifExists('release_date', $movie),
'revenue' => $movie['revenue'] ?? null,
'runtime' => $movie['runtime'] ?? null,
'status' => $movie['status'] ?? null,
'tagline' => $movie['tagline'] ?? null,
'title' => Str::limit($movie['title'], 200),
'title_sort' => $titleSort,
'vote_average' => $movie['vote_average'] ?? null,
'vote_count' => $movie['vote_count'] ?? null,
];
Movie::updateOrCreate(['id' => $movie['id']], $array);
ProcessMovieJob::dispatch($movie);
//return ['message' => 'Movies with id: ' . $id . ' Has been added to the database, But relations are loaded with the queue'];
}
ProcessMovieJob::dispatch($id);
}
}