forked from all-in-aigc/melodisco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudio.ts
76 lines (61 loc) · 1.68 KB
/
udio.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const apiBaseUri = "https://www.udio.com";
export async function getTrendingSongs(page: number, page_size: number) {
return getSongs(page, page_size, "trending_score");
}
export async function getLatestSongs(page: number, page_size: number) {
return getSongs(page, page_size, "created_at");
}
export async function getSongInfo(ids: string[]) {
try {
const uri = `${apiBaseUri}/api/songs?songIds=${encodeURIComponent(
ids.join(",")
)}`;
const headers = await getReqHeaders();
const resp = await fetch(uri, {
headers: headers,
});
console.log("res", headers, resp);
const data = await resp.json();
return data;
} catch (e) {
console.log("get song info failed: ", e);
}
}
async function getSongs(page: number, page_size: number, sort: string) {
if (!page) {
page = 1;
}
if (!page_size) {
page_size = 30;
}
try {
const uri = `${apiBaseUri}/api/songs/search`;
const headers = await getReqHeaders();
const params = {
searchQuery: { sort: sort, searchTerm: "" },
pageParam: (page - 1) * page_size,
pageSize: page_size,
trendingId: process.env.UDIO_TRENDING_UUID,
};
const resp = await fetch(uri, {
method: "POST",
headers: headers,
body: JSON.stringify(params),
});
const data = await resp.json();
return data;
} catch (e) {
console.log("get songs failed:", e);
return [];
}
}
async function getReqHeaders(): Promise<any> {
const userAgent = process.env.UDIO_UA;
const headers = {
cookie: process.env.UDIO_COOKIE,
"user-agent": userAgent,
origin: "https://www.udio.com",
referer: "https://www.udio.com/",
};
return headers;
}