improved game name fetching
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@@ -16,40 +17,45 @@ type SteamAPIResponse struct {
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
func getGameName(appid string, cache map[string]string) string {
|
||||
if name, ok := cache[appid]; ok {
|
||||
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)
|
||||
name, err := fetchGameName(appID)
|
||||
if err != nil {
|
||||
log.Printf("failed to fetch name for AppID: %s. Setting to Unknown", 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 {
|
||||
fmt.Printf("Error fetching app name for %s: %v\n", appid, err)
|
||||
cache[appid] = UNKOWN_GAME_GAME
|
||||
return cache[appid]
|
||||
return "", fmt.Errorf("fetching game name for %s: %s", appID, err)
|
||||
}
|
||||
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]
|
||||
return "", fmt.Errorf("reading response for %s: %s", appID, err)
|
||||
}
|
||||
|
||||
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]
|
||||
return "", fmt.Errorf("parsing response for %s: %s", appID, err)
|
||||
}
|
||||
|
||||
if entry, ok := result[appid]; ok && entry.Success {
|
||||
name := entry.Data.Name
|
||||
cache[appid] = name
|
||||
return name
|
||||
entry, ok := result[appID]
|
||||
|
||||
if !ok || !entry.Success {
|
||||
return "", fmt.Errorf("game not found in response for %s", appID)
|
||||
}
|
||||
|
||||
cache[appid] = UNKOWN_GAME_GAME
|
||||
return cache[appid]
|
||||
return entry.Data.Name, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user