diff --git a/src/lib/lastfm.ts b/src/lib/lastfm.ts index 18752d8..dabceb2 100644 --- a/src/lib/lastfm.ts +++ b/src/lib/lastfm.ts @@ -1,15 +1,19 @@ import { get, writable } from "svelte/store" -const cachedLastTrack = writable<{track: LastTrack | null, since: number}>({track: null, since: 0}) -export type LastTrack = {name: string, artist: string, image: string | null, link: string} +const GET_RECENT_TRACKS_ENDPOINT = "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=yusdacra&api_key=da1911d405b5b37383e200b8f36ee9ec&format=json&limit=1" +const CACHE_EXPIRY_SECONDS = 10 + +type LastTrack = {name: string, artist: string, image: string | null, link: string} +type CachedLastTrack = {track: LastTrack | null, since: number} +const cachedLastTrack = writable({track: null, since: 0}) + export const lastFmGetNowPlaying: () => Promise = async () => { var cached = get(cachedLastTrack) - if (Date.now() - cached.since < 10 * 1000) { + if (Date.now() - cached.since < CACHE_EXPIRY_SECONDS * 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 resp = await (await fetch(GET_RECENT_TRACKS_ENDPOINT)).json() var track = resp.recenttracks.track[0] ?? null if (!(track['@attr'].nowplaying ?? null)) { throw "no nowplaying track found" diff --git a/src/lib/steam.ts b/src/lib/steam.ts index c4a846c..cefcc96 100644 --- a/src/lib/steam.ts +++ b/src/lib/steam.ts @@ -3,10 +3,15 @@ import SGDB from "steamgriddb"; import { get, writable } from "svelte/store"; const STEAM_ID = "76561198106829949" +const GET_PLAYER_SUMMARY_ENDPOINT = `http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=${env.STEAM_API_KEY}&steamids=${STEAM_ID}&format=json` +const CACHE_EXPIRY_SECONDS = 10 + +type LastGame = {name: string, link: string, icon: string, pfp: string} +type CachedLastGame = {game: LastGame | null, since: number} 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} +const cachedLastGame = writable({game: null, since: 0}) + export const steamGetNowPlaying: () => Promise = async () => { var griddbClient = get(steamgriddbClient) if (griddbClient === null) { @@ -14,12 +19,11 @@ export const steamGetNowPlaying: () => Promise = async () => { steamgriddbClient.set(griddbClient) } var cached = get(cachedLastGame) - if (Date.now() - cached.since < 10 * 1000) { + if (Date.now() - cached.since < CACHE_EXPIRY_SECONDS * 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] + var profile = (await (await fetch(GET_PLAYER_SUMMARY_ENDPOINT)).json()).response.players[0] if (!profile.gameid) { throw "no game is being played" } diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts index 89a2d1a..f6c9bd9 100644 --- a/src/routes/+layout.server.ts +++ b/src/routes/+layout.server.ts @@ -3,9 +3,9 @@ import { incrementVisitCount, notifyDarkVisitors } from '$lib/visits.js'; export const csr = true; export const ssr = true; export const prerender = false; -export const trailingSlash = 'always'; +export const trailingSlash = 'always'; -export async function load({ request, cookies, url, setHeaders, fetch }) { +export async function load({ request, cookies, url, setHeaders }) { notifyDarkVisitors(url, request) // no await so it doesnt block load setHeaders({ 'Cache-Control': 'no-cache' })