Files
cleargame/app/Console/Commands/ProcessIgdbDumps.php
T

203 lines
7.0 KiB
PHP
Raw Normal View History

2025-04-10 00:44:55 +03:00
<?php
namespace App\Console\Commands;
use App\Services\IgdbService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;
use ZipArchive;
class ProcessIgdbDumps extends Command
{
protected $signature = 'igdb:process-dumps
{--path= : Path to the directory containing IGDB dumps}
{--type=games : Type of dump to process (games, genres, platforms)}';
protected $description = 'Process IGDB data dumps and import them into the database';
public function handle(IgdbService $igdbService): int
{
$path = $this->option('path');
$type = $this->option('type');
if (!$path) {
// $this->error('Please provide a path to the IGDB dumps directory');
// return Command::FAILURE;
$path = storage_path('/dumps');
}
if (!is_dir($path)) {
$this->error('The provided path is not a directory');
return Command::FAILURE;
}
$this->info("Processing {$type} dumps from {$path}");
try {
switch ($type) {
case 'games':
$this->processGamesDump($path, $igdbService);
break;
case 'genres':
$this->processGenresDump($path, $igdbService);
break;
case 'platforms':
$this->processPlatformsDump($path, $igdbService);
break;
default:
$this->error("Unknown dump type: {$type}");
return Command::FAILURE;
}
} catch (\Exception $e) {
$this->error("Error processing dumps: {$e->getMessage()}");
Log::error('Error processing IGDB dumps', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString()
]);
return Command::FAILURE;
}
$this->info('Dump processing completed successfully');
return Command::SUCCESS;
}
private function processGamesDump(string $path, IgdbService $igdbService): void
{
$files = glob($path . '/games_*.json');
$totalFiles = count($files);
$processedFiles = 0;
$this->info("Found {$totalFiles} game dump files");
foreach ($files as $file) {
$this->info("Processing file: " . basename($file));
$content = file_get_contents($file);
$games = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$this->error("Error decoding JSON in file: " . basename($file));
continue;
}
$totalGames = count($games);
$this->info("Found {$totalGames} games in file");
$progressBar = $this->output->createProgressBar($totalGames);
$progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%');
foreach ($games as $gameData) {
try {
$igdbService->processGameData($gameData);
$progressBar->advance();
} catch (\Exception $e) {
Log::error('Failed to process game', [
'game_id' => $gameData['id'] ?? 'unknown',
'error' => $e->getMessage()
]);
$this->warn("Failed to process game: {$e->getMessage()}");
}
}
$progressBar->finish();
$this->newLine();
$processedFiles++;
}
$this->info("Processed {$processedFiles} out of {$totalFiles} files");
}
private function processGenresDump(string $path, IgdbService $igdbService): void
{
$files = glob($path . '/genres_*.json');
$totalFiles = count($files);
$processedFiles = 0;
$this->info("Found {$totalFiles} genre dump files");
foreach ($files as $file) {
$this->info("Processing file: " . basename($file));
$content = file_get_contents($file);
$genres = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$this->error("Error decoding JSON in file: " . basename($file));
continue;
}
$totalGenres = count($genres);
$this->info("Found {$totalGenres} genres in file");
$progressBar = $this->output->createProgressBar($totalGenres);
$progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%');
foreach ($genres as $genreData) {
try {
$igdbService->processGenreData($genreData);
$progressBar->advance();
} catch (\Exception $e) {
Log::error('Failed to process genre', [
'genre_id' => $genreData['id'] ?? 'unknown',
'error' => $e->getMessage()
]);
$this->warn("Failed to process genre: {$e->getMessage()}");
}
}
$progressBar->finish();
$this->newLine();
$processedFiles++;
}
$this->info("Processed {$processedFiles} out of {$totalFiles} files");
}
private function processPlatformsDump(string $path, IgdbService $igdbService): void
{
$files = glob($path . '/platforms_*.json');
$totalFiles = count($files);
$processedFiles = 0;
$this->info("Found {$totalFiles} platform dump files");
foreach ($files as $file) {
$this->info("Processing file: " . basename($file));
$content = file_get_contents($file);
$platforms = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$this->error("Error decoding JSON in file: " . basename($file));
continue;
}
$totalPlatforms = count($platforms);
$this->info("Found {$totalPlatforms} platforms in file");
$progressBar = $this->output->createProgressBar($totalPlatforms);
$progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%');
foreach ($platforms as $platformData) {
try {
$igdbService->processPlatformData($platformData);
$progressBar->advance();
} catch (\Exception $e) {
Log::error('Failed to process platform', [
'platform_id' => $platformData['id'] ?? 'unknown',
'error' => $e->getMessage()
]);
$this->warn("Failed to process platform: {$e->getMessage()}");
}
}
$progressBar->finish();
$this->newLine();
$processedFiles++;
}
$this->info("Processed {$processedFiles} out of {$totalFiles} files");
}
}