From d6f884a83d78dac4b98325d891ccaf72447e0362 Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Thu, 29 May 2025 16:48:04 +0200 Subject: [PATCH] feat: better station list --- src/lib/state.svelte.ts | 2 +- src/lib/stations/chillhop.ts | 7 ++++++ src/lib/stations/index.ts | 17 +++++++++++++ src/lib/stations/sleep.ts | 49 ++++++++++++++++-------------------- src/lib/types.ts | 6 ----- 5 files changed, 47 insertions(+), 34 deletions(-) create mode 100644 src/lib/stations/chillhop.ts create mode 100644 src/lib/stations/index.ts diff --git a/src/lib/state.svelte.ts b/src/lib/state.svelte.ts index b239225..1676588 100644 --- a/src/lib/state.svelte.ts +++ b/src/lib/state.svelte.ts @@ -14,7 +14,7 @@ export const state = $state({ isLoading: true, error: null as string | null, currentTime: 0, - duration: 0, + duration: 0 as number | undefined, presets: [] as Preset[], stations: [] as Station[], diff --git a/src/lib/stations/chillhop.ts b/src/lib/stations/chillhop.ts new file mode 100644 index 0000000..2564fdb --- /dev/null +++ b/src/lib/stations/chillhop.ts @@ -0,0 +1,7 @@ +import type { Song } from "@/types"; + +export async function getChillhopStation(id: number): Promise { + const res = await fetch(`https://stream.chillhop.com/live/${id}`); + const data = await res.json() as Song[]; + return data; +} diff --git a/src/lib/stations/index.ts b/src/lib/stations/index.ts new file mode 100644 index 0000000..8fa9888 --- /dev/null +++ b/src/lib/stations/index.ts @@ -0,0 +1,17 @@ +import { getChillhopStation } from './chillhop'; +import { getSleepStationSongs } from './sleep'; + +const customStations = { + 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, +}; diff --git a/src/lib/stations/sleep.ts b/src/lib/stations/sleep.ts index 2272bd5..5b33ce6 100644 --- a/src/lib/stations/sleep.ts +++ b/src/lib/stations/sleep.ts @@ -1,28 +1,23 @@ -import type { Song, StationClass } from "@/types"; +import type { Song } from "@/types"; -export class SleepStation implements StationClass { - stationId = 50000; - private m = new MersenneTwister(); - async getFiles(): Promise { - const res = await fetch('https://lofi-cdn.srizan.dev/sleep/list.json'); - const data = await res.json(); - return data; - } - - async finalData(): Promise { - const files = await this.getFiles(); - const mapped = files.map(file => { - const [artist, title] = file.replace('.opus', '').split(' - '); - return { - id: parseInt((this.m.random() * 1000000).toFixed(0)), - fileId: file.replace('.opus', ''), - endpoint: `https://lofi-cdn.srizan.dev/sleep/${file}`, - artists: artist, - title: title, - image: `https://lofi-cdn.srizan.dev/sleep/thumbs/${file.replace('.opus', '')}.webp`, - }; - }) as Song[]; - const shuffled = mapped.sort(() => 0.5 - this.m.random()).slice(0, 5); - return shuffled; - } -} \ No newline at end of file +export async function getSleepStationSongs(): Promise { + const m = new MersenneTwister(); + + const res = await fetch('https://lofi-cdn.srizan.dev/sleep/list.json'); + const files = await res.json() as string[]; + + const mapped = files.map(file => { + const [artist, title] = file.replace('.opus', '').split(' - '); + return { + id: parseInt((m.random() * 1000000).toFixed(0)), + fileId: file.replace('.opus', ''), + endpoint: `https://lofi-cdn.srizan.dev/sleep/${file}`, + artists: artist, + title: title, + image: `https://lofi-cdn.srizan.dev/sleep/thumbs/${file.replace('.opus', '')}.webp`, + }; + }) as Song[]; + + const shuffled = mapped.sort(() => 0.5 - m.random()).slice(0, 5); + return shuffled; +} diff --git a/src/lib/types.ts b/src/lib/types.ts index 78325ad..7bec215 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -85,9 +85,3 @@ export interface BRStation { Name: string; Source: string; } - -export interface StationClass { - stationId: number; - getFiles(): Promise; - finalData(): Promise; -} \ No newline at end of file