Paul-API-Next/src/utils/netease.ts

57 lines
1.5 KiB
TypeScript

import fetch from "isomorphic-unfetch";
export const getSong = async (id: number) => {
var urlencoded = new URLSearchParams();
urlencoded.append("ids", `['${id}']`);
return fetch("http://music.163.com/api/song/detail/", {
method: "POST",
body: urlencoded,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept-language": "en",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
}
}).then(res => {
try{
return res.json();
}
catch(e){
// Todo: 这个地方可能会有问题
return undefined;
}
}).then(json => {
return json ? parseSongData(json.songs[0]) : undefined;
})
}
export const getLyric = async (id: number) => {
return fetch(`http://music.163.com/api/song/lyric?id=${id}&lv=-1&kv=-1&tv=-1`).then(res => {
try{
return res.json();
}
catch(e){
// Todo: 这个地方可能会有问题
return undefined;
}
}).then(json => {
return json ? [json.lrc ?. lyric, json.tlyric ?. lyric || ""] : ["", ""];
})
}
export const parseSongData = async (item: any) => {
const [lyric, sub_lyric] = await getLyric(item.id);
return {
id: item.id,
title: item.name,
artist: item.artists ?. [0].name || "",
album: item.album.name || "",
cover: `${item.album.picUrl.replace("http://", "https://")}?param=250y250"`,
lyric,
sub_lyric,
link: `https://music.163.com/song/media/outer/url?id=${item.id}`,
served: false
}
}