2025-10-29 01:34:11 +01:00

56 lines
1.2 KiB
Go

package steamimmich
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
const UNKOWN_GAME_GAME = "Unkown"
type SteamAPIResponse struct {
Success bool `json:"success"`
Data struct {
Name string `json:"name"`
} `json:"data"`
}
func getGameName(appid string, cache map[string]string) string {
if name, ok := cache[appid]; ok {
return name
}
url := fmt.Sprintf("https://store.steampowered.com/api/appdetails?appids=%s", appid)
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Error fetching app name for %s: %v\n", appid, err)
cache[appid] = UNKOWN_GAME_GAME
return cache[appid]
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response for %s: %v\n", appid, err)
cache[appid] = UNKOWN_GAME_GAME
return cache[appid]
}
var result map[string]SteamAPIResponse
if err := json.Unmarshal(body, &result); err != nil {
fmt.Printf("Error parsing JSON for %s: %v\n", appid, err)
cache[appid] = UNKOWN_GAME_GAME
return cache[appid]
}
if entry, ok := result[appid]; ok && entry.Success {
name := entry.Data.Name
cache[appid] = name
return name
}
cache[appid] = UNKOWN_GAME_GAME
return cache[appid]
}