backfill command and minor fixes
This commit is contained in:
@@ -2,16 +2,13 @@
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Company;
|
||||
use App\Models\Game;
|
||||
use App\Models\GameScreenshot;
|
||||
use App\Models\Genre;
|
||||
use App\Models\Platform;
|
||||
use App\Console\Commands\Concerns\UpsertsGames;
|
||||
use App\Services\IgdbService;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class ImportGamesCommand extends Command
|
||||
{
|
||||
use UpsertsGames;
|
||||
protected $signature = 'games:import {--limit=100} {--offset=0} {--popular} {--new} {--year=} {--platform=}';
|
||||
protected $description = 'Import games from IGDB into the local database';
|
||||
|
||||
@@ -74,87 +71,4 @@ class ImportGamesCommand extends Command
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
private function upsertGame(array $data, IgdbService $igdb): void
|
||||
{
|
||||
$igdbId = $data['id'];
|
||||
$title = $data['name'] ?? 'Unknown';
|
||||
$releaseDate = isset($data['first_release_date'])
|
||||
? date('Y-m-d', $data['first_release_date'])
|
||||
: null;
|
||||
$releaseYear = $releaseDate ? substr($releaseDate, 0, 4) : null;
|
||||
|
||||
$coverUrl = null;
|
||||
$coverThumbUrl = null;
|
||||
if (!empty($data['cover']['image_id'])) {
|
||||
$coverUrl = $igdb::coverUrl($data['cover']['image_id'], 'cover_big');
|
||||
$coverThumbUrl = $igdb::coverUrl($data['cover']['image_id'], 'cover_small');
|
||||
}
|
||||
|
||||
$game = Game::updateOrCreate(
|
||||
['igdb_id' => $igdbId],
|
||||
[
|
||||
'title' => $title,
|
||||
'summary' => $data['summary'] ?? null,
|
||||
'cover_url' => $coverUrl,
|
||||
'cover_thumb_url'=> $coverThumbUrl,
|
||||
'release_date' => $releaseDate,
|
||||
'release_year' => $releaseYear,
|
||||
'status' => 'published',
|
||||
]
|
||||
);
|
||||
|
||||
// Genres
|
||||
if (!empty($data['genres'])) {
|
||||
$genreIds = [];
|
||||
foreach ($data['genres'] as $g) {
|
||||
$genre = Genre::firstOrCreate(
|
||||
['igdb_id' => $g['id']],
|
||||
['name' => $g['name'], 'slug' => \Str::slug($g['name'])]
|
||||
);
|
||||
$genreIds[] = $genre->id;
|
||||
}
|
||||
$game->genres()->sync($genreIds);
|
||||
}
|
||||
|
||||
// Platforms
|
||||
if (!empty($data['platforms'])) {
|
||||
$platformIds = [];
|
||||
foreach ($data['platforms'] as $p) {
|
||||
$platform = Platform::firstOrCreate(
|
||||
['igdb_id' => $p['id']],
|
||||
['name' => $p['name'], 'slug' => \Str::slug($p['name'])]
|
||||
);
|
||||
$platformIds[] = $platform->id;
|
||||
}
|
||||
$game->platforms()->sync($platformIds);
|
||||
}
|
||||
|
||||
// Companies (involved_companies)
|
||||
if (!empty($data['involved_companies'])) {
|
||||
$companyPivot = [];
|
||||
foreach ($data['involved_companies'] as $ic) {
|
||||
if (empty($ic['company']['name'])) continue;
|
||||
$company = Company::firstOrCreate(
|
||||
['igdb_id' => $ic['company']['id']],
|
||||
['name' => $ic['company']['name'], 'slug' => \Str::slug($ic['company']['name'])]
|
||||
);
|
||||
$role = $ic['developer'] ? 'developer' : ($ic['publisher'] ? 'publisher' : 'other');
|
||||
$companyPivot[$company->id] = ['role' => $role];
|
||||
}
|
||||
$game->companies()->sync($companyPivot);
|
||||
}
|
||||
|
||||
// Screenshots
|
||||
if (!empty($data['screenshots'])) {
|
||||
GameScreenshot::where('game_id', $game->id)->delete();
|
||||
foreach ($data['screenshots'] as $i => $shot) {
|
||||
GameScreenshot::create([
|
||||
'game_id' => $game->id,
|
||||
'url' => $igdb::screenshotUrl($shot['image_id'], 'screenshot_big'),
|
||||
'thumb_url' => $igdb::screenshotUrl($shot['image_id'], 'screenshot_med'),
|
||||
'order' => $i,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user