This commit is contained in:
2025-02-10 01:58:12 +02:00
parent 25d99e7b72
commit 87bbfc33d3
242 changed files with 38662 additions and 99 deletions
@@ -26,6 +26,7 @@ use App\Models\Video;
use App\Models\WatchProvider;
use App\Service\TMDBApi\PersonApi;
use App\Service\TMDBApi\ShowsApi;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use function PHPUnit\Framework\containsIdentical;
@@ -42,13 +43,40 @@ class ShowProcessingService implements ProcessingInterface
$this->personApi = $personApi;
}
public static function getBatch(int $batch, string $job, int $tmdbId = null)
{
$query = ItemToFetchJobs::leftJoin('shows', function ($join) {
$join->on('shows.tmdb_id', '=', 'item_to_fetch_jobs.tmdb_id');
})
->select('item_to_fetch_jobs.*');
if ($tmdbId) {
$query = $query->where('item_to_fetch_jobs.tmdb_id', $tmdbId);
}
if ($job === ItemToFetchJobs::JOB_DETAILS) {
return $query
->whereRaw("FIND_IN_SET('".ItemToFetchJobs::JOB_DETAILS."', item_to_fetch_jobs.jobs)")
->where('item_to_fetch_jobs.type', 'tv_series')
->whereNull('shows.id')
->take($batch)
->get();
}
return $query
->whereRaw("FIND_IN_SET('".$job."', item_to_fetch_jobs.jobs)")
->where('item_to_fetch_jobs.type', 'tv_series')
->whereNotNull('shows.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;
throw new \Exception("No method named $methodName\n");
}
$this->$methodName($item);
@@ -62,10 +90,10 @@ class ShowProcessingService implements ProcessingInterface
$show = Show::where('tmdb_id', $item->tmdb_id)->first();
if ($show) {
echo "+";
// echo "+";
return;
}
echo ".";
// echo ".";
$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;
@@ -140,11 +168,13 @@ class ShowProcessingService implements ProcessingInterface
$tmdbCredits = $this->showsApi->getCredits($item->tmdb_id);
if (empty($tmdbCredits)) {
echo "no credits\n";
return;
}
$show = Show::where('tmdb_id', $item->tmdb_id)->first();
echo $item->tmdb_id."\n";
$this->processCast($show, $tmdbCredits['cast'] ?? []);
$this->processCrew($show, $tmdbCredits['crew'] ?? []);
@@ -177,7 +207,27 @@ class ShowProcessingService implements ProcessingInterface
$externalIds['entity_id'] = $show->id;
$externalIds['entity_type'] = ExternalId::TYPE_SHOW;
$externalId['imdb_id'] = $tmdbExternalIds['imdb_id'] ?? null;
$externalId['wikidata_id'] = $tmdbExternalIds['wikidata_id'] ?? null;
$externalId['facebook_id'] = $tmdbExternalIds['facebook_id'] ?? null;
$externalId['instagram_id'] = $tmdbExternalIds['instagram_id'] ?? null;
$externalId['twitter_id'] = $tmdbExternalIds['twitter_id'] ?? null;
$externalId['tvdb_id'] = $tmdbExternalIds['tvdb_id'] ?? null;
$externalId['freebase_id'] = $tmdbExternalIds['freebase_id'] ?? null;
$externalId['tvrage_id'] = $tmdbExternalIds['tvrage_id'] ?? null;
ExternalId::create($externalIds);
} else {
$externalId->imdb_id = $tmdbExternalIds['imdb_id'] ?? null;
$externalId->wikidata_id = $tmdbExternalIds['wikidata_id'] ?? null;
$externalId->facebook_id = $tmdbExternalIds['facebook_id'] ?? null;
$externalId->instagram_id = $tmdbExternalIds['instagram_id'] ?? null;
$externalId->twitter_id = $tmdbExternalIds['twitter_id'] ?? null;
$externalId->tvdb_id = $tmdbExternalIds['tvdb_id'] ?? null;
$externalId->freebase_id = $tmdbExternalIds['freebase_id'] ?? null;
$externalId->tvrage_id = $tmdbExternalIds['tvrage_id'] ?? null;
$externalId->save();
}
$this->removeElementFromRemainingJobs($item->tmdb_id, ItemToFetchJobs::JOB_EXTERNAL_IDS);
@@ -250,12 +300,17 @@ class ShowProcessingService implements ProcessingInterface
]);
}
if ($show->whereHas('videos', function ($query) use ($show) {
$query->where('show_id', $show->id);
})->exists()) {
return;
} else {
$show->videos()->attach($videoExists);
$videoIsAttached = DB::table('show_videos')
->where('show_id', $show->id)
->where('video_id', $videoExists->id)
->first();
if (!$videoIsAttached) {
DB::table('show_videos')
->insert([
'show_id' => $show->id,
'video_id' => $videoExists->id
]);
}
}
@@ -282,12 +337,17 @@ class ShowProcessingService implements ProcessingInterface
]);
}
if ($show->whereHas('keywords', function ($query) use ($existingKeyword) {
$query->where('tmdb_id', $existingKeyword->tmdb_id);
})->exists()) {
return;
} else {
$show->keywords()->attach($existingKeyword);
$keywordIsAttached = DB::table('show_keywords')
->where('show_id', $show->id)
->where('keyword_id', $existingKeyword->id)
->first();
if (!$keywordIsAttached) {
DB::table('show_keywords')
->insert([
'show_id' => $show->id,
'keyword_id' => $existingKeyword->id,
]);
}
}
@@ -597,12 +657,25 @@ class ShowProcessingService implements ProcessingInterface
private function removeElementFromRemainingJobs(int $tmdbId, string $job)
{
$itemToFetchJobs = ItemToFetchJobs::where('tmdb_id', $tmdbId)->first();
$itemToFetchJobs = ItemToFetchJobs::where('tmdb_id', $tmdbId)
->where('type', 'tv_series')
->first();
$remainingJobs = $itemToFetchJobs->remaining_jobs;
if (!$itemToFetchJobs) {
echo "no job for ".$tmdbId."\n";
return;
}
$remainingJobs = $itemToFetchJobs->jobs;
if (is_string($remainingJobs)) {
$remainingJobs = json_decode($remainingJobs, true);
$remainingJobs = explode(',', $remainingJobs);
} else if ($itemToFetchJobs->jobs == null) {
$this->removeItemToFetch($tmdbId);
return;
} else {
dd("Not string", $remainingJobs, $itemToFetchJobs);
return;
}
$remainingJobKey = array_search($job, $remainingJobs);
@@ -610,20 +683,42 @@ class ShowProcessingService implements ProcessingInterface
unset($remainingJobs[$remainingJobKey]);
if (empty($remainingJobs)) {
$itemToFetchJobs->delete();
$this->removeItemToFetch($tmdbId);
return;
}
$itemToFetchJobs->remaining_jobs = $remainingJobs;
$itemToFetchJobs->jobs = implode(',', $remainingJobs);
$itemToFetchJobs->save();
}
private function removeItemToFetch($tmdbId)
{
$itemToFetchJobs = ItemToFetchJobs::where('tmdb_id', $tmdbId)
->where('type', 'tv_series')
->first();
$itemToFetch = ItemToFetch::where('tmdb_id', $tmdbId)
->where('type', 'tv_series')
->first();
if ($itemToFetch) {
$itemToFetch->delete();
}
if ($itemToFetchJobs) {
$itemToFetchJobs->delete();
}
}
private function processCast($show, array $credits)
{
if (empty($credits)) {
if (empty($credits) || $show === null) {
echo "+";
return;
}
foreach ($credits as $castPerson) {
echo "Searching for person ".$castPerson['id']."\n";
$person = Person::where('tmdb_id', $castPerson['id'])->first();
if (!$person) {
@@ -652,27 +747,33 @@ class ShowProcessingService implements ProcessingInterface
]);
}
echo "Creating person ".$castPerson['id']."\n";
$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'],
]);
$castExists = DB::table('show_cast')
->where('show_id', $show->id)
->where('person_id', $person->id)
->first();
if (!$castExists) {
DB::table('show_cast')
->insert([
'show_id' => $show->id,
'person_id' => $person->id,
'known_for_department' => trim($castPerson['known_for_department'], '"'),
'character' => $castPerson['character'],
'tmdb_cast_id' => $castPerson['credit_id'],
'order' => $castPerson['order'],
]);
}
}
}
private function processCrew($show, array $credits)
{
if (empty($credits)) {
if (empty($credits) || $show === null) {
echo "+";
return;
}
@@ -708,17 +809,21 @@ class ShowProcessingService implements ProcessingInterface
$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'],
]);
$crewExists = DB::table('show_crew')
->where('show_id', $show->id)
->where('person_id', $person->id)
->first();
if (!$crewExists) {
DB::table('show_crew')
->insert([
'show_id' => $show->id,
'person_id' => $person->id,
'known_for_department' => trim($crewPerson['known_for_department'], '"'),
'job' => $crewPerson['job'],
'department' => $crewPerson['department'],
'tmdb_cast_id' => $crewPerson['credit_id'],
]);
}
}
}