diff --git a/src/lib/index.ts b/src/lib/index.ts index 2c64a4f..6a31d0f 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -2,7 +2,6 @@ import type { Cookies } from '@sveltejs/kit' import { env } from '$env/dynamic/private' import { get, writable } from 'svelte/store' import { existsSync, readFileSync } from 'fs' -import SGDB from 'steamgriddb' export const scopeCookies = (cookies: Cookies, path: string) => { return { @@ -48,40 +47,4 @@ export const lastFmGetNowPlaying: () => Promise = async () => cachedLastTrack.set({track: null, since: Date.now()}) return null } -} - -const steamgriddbClient = writable(null); -const cachedLastGame = writable<{game: LastGame | null, since: number}>({game: null, since: 0}) -export type LastGame = {name: string, link: string, icon: string, pfp: string} -export const steamGetNowPlaying: () => Promise = async () => { - var griddbClient = get(steamgriddbClient) - if (griddbClient === null) { - griddbClient = new SGDB(env.STEAMGRIDDB_API_KEY) - steamgriddbClient.set(griddbClient) - } - var cached = get(cachedLastGame) - if (Date.now() - cached.since < 10 * 1000) { - return cached.game - } - try { - const API_URL = `http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=${env.STEAM_API_KEY}&steamids=76561198106829949&format=json` - var profile = (await (await fetch(API_URL)).json()).response.players[0] - if (!profile.gameid) { - throw "no game is being played" - } - var icons = await griddbClient.getIconsBySteamAppId(profile.gameid, ['official']) - console.log(icons) - var game = { - name: profile.gameextrainfo, - link: `https://store.steampowered.com/app/${profile.gameid}`, - icon: icons[0].thumb.toString(), - pfp: profile.avatarmedium, - } - cachedLastGame.set({game, since: Date.now()}) - return game - } catch(why) { - console.log("could not fetch steam: ", why) - cachedLastGame.set({game: null, since: Date.now()}) - return null - } } \ No newline at end of file diff --git a/src/lib/steam.ts b/src/lib/steam.ts new file mode 100644 index 0000000..c4a846c --- /dev/null +++ b/src/lib/steam.ts @@ -0,0 +1,41 @@ +import { env } from "$env/dynamic/private"; +import SGDB from "steamgriddb"; +import { get, writable } from "svelte/store"; + +const STEAM_ID = "76561198106829949" + +const steamgriddbClient = writable(null); +const cachedLastGame = writable<{game: LastGame | null, since: number}>({game: null, since: 0}) +type LastGame = {name: string, link: string, icon: string, pfp: string} +export const steamGetNowPlaying: () => Promise = async () => { + var griddbClient = get(steamgriddbClient) + if (griddbClient === null) { + griddbClient = new SGDB(env.STEAMGRIDDB_API_KEY) + steamgriddbClient.set(griddbClient) + } + var cached = get(cachedLastGame) + if (Date.now() - cached.since < 10 * 1000) { + return cached.game + } + try { + const API_URL = `http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=${env.STEAM_API_KEY}&steamids=${STEAM_ID}&format=json` + var profile = (await (await fetch(API_URL)).json()).response.players[0] + if (!profile.gameid) { + throw "no game is being played" + } + var icons = await griddbClient.getIconsBySteamAppId(profile.gameid, ['official']) + console.log(icons) + var game = { + name: profile.gameextrainfo, + link: `https://store.steampowered.com/app/${profile.gameid}`, + icon: icons[0].thumb.toString(), + pfp: profile.avatarmedium, + } + cachedLastGame.set({game, since: Date.now()}) + return game + } catch(why) { + console.log("could not fetch steam: ", why) + cachedLastGame.set({game: null, since: Date.now()}) + return null + } +} \ No newline at end of file diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 8b7af32..a846e0e 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,4 +1,5 @@ -import { lastFmGetNowPlaying, steamGetNowPlaying } from "$lib" +import { lastFmGetNowPlaying } from "$lib" +import { steamGetNowPlaying } from "$lib/steam" export const load = async ({}) => { const lastTrack = await lastFmGetNowPlaying()