refactor: better organize some code

This commit is contained in:
dusk 2024-11-23 03:00:36 +03:00
parent 3e640524e8
commit 22c773c38e
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw
3 changed files with 20 additions and 12 deletions

View File

@ -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<CachedLastTrack>({track: null, since: 0})
export const lastFmGetNowPlaying: () => Promise<LastTrack | null> = 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"

View File

@ -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<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}
const cachedLastGame = writable<CachedLastGame>({game: null, since: 0})
export const steamGetNowPlaying: () => Promise<LastGame | null> = async () => {
var griddbClient = get(steamgriddbClient)
if (griddbClient === null) {
@ -14,12 +19,11 @@ export const steamGetNowPlaying: () => Promise<LastGame | null> = 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"
}

View File

@ -5,7 +5,7 @@ export const ssr = true;
export const prerender = false;
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' })