refactor: move steam code into its own lib file

This commit is contained in:
dusk 2024-11-23 02:45:52 +03:00
parent 61bec8904e
commit c06b5b010c
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw
3 changed files with 43 additions and 38 deletions

View File

@ -2,7 +2,6 @@ import type { Cookies } from '@sveltejs/kit'
import { env } from '$env/dynamic/private' import { env } from '$env/dynamic/private'
import { get, writable } from 'svelte/store' import { get, writable } from 'svelte/store'
import { existsSync, readFileSync } from 'fs' import { existsSync, readFileSync } from 'fs'
import SGDB from 'steamgriddb'
export const scopeCookies = (cookies: Cookies, path: string) => { export const scopeCookies = (cookies: Cookies, path: string) => {
return { return {
@ -48,40 +47,4 @@ export const lastFmGetNowPlaying: () => Promise<LastTrack | null> = async () =>
cachedLastTrack.set({track: null, since: Date.now()}) cachedLastTrack.set({track: null, since: Date.now()})
return null return null
} }
}
const steamgriddbClient = writable<SGDB | null>(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<LastGame | null> = 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
}
} }

41
src/lib/steam.ts Normal file
View File

@ -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<SGDB | null>(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<LastGame | null> = 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
}
}

View File

@ -1,4 +1,5 @@
import { lastFmGetNowPlaying, steamGetNowPlaying } from "$lib" import { lastFmGetNowPlaying } from "$lib"
import { steamGetNowPlaying } from "$lib/steam"
export const load = async ({}) => { export const load = async ({}) => {
const lastTrack = await lastFmGetNowPlaying() const lastTrack = await lastFmGetNowPlaying()