website/src/lib/index.ts

50 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-10-01 02:44:39 +03:00
import type { Cookies } from '@sveltejs/kit'
2024-10-01 04:59:41 +03:00
import { env } from '$env/dynamic/private'
import { get, writable } from 'svelte/store'
import { existsSync, readFileSync } from 'fs'
2024-10-01 02:44:39 +03:00
export const scopeCookies = (cookies: Cookies, path: string) => {
return {
get: (key: string) => {
return cookies.get(key)
},
set: (key: string, value: string, props: import('cookie').CookieSerializeOptions = {}) => {
cookies.set(key, value, { ...props, path })
},
delete: (key: string, props: import('cookie').CookieSerializeOptions = {}) => {
cookies.delete(key, { ...props, path })
}
}
2024-10-01 04:34:07 +03:00
}
2024-10-01 04:59:41 +03:00
export const visitCountFile = `${env.WEBSITE_DATA_DIR}/visitcount`
export const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0'));
2024-10-30 16:33:08 +03:00
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<LastTrack | null> = 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
}
}