This commit is contained in:
2025-10-09 00:22:54 +03:00
parent 7e0e330ef6
commit b8b39c945d
28 changed files with 2186 additions and 452 deletions
@@ -69,6 +69,7 @@ class FetchItemDataCommand extends Command
$bar = $this->output->createProgressBar(count($items));
foreach ($items as $item) {
echo "Processing ".$item->tmdb_id."\n";
$this->processItem($item, $job, $type);
$bar->advance();
}
@@ -0,0 +1,128 @@
<?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");
}
}
@@ -0,0 +1,127 @@
<?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");
}
}