Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

API:https://nishui-MusicAPI2.web.val.run?id=xxx

id 是网易云的音乐 id 值,使用此 API 将会访问网页的两个 API 去获取音乐信息和歌词信息

我们用 req.url 获取 API 的url,然后再获取 url 的参数 id,这样就获取到对应的参数 id 了

2024年03月25日 不知道什么原因,获取 song 的 detail 失败,说是 querystring/url must match format \"uri\

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { fetch } from "https://esm.town/v/std/fetch";
export default async function(req: Request): Promise<Response> {
const url = new URL(req.url);
const Params = url.searchParams;
const id = Params.get("id");
if (!id) {
return Response.json("id no null", { status: 400 });
}
console.log(id);
console.log(`https://music.163.com/api/v3/song/detail?id=${id}&c=[{"id":"${id}"}]`);
var uri = `https://music.163.com/api/v3/song/detail?id=${id}&c=[{"id":"${id}"}]`;
var encodedUri = encodeURI(uri);
console.log("Test URI: " + encodedUri);
let data, data1, data2;
try {
[data1, data2] = await Promise.all([
fetch(encodedUri),
fetch(`https://music.163.com/api/song/media?id=${id}`),
]);
// 获取 JSON 格式的数据
const jsonData1 = await data1.json();
const jsonData2 = await data2.json();
// 合并数据成一个 JSON 对象
const mergedJson = {
musicInfo: jsonData1,
lyricsInfo: jsonData2,
};
// 返回合并后的 JSON 对象
return Response.json(mergedJson);
} catch (error) {
// 处理错误...
console.error(error);
return Response.json("Internal Server Error", { status: 500 });
}
}
nishui-musicapi2.web.val.run
April 20, 2024