lots of progress

This commit is contained in:
2025-04-12 17:18:33 +03:00
parent 8d230e8e67
commit 3d4260d57e
44 changed files with 2590 additions and 2883 deletions
+22 -5
View File
@@ -9,7 +9,7 @@ use Illuminate\Support\Facades\Log;
class IgdbAuthService
{
private const TOKEN_CACHE_KEY = 'igdb_access_token';
private const TOKEN_EXPIRY = 3600; // 1 hour in seconds
private const TOKEN_EXPIRY = 3600*24*31; // 1 month in seconds
public function getAccessToken(): string
{
@@ -21,13 +21,30 @@ class IgdbAuthService
private function fetchNewAccessToken(): string
{
try {
$response = Http::post('https://id.twitch.tv/oauth2/token', [
'client_id' => config('services.igdb.client_id'),
'client_secret' => config('services.igdb.client_secret'),
// Log the config values to debug
Log::info('IGDB Auth Attempt', [
'client_id' => config('services.igdb.credentials.client_id'),
'client_secret' => config('services.igdb.credentials.client_secret')
]);
$clientId = config('igdb.credentials.client_id');
$clientSecret = config('igdb.credentials.client_secret');
if (empty($clientId) || empty($clientSecret)) {
throw new \Exception("IGDB credentials are not properly configured");
}
$response = Http::asForm()->post('https://id.twitch.tv/oauth2/token', [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'grant_type' => 'client_credentials',
]);
if (!$response->successful()) {
Log::error('IGDB Auth Response', [
'status' => $response->status(),
'body' => $response->body()
]);
throw new \Exception("Failed to get access token: " . $response->body());
}
@@ -40,4 +57,4 @@ class IgdbAuthService
throw $e;
}
}
}
}
+100 -34
View File
@@ -17,7 +17,7 @@ use MarcReichel\IGDBLaravel\Models\Platform as IgdbPlatform;
class IgdbService
{
public function get()
public function get(int $limit)
{
try {
$existingIds = $this->getExistingIds();
@@ -26,7 +26,7 @@ class IgdbService
'id',
'name',
'summary',
'cover.url',
'cover',
'slug',
'genres',
'platforms',
@@ -34,17 +34,21 @@ class IgdbService
'rating',
'rating_count'
])
->where('rating_count', '>', 0)
->where('category', '=', 0);
// ->where('rating_count', '>', 0)
// ->where('category', '=', 0)
;
if (!empty($existingIds)) {
$queryBuilder = $queryBuilder->whereNotIn('id', array_map(function($id) {
return (int)str_replace('"', '', $id);
}, $existingIds));
// Convert array of quoted strings to array of integers
// $existingIds = array_map(function($id) {
// return (int)str_replace('"', '', $id);
// }, $existingIds);
$queryBuilder->whereNotIn('id', $existingIds);
}
$a = $queryBuilder
->limit(50)
->orderBy('first_release_date', 'asc')
->limit($limit)
->get();
}catch (\Throwable $e) {
dd(
@@ -68,12 +72,13 @@ class IgdbService
/** @var Game $existingGame */
foreach ($existingGames as $existingGame) {
$existingIds[] = (string)$existingGame->id;
$existingIds[] = (string)$existingGame->igdb_id;
}
return array_map(function($id) {
return '"'.$id.'"';
}, $existingIds);
return $existingIds;
// return array_map(function($id) {
// return '"'.$id.'"';
// }, $existingIds);
}
public function searchGames(string $query, int $limit = 10, ?int $startTimestamp = null, ?int $endTimestamp = null): array
@@ -240,13 +245,41 @@ class IgdbService
}
}
// Process genres
$genres = [];
if (isset($gameData->genres) && is_array($gameData->genres)) {
$genreData = IgdbGenre::select(['id', 'name', 'slug'])
->whereIn('id', $gameData->genres)
->get();
foreach ($genreData as $genreDetails) {
$genre = Genre::updateOrCreate(
['igdb_id' => $genreDetails->id],
[
'name' => $genreDetails->name,
'slug' => $genreDetails->slug,
]
);
$genres[] = $genre->id;
}
}
$cover = Cover::where('game', '=', $gameData->id)->first();
$coverUrl = null;
if ($cover) {
$coverUrl = 'https:'.$cover->url;
$coverUrl = str_replace('t_thumb', 't_cover_big', $coverUrl);
}
// Create or update the game
$game = Game::updateOrCreate(
['igdb_id' => $gameData->id],
[
'name' => $gameData->name,
'summary' => $gameData->summary ?? null,
'cover_url' => $gameData->cover?->url ?? null,
'cover_url' => $coverUrl,
'slug' => $gameData->slug,
'first_release_date' => $gameData->first_release_date ?? null,
'rating' => $gameData->rating ?? null,
@@ -254,12 +287,20 @@ class IgdbService
]
);
// Sync platforms and genres
if (!empty($platforms)) {
$game->platforms()->sync($platforms);
}
if (!empty($genres)) {
$game->genres()->sync($genres);
}
// Process artworks
try {
$artworks = \MarcReichel\IGDBLaravel\Models\Artwork::select([
'id',
'url',
'width',
'width',
'height',
'image_id'
])
@@ -285,11 +326,6 @@ class IgdbService
]);
}
// Process platforms relationship
if (!empty($platforms)) {
$game->platforms()->sync($platforms);
}
// Process screenshots
try {
$screenshots = \MarcReichel\IGDBLaravel\Models\Screenshot::select([
@@ -325,7 +361,7 @@ class IgdbService
try {
$videos = \MarcReichel\IGDBLaravel\Models\GameVideo::select([
'id',
'name',
'name',
'video_id',
'url',
'checksum'
@@ -365,25 +401,55 @@ class IgdbService
throw new \InvalidArgumentException('Game data must be an instance of IgdbGame');
}
// Fetch related games data using IGDB API
$relationships = [
'bundles' => IgdbGame::select(['id'])->where('id', 'in', $gameData->bundles ?? [])->get()->pluck('id')->toArray(),
'collections' => IgdbGame::select(['id'])->where('id', 'in', $gameData->collections ?? [])->get()->pluck('id')->toArray(),
'dlcs' => IgdbGame::select(['id'])->where('id', 'in', $gameData->dlcs ?? [])->get()->pluck('id')->toArray(),
'expanded_games' => IgdbGame::select(['id'])->where('id', 'in', $gameData->expanded_games ?? [])->get()->pluck('id')->toArray(),
'franchises' => IgdbGame::select(['id'])->where('id', 'in', $gameData->franchises ?? [])->get()->pluck('id')->toArray(),
'ports' => IgdbGame::select(['id'])->where('id', 'in', $gameData->ports ?? [])->get()->pluck('id')->toArray(),
'remakes' => IgdbGame::select(['id'])->where('id', 'in', $gameData->remakes ?? [])->get()->pluck('id')->toArray(),
'similar_games' => IgdbGame::select(['id'])->where('id', 'in', $gameData->similar_games ?? [])->get()->pluck('id')->toArray(),
// Initialize relationships array
$relationships = [];
// Define relationship mappings and check each one
$relationshipMappings = [
'bundles',
'collections',
'dlcs',
'expanded_games',
'franchises',
'ports',
'remakes',
'similar_games'
];
foreach ($relationships as $igdbKey => $relationship) {
// Only query IGDB for relationships that have data
foreach ($relationshipMappings as $relationship) {
if (!empty($gameData->$relationship)) {
$relationships[$relationship] = IgdbGame::select(['id'])
->where('id', 'in', $gameData->$relationship)
->get()
->pluck('id')
->toArray();
} else {
$relationships[$relationship] = [];
}
}
// Process and sync each relationship
foreach ($relationships as $igdbKey => $relatedIds) {
// Convert IGDB relationship key to Laravel relationship method
$relationshipMethod = str_replace('_', '', $igdbKey); // Remove underscores
$relatedGames = [];
if (isset($gameData->$igdbKey) && !empty($gameData->$igdbKey)) {
$relatedIds = collect($gameData->$igdbKey)->pluck('id')->toArray();
if (!empty($relatedIds)) {
$relatedGames = Game::whereIn('igdb_id', $relatedIds)->pluck('id')->toArray();
}
$game->$relationship()->sync($relatedGames);
// Only sync if the relationship method exists
if (method_exists($game, $relationshipMethod)) {
Log::info("Syncing relationship '$relationshipMethod' for game {$game->id}");
$game->$relationshipMethod()->sync($relatedGames);
} else {
Log::warning("Relationship method '$relationshipMethod' does not exist on Game model", [
'game_id' => $game->id,
'igdb_key' => $igdbKey
]);
}
}
}