· 5 years ago · Oct 19, 2020, 01:54 PM
1let API = "https://api.bilibili.com";
2let cookies = {};
3for (let index = 0; index < document.cookie.split("; ").length; index++) {
4 const element = document.cookie.split("; ")[index].split('=');
5 cookies[element[0]] = element[1];
6};
7function fromData(Data) {
8 let str = "";
9 for (var key in Data) {
10 str += key + '=' + Data[key] + '&';
11 };
12 return str.substr(0, str.length - 1);
13};
14function arrSubtraction(a, b) {
15 if (Array.isArray(a) && Array.isArray(b)) {
16 return a.filter(el => !~b.indexOf(el));
17 } else {
18 return [];
19 };
20};
21function sleep(fn, par) {
22 return new Promise((resolve) => {
23 setTimeout(() => resolve(fn(par["parameter"])), par["delay"]);
24 });
25};
26async function getData(url, data) {
27 let responseData;
28 await fetch(url + "?" + fromData(data), {
29 headers: {
30 "accept": "application/json, text/plain, */*",
31 "content-type": "application/x-www-form-urlencoded",
32 },
33 credentials: 'include',
34 method: 'GET',
35 mode: 'cors',
36 redirect: 'follow'
37 }).then(function (response) {
38 responseData = response.text();
39 }).catch(error => function () {
40 console.log(error);
41 });
42 return responseData;
43};
44async function postData(url, data) {
45 let responseData;
46 await fetch(url, {
47 headers: {
48 "accept": "application/json, text/plain, */*",
49 "content-type": "application/x-www-form-urlencoded"
50 },
51 credentials: 'include',
52 method: 'POST',
53 mode: 'cors',
54 redirect: 'follow',
55 body: fromData(data)
56 }).then(function (response) {
57 responseData = response.text();
58 }).catch(error => function () {
59 console.log(error);
60 });
61 return responseData;
62};
63//获取hololive官方账号信息
64let hololive = JSON.parse(await getData(API + '/x/space/acc/info', {
65 "mid": 286700005,
66 "jsonp": "jsonp"
67}));
68hololive["data"]["uname"] = hololive["data"]["name"];
69//获取hololive官方账号关注列表
70let subscribeList = JSON.parse(await getData(API + '/x/relation/followings', {
71 "vmid": 286700005,
72 "pn": 1,
73 "ps": 100,
74 "jsonp": "jsonp"
75}));
76//取关uid列表初始化并添加hololive官方账号信息
77let unsubscribeList = [];
78subscribeList["data"]["list"].push(hololive["data"]);
79for (let index = 0; index < subscribeList["data"]["list"].length; index++) {
80 const element = subscribeList["data"]["list"][index];
81 unsubscribeList.push(element["mid"]);
82};
83//hololive官方账号关注的用户:狗妈、哔哩哔哩线下活动、BML制作指挥部、超电VUP官方账号
84let whitelist = [2306780, 403748305, 597066058, 386900246];
85//在取关uid列表中排除以上白名单
86unsubscribeList = arrSubtraction(unsubscribeList, whitelist);
87//历遍取关uid列表并执行取关
88for (let i = 0; i < unsubscribeList.length; i++) {
89 let response = JSON.parse(await postData(API + '/x/relation/modify', {
90 "fid": unsubscribeList[i],
91 "act": 2,
92 "re_src": 11,
93 "jsonp": "jsonp",
94 "csrf": cookies["bili_jct"]
95 }));
96 if (response["code"] == 0) {
97 if (subscribeList["data"]["list"].filter(element => element["mid"] == unsubscribeList[i]).length < 0) {
98 console.log("[UID:" + unsubscribeList[i] + "] 取关成功!");
99 } else {
100 console.log("[" + (subscribeList["data"]["list"].filter(element => element["mid"] == unsubscribeList[i]))[0]["uname"] + "] 取关成功!");
101 };
102 } else {
103 if (subscribeList["data"]["list"].filter(element => element["mid"] == unsubscribeList[i]).length < 0) {
104 console.log("[UID:" + unsubscribeList[i] + "] 取关失败!");
105 } else {
106 console.log("[" + (subscribeList["data"]["list"].filter(element => element["mid"] == unsubscribeList[i]))[0]["uname"] + "] 取关失败!");
107 };
108 };
109 await sleep(function () { }, { "parameter": "", "delay": 200 });
110};
111//历遍取关uid列表并执行拉黑
112for (let i = 0; i < unsubscribeList.length; i++) {
113 let response = JSON.parse(await postData(API + '/x/relation/modify', {
114 "fid": unsubscribeList[i],
115 "act": 5,
116 "re_src": 11,
117 "jsonp": "jsonp",
118 "csrf": cookies["bili_jct"]
119 }));
120 if (response["code"] == 0) {
121 if (subscribeList["data"]["list"].filter(element => element["mid"] == unsubscribeList[i]).length < 0) {
122 console.log("[UID:" + unsubscribeList[i] + "] 拉黑成功!");
123 } else {
124 console.log("[" + (subscribeList["data"]["list"].filter(element => element["mid"] == unsubscribeList[i]))[0]["uname"] + "] 拉黑成功!");
125 };
126 } else {
127 if (subscribeList["data"]["list"].filter(element => element["mid"] == unsubscribeList[i]).length < 0) {
128 console.log("[UID:" + unsubscribeList[i] + "] 拉黑失败!" + response["message"]);
129 } else {
130 console.log("[" + (subscribeList["data"]["list"].filter(element => element["mid"] == unsubscribeList[i]))[0]["uname"] + "] 拉黑失败!" + response["message"]);
131 };
132 };
133 await sleep(function () { }, { "parameter": "", "delay": 200 });
134};