feat: better station list

This commit is contained in:
2025-05-29 16:48:04 +02:00
parent 4e3a014040
commit d6f884a83d
5 changed files with 47 additions and 34 deletions

View File

@@ -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[],

View File

@@ -0,0 +1,7 @@
import type { Song } from "@/types";
export async function getChillhopStation(id: number): Promise<Song[]> {
const res = await fetch(`https://stream.chillhop.com/live/${id}`);
const data = await res.json() as Song[];
return data;
}

17
src/lib/stations/index.ts Normal file
View File

@@ -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,
};

View File

@@ -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<string[]> {
const res = await fetch('https://lofi-cdn.srizan.dev/sleep/list.json');
const data = await res.json();
return data;
}
async finalData(): Promise<Song[]> {
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;
}
}
export async function getSleepStationSongs(): Promise<Song[]> {
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;
}

View File

@@ -85,9 +85,3 @@ export interface BRStation {
Name: string;
Source: string;
}
export interface StationClass {
stationId: number;
getFiles(): Promise<string[]>;
finalData(): Promise<Song[]>;
}