mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
Stuff i guess ?
This commit is contained in:
132
app/Services/SteamGridDbService.php
Normal file
132
app/Services/SteamGridDbService.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
class SteamGridDbService
|
||||
{
|
||||
private const BASE_URI = 'https://www.steamgriddb.com/api/v2/';
|
||||
private Client $client;
|
||||
private ?string $apiKey;
|
||||
|
||||
public function __construct(?string $apiKey = null)
|
||||
{
|
||||
$this->apiKey = $apiKey ?? $_ENV['STEAMGRIDDB_API_KEY'] ?? null;
|
||||
$this->client = new Client([
|
||||
'base_uri' => self::BASE_URI,
|
||||
'headers' => [
|
||||
'Authorization' => 'Bearer ' . $this->apiKey,
|
||||
'Accept' => 'application/json',
|
||||
],
|
||||
'http_errors' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for games by name
|
||||
*/
|
||||
public function searchGames(string $query): array
|
||||
{
|
||||
try {
|
||||
$response = $this->client->get('search/autocomplete/' . urlencode($query));
|
||||
$data = json_decode($response->getBody()->getContents(), true);
|
||||
return $data['data'] ?? [];
|
||||
} catch (GuzzleException $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get game by ID
|
||||
*/
|
||||
public function getGame(int $gameId): ?array
|
||||
{
|
||||
try {
|
||||
$response = $this->client->get('games/id/' . $gameId);
|
||||
$data = json_decode($response->getBody()->getContents(), true);
|
||||
return $data['data'] ?? null;
|
||||
} catch (GuzzleException $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get game grids (covers)
|
||||
*/
|
||||
public function getGrids(int $gameId, array $options = []): array
|
||||
{
|
||||
return $this->getMedia('grids/game/' . $gameId, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get game heroes (backgrounds)
|
||||
*/
|
||||
public function getHeroes(int $gameId, array $options = []): array
|
||||
{
|
||||
return $this->getMedia('heroes/game/' . $gameId, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get game icons
|
||||
*/
|
||||
public function getIcons(int $gameId, array $options = []): array
|
||||
{
|
||||
return $this->getMedia('icons/game/' . $gameId, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get game logos
|
||||
*/
|
||||
public function getLogos(int $gameId, array $options = []): array
|
||||
{
|
||||
return $this->getMedia('logos/game/' . $gameId, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Download a media file
|
||||
*/
|
||||
public function downloadMedia(string $url): ?string
|
||||
{
|
||||
try {
|
||||
$response = $this->client->get($url, ['stream' => true]);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$tempFile = tempnam(sys_get_temp_dir(), 'sgdb_');
|
||||
file_put_contents($tempFile, $response->getBody());
|
||||
|
||||
return $tempFile;
|
||||
} catch (GuzzleException $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function getMedia(string $endpoint, array $options = []): array
|
||||
{
|
||||
$query = [];
|
||||
if (!empty($options['styles'])) {
|
||||
$query['styles'] = is_array($options['styles']) ? implode(',', $options['styles']) : $options['styles'];
|
||||
}
|
||||
if (!empty($options['dimensions'])) {
|
||||
$query['dimensions'] = is_array($options['dimensions']) ? implode(',', $options['dimensions']) : $options['dimensions'];
|
||||
}
|
||||
if (!empty($options['mimes'])) {
|
||||
$query['mimes'] = is_array($options['mimes']) ? implode(',', $options['mimes']) : $options['mimes'];
|
||||
}
|
||||
if (!empty($options['types'])) {
|
||||
$query['types'] = is_array($options['types']) ? implode(',', $options['types']) : $options['types'];
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->client->get($endpoint, ['query' => $query]);
|
||||
$data = json_decode($response->getBody()->getContents(), true);
|
||||
return $data['data'] ?? [];
|
||||
} catch (GuzzleException $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user