Extend GameMetadataProvider with fetchById

This commit is contained in:
grimsi
2025-06-12 19:53:09 +02:00
parent ddfaeed34a
commit 0633bb14e7
6 changed files with 72 additions and 25 deletions
@@ -72,9 +72,9 @@ class SteamGridDbPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin(wra
@Extension
class SteamGridDBGameCoverProvider : GameMetadataProvider {
override fun fetchMetadata(gameId: String, maxResults: Int): List<GameMetadata> {
override fun fetchByTitle(gameTitle: String, maxResults: Int): List<GameMetadata> {
return runBlocking {
var searchResults = searchSteamGridDb(gameId)
var searchResults = searchSteamGridDb(gameTitle)
if (searchResults.isEmpty()) return@runBlocking emptyList()
if (searchResults.size > maxResults) searchResults = searchResults.slice(0 until maxResults)
@@ -91,6 +91,19 @@ class SteamGridDbPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin(wra
}
}
override fun fetchById(id: String): GameMetadata? {
return runBlocking {
val gameId = id.toIntOrNull() ?: return@runBlocking null
val game = getGameById(gameId) ?: return@runBlocking null
return@runBlocking GameMetadata(
originalId = game.id.toString(),
title = game.name,
coverUrl = getGridForGame(game.id)?.let { grid -> URI(grid.url) }
)
}
}
private suspend fun searchSteamGridDb(term: String): List<SteamGridDbGame> {
val client = client ?: throw PluginConfigError("SteamGridDB API client not initialized")
@@ -110,5 +123,13 @@ class SteamGridDbPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin(wra
return gameDetails.data?.firstOrNull()
}
private suspend fun getGameById(gameId: Int): SteamGridDbGame? {
val client = client ?: throw PluginConfigError("SteamGridDB API client not initialized")
val gameDetails = client.game(gameId)
return gameDetails.data?.firstOrNull()
}
}
}
@@ -51,6 +51,10 @@ class SteamGridDbApiClient(private val apiKey: String) {
}.body()
}
suspend fun game(gameId: Int, block: HttpRequestBuilder.() -> Unit = {}): SteamGridDbSearchResult {
return get("games/id/$gameId", block).body()
}
private suspend fun get(endpoint: String, block: HttpRequestBuilder.() -> Unit = {}): HttpResponse {
return client.get("$BASE_URL/$endpoint".encodeURLPath(encodeEncoded = false)) {
bearerAuth(apiKey)