Avatar

rebelpotato

4 public vals
Joined December 4, 2023

A simple example script that selects a bilibili video at random* and watches it.

It requires a cookie from the bilibili website, so after you fork this script, change the cookie accordingly.

Maybe turn it into a library next?

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
import { get_random_video } from "./get_random_bilibili_video";
import { watch_video } from "./watch_bilibili_video";
const sampleCookie = {
sessdata:
"5c6ffd9d%2C1717253794%2Ccd556%2Ac2CjCX5GidJEoMKP81DL-XwWDjyYvgFAqV-cpy7A7HH75amBHZC_NuCglf4tYLBVHKNrwSVmNKMW1QSnl5amxSX285alpGMkE2bzg1UnFQNnR3c0d0Y3NVazQ2NVI3ZUhjcW9namMybnFPZHQ1RGhDN2xlTUZfX3ZMRm05aUY2cFhseDB6R2lyMkpRIIEC",
bilijct: "7b989ab265c613f1795995f67b595b15",
dedeuserid: "2008607578",
};
function brief(video) {
return {
tname: video.tname,
title: video.title,
owner: {
name: video.owner.name,
},
stat: {
view: video.stat.view,
reply: video.stat.reply,
favorite: video.stat.favorite,
coin: video.stat.coin,
share: video.stat.share,
},
aid: video.aid,
bvid: video.bvid,
short_link_v2: video.short_link_v2,
};
}
async function watch_random_video(cookie) {
try {
let video = await get_random_video(cookie);
console.log("Watching video:");
console.log(brief(video));
await watch_video(video, cookie);
console.log("Successful.");
}
catch (error) {
console.error(error);
}
}
// get_bilibili(video_url);
watch_random_video(sampleCookie);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { post_bilibili } from "./bilibili_methods";
function randint(x) {
return Math.floor(Math.random() * x);
}
const watch_video_url = "http://api.bilibili.com/x/v2/history/report";
// watch a bilibili video for a random time less than its duration
export async function watch_video(video, cookie) {
let watch_video_data = {
aid: video.aid,
cid: video.cid,
progress: randint(video.duration),
csrf: cookie.bilijct,
};
await post_bilibili(watch_video_url, watch_video_data, cookie);
// if no error has occured, then a video has been watched successfully.
}
1
2
3
4
5
6
7
8
9
10
11
12
13
import { get_bilibili } from "https://esm.town/v/rebelpotato/bilibili_methods";
function randint(x) {
return Math.floor(Math.random() * x);
}
const video_url = "https://api.bilibili.com/x/web-interface/dynamic/region?ps=6&rid=1";
export async function get_random_video(cookie) {
let videos = (await get_bilibili(video_url, cookie)).archives;
let video = videos[randint(videos.length)];
return video;
}

Methods for accessing the bilibili api with a cookie.

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
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),
})
.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 post request manually?",
);
}
return true;
});
}
Next