fix: put steamgriddb client behind store

This commit is contained in:
dusk 2024-11-19 01:28:50 +03:00
parent d91a00a3be
commit 06cc45c4de
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw

View File

@ -59,10 +59,15 @@ export const lastFmGetNowPlaying: () => Promise<LastTrack | null> = async () =>
} }
} }
const steamgriddbClient = new SGDB(env.STEAMGRIDDB_API_KEY); const steamgriddbClient = writable<SGDB | null>(null);
const cachedLastGame = writable<{game: LastGame | null, since: number}>({game: null, since: 0}) const cachedLastGame = writable<{game: LastGame | null, since: number}>({game: null, since: 0})
export type LastGame = {name: string, link: string, icon: string, pfp: string} export type LastGame = {name: string, link: string, icon: string, pfp: string}
export const steamGetNowPlaying: () => Promise<LastGame | null> = async () => { export const steamGetNowPlaying: () => Promise<LastGame | null> = async () => {
var griddbClient = get(steamgriddbClient)
if (griddbClient === null) {
griddbClient = new SGDB(env.STEAMGRIDDB_API_KEY)
steamgriddbClient.set(griddbClient)
}
var cached = get(cachedLastGame) var cached = get(cachedLastGame)
if (Date.now() - cached.since < 10 * 1000) { if (Date.now() - cached.since < 10 * 1000) {
return cached.game return cached.game
@ -73,7 +78,7 @@ export const steamGetNowPlaying: () => Promise<LastGame | null> = async () => {
if (!profile.gameid) { if (!profile.gameid) {
throw "no game is being played" throw "no game is being played"
} }
var icons = await steamgriddbClient.getIconsBySteamAppId(profile.gameid, ['official']) var icons = await griddbClient.getIconsBySteamAppId(profile.gameid, ['official'])
console.log(icons) console.log(icons)
var game = { var game = {
name: profile.gameextrainfo, name: profile.gameextrainfo,