diff --git a/bun.lockb b/bun.lockb index a0d3d24..e91d688 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 8bf74b5..bfb3a32 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,9 @@ "nanoid": "^5.0.8", "rehype-autolink-headings": "^7.1.0", "rehype-slug": "^6.0.0", - "typescript-svelte-plugin": "^0.3.42" + "steamgriddb": "^2.2.0", + "typescript-svelte-plugin": "^0.3.42", + "xml-js": "^1.6.11" }, "trustedDependencies": [ "@sveltejs/kit", diff --git a/src/lib/index.ts b/src/lib/index.ts index 5f3c0d1..97f23c6 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,8 +1,10 @@ import type { Cookies } from '@sveltejs/kit' import { env } from '$env/dynamic/private' -import { writable } from 'svelte/store' +import { get, writable } from 'svelte/store' import { existsSync, readFileSync } from 'fs' import { Agent, CredentialSession } from '@atproto/api' +import { xml2json } from 'xml-js' +import SGDB from 'steamgriddb' export const scopeCookies = (cookies: Cookies, path: string) => { return { @@ -26,4 +28,64 @@ export const loginToBsky = async () => { await creds.login({ identifier: 'gaze.systems', password: env.BSKY_PASSWORD ?? "" }) return new Agent(creds) } -export const bskyClient = writable(null) \ No newline at end of file +export const bskyClient = writable(null) + +const cachedLastTrack = writable<{track: LastTrack | null, since: number}>({track: null, since: 0}) +export type LastTrack = {name: string, artist: string, image: string | null, link: string} +export const lastFmGetNowPlaying: () => Promise = async () => { + var cached = get(cachedLastTrack) + if (Date.now() - cached.since < 10 * 1000) { + return cached.track + } + try { + const API_URL = "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=yusdacra&api_key=da1911d405b5b37383e200b8f36ee9ec&format=json&limit=1" + var resp = await (await fetch(API_URL)).json() + var track = resp.recenttracks.track[0] ?? null + if (!(track['@attr'].nowplaying ?? null)) { + throw "no nowplaying track found" + } + var data = { + name: track.name, + artist: track.artist['#text'], + image: track.image[2]['#text'] ?? null, + link: track.url, + } + cachedLastTrack.set({track: data, since: Date.now()}) + return data + } catch(why) { + console.log("could not fetch last fm: ", why) + cachedLastTrack.set({track: null, since: Date.now()}) + return null + } +} + +const steamgriddbClient = new SGDB(env.STEAMGRIDDB_API_KEY); +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 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 steamgriddbClient.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 new file mode 100644 index 0000000..8b7af32 --- /dev/null +++ b/src/routes/+page.server.ts @@ -0,0 +1,7 @@ +import { lastFmGetNowPlaying, steamGetNowPlaying } from "$lib" + +export const load = async ({}) => { + const lastTrack = await lastFmGetNowPlaying() + const lastGame = await steamGetNowPlaying() + return {lastTrack, lastGame} +} \ No newline at end of file diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 7b40393..e64062a 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,6 +1,8 @@
@@ -61,10 +63,8 @@

- new game prototype thingy at itch.io page! - spent a lot of time learning and designing the environment and scene stuff :3 + new game prototype thingy at itch.io page! spent a lot of time learning and designing the environment and scene stuff :3 trenchbroom and func_godot were used mainly!

- -
-
    -
  • playing wynncraft, helldivers 2, warframe
  • -
  • idk bother me to do stuff
  • -
-
+ + {#if data.lastTrack} +
+ + {#if data.lastTrack.image} + + {:else} + + {/if} +
+

+ listening to + {data.lastTrack.name} +

+

+ by + {data.lastTrack.artist} +

+
+
+ {/if} + {#if data.lastGame} +
+ + +
+

+ playing + {data.lastGame.name} +

+ + + steam profile +
+
+ {/if} + {#if !data.lastGame && !data.lastTrack} +

nothing, apparently.

+ {/if}
diff --git a/static/icons/cd_audio.png b/static/icons/cd_audio.png new file mode 100644 index 0000000..f7cce7b Binary files /dev/null and b/static/icons/cd_audio.png differ