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