refactor: move steam code into its own lib file
This commit is contained in:
parent
61bec8904e
commit
c06b5b010c
@ -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 {
|
||||
@ -49,39 +48,3 @@ export const lastFmGetNowPlaying: () => Promise<LastTrack | null> = async () =>
|
||||
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
41
src/lib/steam.ts
Normal 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
|
||||
}
|
||||
}
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user