diff --git a/src/lib/index.ts b/src/lib/index.ts index 6a31d0f..12485ed 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -18,33 +18,4 @@ export const scopeCookies = (cookies: Cookies, path: string) => { } export const visitCountFile = `${env.WEBSITE_DATA_DIR}/visitcount` -export const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0')); - -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 - } -} \ No newline at end of file +export const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0')); \ No newline at end of file diff --git a/src/lib/lastfm.ts b/src/lib/lastfm.ts new file mode 100644 index 0000000..18752d8 --- /dev/null +++ b/src/lib/lastfm.ts @@ -0,0 +1,30 @@ +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} +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 + } +} \ No newline at end of file diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index a846e0e..38119f4 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,4 +1,4 @@ -import { lastFmGetNowPlaying } from "$lib" +import { lastFmGetNowPlaying } from "$lib/lastfm" import { steamGetNowPlaying } from "$lib/steam" export const load = async ({}) => {