mirror of
https://github.com/SrIzan10/lofi.git
synced 2026-06-06 00:56:53 +00:00
feat: optimize and use new object
This commit is contained in:
@@ -79,7 +79,7 @@
|
|||||||
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
|
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class SIMDMersenneTwister {
|
export class SIMDMersenneTwister {
|
||||||
/* SFMT period parameters for SFMT19937 */
|
/* SFMT period parameters for SFMT19937 */
|
||||||
private readonly MEXP = 19937;
|
private readonly MEXP = 19937;
|
||||||
private readonly N = Math.floor(this.MEXP / 128) + 1; // 156
|
private readonly N = Math.floor(this.MEXP / 128) + 1; // 156
|
||||||
|
|||||||
@@ -1,17 +1,28 @@
|
|||||||
|
import type { Song } from '@/types';
|
||||||
import { getChillhopStation } from './chillhop';
|
import { getChillhopStation } from './chillhop';
|
||||||
import { getSleepStationSongs } from './sleep';
|
import { getSleepStationSongs } from './sleep';
|
||||||
|
|
||||||
const customStations = {
|
const customStations: Record<number, () => Promise<Song[]>> = {
|
||||||
50000: getSleepStationSongs,
|
50000: getSleepStationSongs,
|
||||||
};
|
};
|
||||||
const chillhopStations = Object.fromEntries(
|
|
||||||
Array.from({ length: 100 }, (_, i) => {
|
|
||||||
const stationId = 10000 + i;
|
|
||||||
return [stationId, getChillhopStation.bind(null, stationId)];
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
export const stations = {
|
export const stations: Record<number, () => Promise<Song[]>> = new Proxy(customStations, {
|
||||||
...customStations,
|
get(target, prop) {
|
||||||
...chillhopStations,
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { Song } from "@/types";
|
import type { Song } from "@/types";
|
||||||
|
import { SIMDMersenneTwister } from "@/mersenne";
|
||||||
|
|
||||||
export async function getSleepStationSongs(): Promise<Song[]> {
|
export async function getSleepStationSongs(): Promise<Song[]> {
|
||||||
const m = new SIMDMersenneTwister();
|
const m = new SIMDMersenneTwister();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { Song } from '@/types.js';
|
import { stations } from '@/stations/index.js';
|
||||||
|
|
||||||
export async function GET(event) {
|
export async function GET(event) {
|
||||||
const { stationId } = event.params;
|
const { stationId } = event.params;
|
||||||
@@ -7,36 +7,15 @@ export async function GET(event) {
|
|||||||
return new Response('Invalid station ID', { status: 400 });
|
return new Response('Invalid station ID', { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stationId.startsWith('50')) {
|
const stationFunction = stations[stationIdInt];
|
||||||
// custom stations
|
if (!stationFunction) {
|
||||||
if (stationIdInt === 50000) {
|
return new Response('Station not found', { status: 404 });
|
||||||
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 songs = await stationFunction();
|
||||||
|
return new Response(JSON.stringify(songs), {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user