Methods for accessing the bilibili api with a cookie.

Readme
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
function getBilibiliHeaders(cookie) {
return new Headers({
"connection": "keep-alive",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",
"referer": "https://www.bilibili.com/",
"Cookie": "SESSDATA=" + cookie.sessdata + "; bili_jct=" + cookie.bilijct + "; DedeUserID=" + cookie.dedeuserid,
});
}
function postBilibiliHeaders(cookie) {
return new Headers({
"connection": "keep-alive",
"referer": "https://www.bilibili.com/",
"accept": "application/json, text/plain, */*",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",
"charset": "UTF-8",
"Cookie": "SESSDATA=" + cookie.sessdata + "; bili_jct=" + cookie.bilijct + "; DedeUserID=" + cookie.dedeuserid,
});
}
export function get_bilibili(url, cookie) {
return fetch(url, {
method: "GET",
credentials: "include",
headers: getBilibiliHeaders(cookie),
})
.then(response => response.json())
.then(data => {
if (data.code != 0) {
throw new Error(
"Error when processing data, received data.code!=0. Check the result of the get request manually?",
);
}
return data.data;
});
}
function post_encode(params) {
return Object.keys(params).map(key => encodeURIComponent(key) + "=" + encodeURIComponent(params[key])).join("&");
}
export function post_bilibili(url, post_data, cookie) {
return fetch(url, {
method: "POST",
credentials: "include",
headers: postBilibiliHeaders(cookie),
body: post_encode(post_data),
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.