Files
movies/app/Service/MediaProcessing/ShowProcessingService.php
T

406 lines
14 KiB
PHP
Raw Normal View History

2024-12-30 01:51:28 +02:00
<?php
namespace App\Service\MediaProcessing;
2025-01-04 03:22:13 +02:00
use App\Models\Episode;
2025-01-07 23:39:22 +02:00
use App\Models\ExternalId;
use App\Models\Image;
2025-01-04 04:02:10 +02:00
use App\Models\ItemToFetchJobs;
use App\Models\Person;
2025-01-04 03:22:13 +02:00
use App\Models\Season;
use App\Models\Show;
2025-01-04 04:02:10 +02:00
use App\Models\ShowAlternativeTitle;
2025-01-07 23:39:22 +02:00
use App\Models\Video;
2025-01-04 04:02:10 +02:00
use App\Service\TMDBApi\PersonApi;
2025-01-04 03:22:13 +02:00
use App\Service\TMDBApi\ShowsApi;
use Illuminate\Support\Str;
use function PHPUnit\Framework\containsIdentical;
2024-12-30 01:51:28 +02:00
class ShowProcessingService implements ProcessingInterface
{
2025-01-04 03:22:13 +02:00
private ShowsApi $showsApi;
2025-01-04 04:02:10 +02:00
private PersonApi $personApi;
2025-01-04 03:22:13 +02:00
2025-01-04 04:02:10 +02:00
public function __construct(
ShowsApi $showsApi,
PersonApi $personApi
) {
2025-01-04 03:22:13 +02:00
$this->showsApi = $showsApi;
2025-01-04 04:02:10 +02:00
$this->personApi = $personApi;
2025-01-04 03:22:13 +02:00
}
2024-12-30 01:51:28 +02:00
public function process($item, $job)
{
2025-01-04 03:22:13 +02:00
$methodName = "get".ucfirst($job);
if (!method_exists($this, $methodName)) {
// @todo maybe raise an exception here
return;
}
$this->$methodName($item);
2024-12-30 01:51:28 +02:00
}
2024-12-31 03:39:06 +02:00
2025-01-04 03:22:13 +02:00
private function getDetails($item)
{
$tmdbShow = $this->showsApi->getTvshow($item->tmdb_id);
$tmdbShow['tmdb_id'] = $tmdbShow['id'];
2024-12-31 03:39:06 +02:00
2025-01-04 03:22:13 +02:00
$show = Show::where('tmdb_id', $item->tmdb_id)->first();
if ($show) {
return;
}
2025-01-07 23:39:22 +02:00
//dd($tmdbShow);
2025-01-04 03:22:13 +02:00
$tmdbShow['slug'] = Str::slug($tmdbShow['name'].'-'.$tmdbShow['tmdb_id']);
$tmdbShow['first_air_date'] = $tmdbShow['first_air_date'] ? new \DateTime($tmdbShow['first_air_date']) : null;
$tmdbShow['last_air_date'] = $tmdbShow['last_air_date'] ? new \DateTime($tmdbShow['last_air_date']) : null;
2025-01-07 23:39:22 +02:00
$tmdbShow['episode_run_time'] = reset($tmdbShow['episode_run_time']);
$tmdbShow['languages'] = json_encode($tmdbShow['languages']);
$tmdbShow['origin_country'] = reset($tmdbShow['origin_country']);
2025-01-04 03:22:13 +02:00
$show = Show::create($tmdbShow);
if (!empty($tmdbShow['seasons'])) {
$this->processSeasons($show, $tmdbShow['seasons']);
}
2025-01-07 23:39:22 +02:00
// @todo process genres
// @todo process networks
// @todo process production countries
// @todo process production companies
2025-01-04 04:02:10 +02:00
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_DETAILS);
2025-01-04 03:22:13 +02:00
}
2025-01-04 04:02:10 +02:00
private function getAlternativeTitles($item)
2025-01-04 03:22:13 +02:00
{
2025-01-04 04:02:10 +02:00
$tmdbAlternativeTitles = $this->showsApi->getAlternativeTitles($item->tmdb_id);
2025-01-04 03:22:13 +02:00
2025-01-04 04:02:10 +02:00
if (empty($tmdbAlternativeTitles) ||empty($tmdbAlternativeTitles['results'])) {
return;
}
$show = Show::where('tmdb_id', $item->tmdb_id)->first();
foreach ($tmdbAlternativeTitles['results'] as $tmdbAlternativeTitle) {
$alternativeTitle = ShowAlternativeTitle::where('show_id', $show->id)
->where('iso', $tmdbAlternativeTitle['iso_3166_1'])
->first();
if (!$alternativeTitle) {
ShowAlternativeTitle::create([
'iso' => $tmdbAlternativeTitle['iso_3166_1'],
'title' => $tmdbAlternativeTitle['title'],
'show_id' => $show->id,
'type' => $tmdbAlternativeTitle['type'],
]);
}
}
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_ALTERNVATIVE_TITLES);
}
private function getCredits($item)
{
$tmdbCredits = $this->showsApi->getCredits($item->tmdb_id);
if (empty($tmdbCredits)) {
return;
}
$show = Show::where('tmdb_id', $item->tmdb_id)->first();
$this->processCast($show, $tmdbCredits['cast'] ?? []);
$this->processCrew($show, $tmdbCredits['crew'] ?? []);
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_CREDITS);
2025-01-04 03:22:13 +02:00
}
2025-01-07 23:39:22 +02:00
private function getExternalIds($item)
{
$tmdbExternalIds = $this->showsApi->getExternalIds($item->tmdb_id);
if (empty($tmdbExternalIds)) {
return;
}
$show = Show::where('tmdb_id', $item->tmdb_id)->first();
if (!$show) {
return;
}
$externalId = ExternalId::where('entity_id', $show->id)
->where('entity_type', ExternalId::TYPE_SHOW)
->first();
if (!$externalId) {
$externalIds['tmdb_id'] = $tmdbExternalIds['id'];
//$externalIds['show_id'] = $show->id;
unset($externalIds['id']);
$externalIds['entity_id'] = $show->id;
$externalIds['entity_type'] = ExternalId::TYPE_SHOW;
ExternalId::create($externalIds);
}
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_EXTERNAL_IDS);
}
private function getImages($item)
{
$tmdbImages = $this->showsApi->getImages($item->tmdb_id);
if (empty($tmdbImages)) {
return;
}
$show = Show::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 ($show->whereHas('images', function ($query) use ($imageExists) {
$query->where('type', $imageExists->type)
->where('file_path', $imageExists->file_path);
})->exists()) {
return;
} else {
$show->images()->attach($imageExists);
}
}
}
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_IMAGES);
}
private function getVideos($item)
{
$show = Show::where('tmdb_id', $item->tmdb_id)->first();
$tmdbVideos = $this->showsApi->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 ($show->whereHas('videos', function ($query) use ($show) {
$query->where('show_id', $show->id);
})->exists()) {
return;
} else {
$show->videos()->attach($videoExists);
}
}
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_VIDEOS);
}
2025-01-04 03:22:13 +02:00
private function processSeasons(Show $show, array $seasons)
{
foreach ($seasons as $season) {
$showSeason = Season::where('tmdb_id', $season['id'])
->where('show_id', $show->id)
->first();
if (!$showSeason) {
2025-01-07 23:39:22 +02:00
$season['show_id'] = $show->id;
2025-01-04 03:22:13 +02:00
$showSeason = Season::create($season);
$tmdbSeasonDetails = $this->showsApi->getSeasonDetails($show->tmdb_id, $season['season_number']);
$this->processEpisodes($show, $showSeason, $tmdbSeasonDetails['episodes'] ?? []);
}
}
}
private function processEpisodes($show, $season, array $tmdbEpisodes)
{
if (empty($tmdbEpisodes)) {
return;
}
foreach ($tmdbEpisodes as $tmdbEpisode) {
$episode = Episode::where('show_id', $show->id)
->where('season_id', $season->id)
->where('tmdb_id', $tmdbEpisode['id'])
->first();
if (!$episode) {
$tmdbEpisode['tmdb_id'] = $tmdbEpisode['id'];
$tmdbEpisode['slug'] = Str::slug($tmdbEpisode['name']);
$tmdbEpisode['tmdb_show_id'] = $tmdbEpisode['show_id'];
2025-01-07 23:39:22 +02:00
$tmdbEpisode['season_id'] = $season->id;
// $tmdbEpisode['crew'] = $tmdbEpisode['crew'];
// $tmdbEpisode['guest_stars'] = $tmdbEpisode['guest_stars'];
2025-01-04 03:22:13 +02:00
$tmdbEpisode['show_id'] = $show->id;
$tmdbEpisode['tmdb_id'] = $season->id;
Episode::create($tmdbEpisode);
}
}
}
2025-01-04 04:02:10 +02:00
private function removeElementFromRemainingJobs(int $tmdbId, string $job)
{
$itemToFetchJobs = ItemToFetchJobs::where('tmdb_id', $tmdbId)->first();
$remainingJobs = $itemToFetchJobs->remaining_jobs;
if (is_string($remainingJobs)) {
$remainingJobs = json_decode($remainingJobs, true);
}
$remainingJobKey = array_search($job, $remainingJobs);
unset($remainingJobs[$remainingJobKey]);
if (empty($remainingJobs)) {
$itemToFetchJobs->delete();
}
$itemToFetchJobs->remaining_jobs = $remainingJobs;
$itemToFetchJobs->save();
}
private function processCast($show, array $credits)
{
if (empty($credits)) {
return;
}
foreach ($credits 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 ($show->whereHas('castList', function ($query) use ($person) {
$query->where('tmdb_id', '=', $person->tmdb_id);
})->exists()) {
return;
} else {
$show->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($show, array $credits)
{
if (empty($credits)) {
return;
}
foreach ($credits 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 ($show->whereHas('crewList', function ($query) use ($person) {
$query->where('tmdb_id', '=', $person->tmdb_id);
})->exists()) {
return;
} else {
$show->crewList()->attach($person, [
'known_for_department' => json_encode($crewPerson['known_for_department']),
'job' => $crewPerson['job'],
'department' => $crewPerson['department'],
'tmdb_cast_id' => $crewPerson['credit_id'],
]);
}
}
}
2024-12-30 01:51:28 +02:00
}