feat: api reworks

This commit is contained in:
2025-05-31 17:24:12 +02:00
parent 8aeba1e87c
commit 3c9ddd466d
5 changed files with 26 additions and 21 deletions

View File

@@ -218,7 +218,7 @@
{#if !appState.isLoading}
<audio
bind:this={audioElement}
src={appState.currentSong!.endpoint || `https://stream.chillhop.com/mp3/${appState.currentSong!.fileId}`}
src={appState.currentSong!.endpoint}
autoplay
volume={appState.volume}
ontimeupdate={checkTimeAndPrepareNextSong}

View File

@@ -1,7 +1,18 @@
import type { Song } from "@/types";
import type { CHSong, 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;
const data = await res.json() as CHSong[];
const finalData = data.map(song => ({
artists: song.artists,
title: song.title,
endpoint: `https://stream.chillhop.com/mp3/${song.fileId}`,
image: song.image,
label: 'Chillhop Music',
spotifyId: song.spotifyId,
duration: song.duration,
})) as Song[];
return finalData;
}

View File

@@ -10,12 +10,12 @@ export async function getSleepStationSongs(): Promise<Song[]> {
const mapped = files.map(file => {
const [artist, title] = file.replace('.opus', '').split(' - ');
return {
id: 727,
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`,
label: 'Chilled Cat',
};
}) as Song[];

View File

@@ -1,4 +1,14 @@
export interface Song {
artists: string;
title: string;
endpoint: string;
image: string;
label?: string; // optional record label
spotifyId?: string; // TODO: enforce in the future for all spotify scraped stations
duration?: number; // TODO: enforce in all stations
}
export interface CHSong {
id: number;
fileId: number | string;
endpoint?: string;

View File

@@ -25,19 +25,3 @@ export async function getStationSongs(stationId: number) {
return data;
}
export function setSongTime() {
if (!state.currentSong!.startTime) {
state.currentTime = 0;
return;
}
const currentTime = new Date().getTime() / 1000;
const startTime = new Date(state.currentSong!.startTime).getTime() / 1000;
const endTime = new Date(state.currentSong!.endTime!).getTime() / 1000;
const duration = endTime - startTime;
const elapsed = currentTime - startTime;
if (elapsed > 0 && elapsed < duration) {
state.currentTime = elapsed;
} else {
state.currentTime = 0;
}
}