[+] Get audio url

This commit is contained in:
2025-11-21 00:50:29 +08:00
parent aba5c5b7c2
commit 10ba316e3e
3 changed files with 33 additions and 3 deletions
+2 -1
View File
@@ -24,4 +24,5 @@ vite.config.ts.timestamp-*
data/
public/assets/fonts/cache
.idea
.idea
static/audio
+28
View File
@@ -6,6 +6,10 @@ import { franc } from 'franc'
import { error } from '@sveltejs/kit'
import type { ObjectId } from 'mongodb'
import '../../shared/ext'
import { promises as fs } from 'fs'
import path from 'path'
const CACHE_DIR = path.resolve('static/audio')
/**
* Functional wrapper to cache API results to MongoDB.
@@ -89,6 +93,30 @@ export const getLyricsProcessed = cached('lyrics_processed',
return aiParseLyrics(raw.lrc.lyric)
})
export const getSongUrl = async (id: number | string) => {
await fs.mkdir(CACHE_DIR, { recursive: true })
const filePath = path.join(CACHE_DIR, `${id}/standard.mp3`)
const publicUrl = `/audio/${id}/standard.mp3`
if (await fs.exists(filePath)) return publicUrl
console.log(`Downloading song ${id}...`)
// @ts-ignore
const res = await ne.song_url_v1({ id: id.toString(), level: 'standard' })
const url = (res.body as any).data?.[0]?.url
if (!url) throw error(404, 'Song URL not found')
const audioRes = await fetch(url)
if (!audioRes.ok) throw error(500, 'Failed to download song')
const buffer = await audioRes.arrayBuffer()
await fs.writeFile(filePath, Buffer.from(buffer))
console.log(`Song ${id} cached to ${filePath}`)
return publicUrl
}
export interface ImportSession {
id: string
+3 -2
View File
@@ -1,9 +1,10 @@
import type { PageServerLoad } from './$types'
import { getLyricsProcessed, getSongRaw, listRecPlaylists } from "$lib/server/songs.ts";
import { getLyricsProcessed, getSongRaw, getSongUrl } from "$lib/server/songs.ts";
export const load: PageServerLoad = async ({ params }) => {
const songId = +params.id
const song = await getSongRaw(songId)
const lrc = await getLyricsProcessed(songId)
return { song, lrc }
const audioUrl = await getSongUrl(songId)
return { song, lrc, audioUrl }
}