128 lines
3.6 KiB
PHP
128 lines
3.6 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Console\Commands\TmdbFetch;
|
||
|
|
|
||
|
|
use App\Models\ItemToFetch;
|
||
|
|
use App\Models\ItemToFetchJobs;
|
||
|
|
use App\Service\TMDBApi\ShowsApi;
|
||
|
|
use Illuminate\Console\Command;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
|
||
|
|
class DiscoverTvSeriesCommand extends Command
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* The name and signature of the console command.
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $signature = 'tmdb:discover-tv-series {--page=1} {--limit=5}';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The console command description.
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $description = 'Discover TV series from TMDB API and add them to the fetch queue';
|
||
|
|
|
||
|
|
private ShowsApi $showsApi;
|
||
|
|
|
||
|
|
public function __construct(ShowsApi $showsApi)
|
||
|
|
{
|
||
|
|
$this->showsApi = $showsApi;
|
||
|
|
parent::__construct();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute the console command.
|
||
|
|
*/
|
||
|
|
public function handle()
|
||
|
|
{
|
||
|
|
$page = $this->option('page');
|
||
|
|
$limit = $this->option('limit');
|
||
|
|
|
||
|
|
$this->info("Discovering TV series from TMDB API (page: {$page}, limit: {$limit})");
|
||
|
|
|
||
|
|
$totalPages = 0;
|
||
|
|
$processedCount = 0;
|
||
|
|
|
||
|
|
try {
|
||
|
|
for ($currentPage = $page; $currentPage < $page + $limit; $currentPage++) {
|
||
|
|
$this->info("Processing page {$currentPage}");
|
||
|
|
|
||
|
|
$discoverResult = $this->showsApi->discover([
|
||
|
|
'page' => $currentPage,
|
||
|
|
'sort_by' => 'popularity.desc',
|
||
|
|
'include_adult' => false,
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (empty($discoverResult) || empty($discoverResult['results'])) {
|
||
|
|
$this->error("No results found on page {$currentPage}");
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
$totalPages = $discoverResult['total_pages'];
|
||
|
|
|
||
|
|
if ($currentPage > $totalPages) {
|
||
|
|
$this->info("Reached the end of results (total pages: {$totalPages})");
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
DB::beginTransaction();
|
||
|
|
|
||
|
|
foreach ($discoverResult['results'] as $show) {
|
||
|
|
$this->processShow($show);
|
||
|
|
$processedCount++;
|
||
|
|
}
|
||
|
|
|
||
|
|
DB::commit();
|
||
|
|
|
||
|
|
// Sleep to avoid rate limiting
|
||
|
|
sleep(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->info("Successfully processed {$processedCount} TV series");
|
||
|
|
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
DB::rollBack();
|
||
|
|
$this->error("Error processing TV series: " . $e->getMessage());
|
||
|
|
$this->error($e->getTraceAsString());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private function processShow(array $show)
|
||
|
|
{
|
||
|
|
$tmdbId = $show['id'];
|
||
|
|
|
||
|
|
// Check if this show is already in the queue
|
||
|
|
$existingJob = ItemToFetchJobs::where('tmdb_id', $tmdbId)
|
||
|
|
->where('type', ItemToFetch::TYPE_TV_SERIES)
|
||
|
|
->first();
|
||
|
|
|
||
|
|
if ($existingJob) {
|
||
|
|
$this->line("TV series ID {$tmdbId} already in queue, skipping");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create ItemToFetch record if it doesn't exist
|
||
|
|
$itemToFetch = ItemToFetch::firstOrCreate(
|
||
|
|
[
|
||
|
|
'tmdb_id' => $tmdbId,
|
||
|
|
'type' => ItemToFetch::TYPE_TV_SERIES,
|
||
|
|
]
|
||
|
|
);
|
||
|
|
|
||
|
|
// Prepare jobs list
|
||
|
|
$jobs = ItemToFetchJobs::SHOW_JOBS;
|
||
|
|
$jobs[] = ItemToFetchJobs::JOB_DETAILS;
|
||
|
|
|
||
|
|
// Create ItemToFetchJobs record
|
||
|
|
ItemToFetchJobs::create([
|
||
|
|
'tmdb_id' => $tmdbId,
|
||
|
|
'type' => ItemToFetch::TYPE_TV_SERIES,
|
||
|
|
'jobs' => implode(',', $jobs),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->line("Added TV series ID {$tmdbId} to fetch queue");
|
||
|
|
}
|
||
|
|
}
|