package steamimmich import ( "encoding/json" "fmt" "io" "log/slog" "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 } name, err := fetchGameName(appID) if err != nil { slog.Warn("Failed to fetch name for AppID. Setting to Unknown", "appid", appID) cache[appID] = UNKOWN_GAME_GAME return UNKOWN_GAME_GAME } cache[appID] = name return name } func fetchGameName(appID string) (string, error) { url := fmt.Sprintf("https://store.steampowered.com/api/appdetails?appids=%s", appID) resp, err := http.Get(url) if err != nil { return "", fmt.Errorf("fetching game name for %s: %s", appID, err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return "", fmt.Errorf("reading response for %s: %s", appID, err) } var result map[string]steamAPIResponse if err := json.Unmarshal(body, &result); err != nil { return "", fmt.Errorf("parsing response for %s: %s", appID, err) } entry, ok := result[appID] if !ok || !entry.Success { return "", fmt.Errorf("game not found in response for %s", appID) } return entry.Data.Name, nil }