diff --git a/src/lib/mersenne.ts b/src/lib/mersenne.ts index 5b816a1..2a468e3 100644 --- a/src/lib/mersenne.ts +++ b/src/lib/mersenne.ts @@ -79,7 +79,7 @@ email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) */ -class SIMDMersenneTwister { +export class SIMDMersenneTwister { /* SFMT period parameters for SFMT19937 */ private readonly MEXP = 19937; private readonly N = Math.floor(this.MEXP / 128) + 1; // 156 diff --git a/src/lib/stations/index.ts b/src/lib/stations/index.ts index 8fa9888..1503179 100644 --- a/src/lib/stations/index.ts +++ b/src/lib/stations/index.ts @@ -1,17 +1,28 @@ +import type { Song } from '@/types'; import { getChillhopStation } from './chillhop'; import { getSleepStationSongs } from './sleep'; -const customStations = { +const customStations: Record Promise> = { 50000: getSleepStationSongs, }; -const chillhopStations = Object.fromEntries( - Array.from({ length: 100 }, (_, i) => { - const stationId = 10000 + i; - return [stationId, getChillhopStation.bind(null, stationId)]; - }) -); -export const stations = { - ...customStations, - ...chillhopStations, -}; +export const stations: Record Promise> = new Proxy(customStations, { + get(target, prop) { + const stationId = Number(prop); + + if (target[stationId]) { + return target[stationId]; + } + + if (stationId >= 10000 && stationId < 20000) { + return () => getChillhopStation(stationId); + } + + return undefined; + }, + + has(target, prop) { + const stationId = Number(prop); + return target[stationId] !== undefined || (stationId >= 10000 && stationId < 20000); + } +}); diff --git a/src/lib/stations/sleep.ts b/src/lib/stations/sleep.ts index 3f22b8f..156e254 100644 --- a/src/lib/stations/sleep.ts +++ b/src/lib/stations/sleep.ts @@ -1,4 +1,5 @@ import type { Song } from "@/types"; +import { SIMDMersenneTwister } from "@/mersenne"; export async function getSleepStationSongs(): Promise { const m = new SIMDMersenneTwister(); diff --git a/src/routes/api/live/[stationId]/+server.ts b/src/routes/api/live/[stationId]/+server.ts index c61fcd9..c3d101e 100644 --- a/src/routes/api/live/[stationId]/+server.ts +++ b/src/routes/api/live/[stationId]/+server.ts @@ -1,4 +1,4 @@ -import type { Song } from '@/types.js'; +import { stations } from '@/stations/index.js'; export async function GET(event) { const { stationId } = event.params; @@ -7,36 +7,15 @@ export async function GET(event) { return new Response('Invalid station ID', { status: 400 }); } - if (stationId.startsWith('50')) { - // custom stations - if (stationIdInt === 50000) { - const data = await fetch('https://lofi-cdn.srizan.dev/sleep/list.json').then(async (res) => await res.json() as string[]); - const randomData = data.sort(() => 0.5 - Math.random()).slice(0, 5).map(song => { - const noOpus = song.replace('.opus', ''); - const [artist, title] = noOpus.split(' - '); - return { - id: parseInt((Math.random() * 1000000).toFixed(0)), - fileId: noOpus, - endpoint: `https://lofi-cdn.srizan.dev/sleep/${song}`, - artists: artist, - title: title, - image: `https://lofi-cdn.srizan.dev/sleep/thumbs/${noOpus}.webp`, - } as Song; - }); - return new Response(JSON.stringify(randomData), { - headers: { - 'Content-Type': 'application/json' - } - }); - } - } else { - // chillhop stations - const res = await fetch(`https://stream.chillhop.com/live/${stationId}`); - const data = await res.json(); - return new Response(JSON.stringify(data), { - headers: { - 'Content-Type': 'application/json' - } - }); + const stationFunction = stations[stationIdInt]; + if (!stationFunction) { + return new Response('Station not found', { status: 404 }); } + + const songs = await stationFunction(); + return new Response(JSON.stringify(songs), { + headers: { + 'Content-Type': 'application/json' + } + }); } \ No newline at end of file