Get game
This commit is contained in:
@ -1,36 +1,33 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using NostrStreamer.ApiModel;
|
|
||||||
using NostrStreamer.Services;
|
using NostrStreamer.Services;
|
||||||
|
|
||||||
namespace NostrStreamer.Controllers;
|
namespace NostrStreamer.Controllers;
|
||||||
|
|
||||||
[Route("/api/v1/games")]
|
[Route("/api/v1/games")]
|
||||||
public class GameInfoController : Controller
|
public class GameInfoController(GameDb gameDb) : Controller
|
||||||
{
|
{
|
||||||
private readonly GameDb _gameDb;
|
|
||||||
|
|
||||||
public GameInfoController(GameDb gameDb)
|
|
||||||
{
|
|
||||||
_gameDb = gameDb;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<IActionResult> GetGames([FromQuery] string q, [FromQuery] int limit = 10)
|
public async Task<IActionResult> GetGames([FromQuery] string q, [FromQuery] int limit = 10)
|
||||||
{
|
{
|
||||||
var data = await _gameDb.SearchGames(q, limit);
|
var data = await gameDb.SearchGames(q, limit);
|
||||||
|
|
||||||
var mapped = data?.Select(a => new GameInfo()
|
var mapped = data?.Select(a => a.ToGameInfo());
|
||||||
{
|
|
||||||
Id = $"igdb:{a.Id}",
|
|
||||||
Name = a.Name,
|
|
||||||
Cover = $"https://images.igdb.com/igdb/image/upload/t_cover_big_2x/{a.Cover?.ImageId}.jpg",
|
|
||||||
Genres = a.Genres.Select(b => b.Name).ToList()
|
|
||||||
});
|
|
||||||
|
|
||||||
return Json(mapped, new JsonSerializerSettings()
|
return Json(mapped, new JsonSerializerSettings()
|
||||||
{
|
{
|
||||||
NullValueHandling = NullValueHandling.Ignore
|
NullValueHandling = NullValueHandling.Ignore
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public async Task<IActionResult> GetGame([FromQuery] string id)
|
||||||
|
{
|
||||||
|
var data = await gameDb.GetGame(id);
|
||||||
|
|
||||||
|
return Json(data?.ToGameInfo(), new JsonSerializerSettings()
|
||||||
|
{
|
||||||
|
NullValueHandling = NullValueHandling.Ignore
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,12 +1,14 @@
|
|||||||
using Amazon;
|
using Amazon;
|
||||||
using Amazon.Runtime;
|
using Amazon.Runtime;
|
||||||
using Amazon.S3;
|
using Amazon.S3;
|
||||||
|
using Igdb;
|
||||||
using MaxMind.GeoIP2;
|
using MaxMind.GeoIP2;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Nostr.Client.Identifiers;
|
using Nostr.Client.Identifiers;
|
||||||
using Nostr.Client.Json;
|
using Nostr.Client.Json;
|
||||||
using Nostr.Client.Keys;
|
using Nostr.Client.Keys;
|
||||||
using Nostr.Client.Messages;
|
using Nostr.Client.Messages;
|
||||||
|
using NostrStreamer.ApiModel;
|
||||||
using NostrStreamer.Database;
|
using NostrStreamer.Database;
|
||||||
|
|
||||||
namespace NostrStreamer;
|
namespace NostrStreamer;
|
||||||
@ -119,6 +121,17 @@ public static class Extensions
|
|||||||
var ev = NostrJson.Deserialize<NostrEvent>(stream.Event);
|
var ev = NostrJson.Deserialize<NostrEvent>(stream.Event);
|
||||||
return ev!.ToIdentifier();
|
return ev!.ToIdentifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static GameInfo ToGameInfo(this Game a)
|
||||||
|
{
|
||||||
|
return new GameInfo
|
||||||
|
{
|
||||||
|
Id = $"igdb:{a.Id}",
|
||||||
|
Name = a.Name,
|
||||||
|
Cover = $"https://images.igdb.com/igdb/image/upload/t_cover_big_2x/{a.Cover?.ImageId}.jpg",
|
||||||
|
Genres = a.Genres.Select(b => b.Name).ToList()
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Variant
|
public class Variant
|
||||||
|
@ -78,6 +78,32 @@ public class GameDb
|
|||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<Game?> GetGame(string id)
|
||||||
|
{
|
||||||
|
await RefreshToken();
|
||||||
|
|
||||||
|
var req = new HttpRequestMessage(HttpMethod.Post, "https://api.igdb.com/v4/games.pb");
|
||||||
|
req.Headers.Add("Client-ID", _config.ClientId);
|
||||||
|
req.Headers.Authorization = new("Bearer", _currentToken?.AccessToken);
|
||||||
|
req.Content = new StringContent($"fields id,cover.image_id,genres.name,name; where id = {id};");
|
||||||
|
|
||||||
|
var rsp = await _client.SendAsync(req);
|
||||||
|
if (rsp.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
var rspStream = await rsp.Content.ReadAsStreamAsync();
|
||||||
|
|
||||||
|
var ret = GameResult.Parser.ParseFrom(rspStream);
|
||||||
|
return ret.Games.FirstOrDefault();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var content = await rsp.Content.ReadAsStringAsync();
|
||||||
|
_logger.LogWarning("Failed to fetch games {msg}", content);
|
||||||
|
}
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
class Token
|
class Token
|
||||||
{
|
{
|
||||||
[JsonProperty("access_token")]
|
[JsonProperty("access_token")]
|
||||||
|
Reference in New Issue
Block a user