Files
movies/app/Console/Commands/TmdbFetch/DiscoverMoviesCommand.php
T
2025-10-09 00:22:54 +03:00

129 lines
3.6 KiB
PHP

<?php
namespace App\Console\Commands\TmdbFetch;
use App\Models\ItemToFetch;
use App\Models\ItemToFetchJobs;
use App\Service\TMDBApi\MoviesApi;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class DiscoverMoviesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tmdb:discover-movies {--page=1} {--limit=5}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Discover movies from TMDB API and add them to the fetch queue';
private MoviesApi $moviesApi;
public function __construct(MoviesApi $moviesApi)
{
$this->moviesApi = $moviesApi;
parent::__construct();
}
/**
* Execute the console command.
*/
public function handle()
{
$page = $this->option('page');
$limit = $this->option('limit');
$this->info("Discovering movies 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->moviesApi->discover([
'page' => $currentPage,
'sort_by' => 'popularity.desc',
'include_adult' => false,
'include_video' => 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 $movie) {
$this->processMovie($movie);
$processedCount++;
}
DB::commit();
// Sleep to avoid rate limiting
sleep(1);
}
$this->info("Successfully processed {$processedCount} movies");
} catch (\Throwable $e) {
DB::rollBack();
$this->error("Error processing movies: " . $e->getMessage());
$this->error($e->getTraceAsString());
}
}
private function processMovie(array $movie)
{
$tmdbId = $movie['id'];
// Check if this movie is already in the queue
$existingJob = ItemToFetchJobs::where('tmdb_id', $tmdbId)
->where('type', 'movie')
->first();
if ($existingJob) {
$this->line("Movie ID {$tmdbId} already in queue, skipping");
return;
}
// Create ItemToFetch record if it doesn't exist
$itemToFetch = ItemToFetch::firstOrCreate(
[
'tmdb_id' => $tmdbId,
'type' => ItemToFetch::TYPE_MOVIE,
]
);
// Prepare jobs list
$jobs = ItemToFetchJobs::MOVIE_JOBS;
$jobs[] = ItemToFetchJobs::JOB_DETAILS;
// Create ItemToFetchJobs record
ItemToFetchJobs::create([
'tmdb_id' => $tmdbId,
'type' => ItemToFetch::TYPE_MOVIE,
'jobs' => implode(',', $jobs),
]);
$this->line("Added movie ID {$tmdbId} to fetch queue");
}
}