-
Notifications
You must be signed in to change notification settings - Fork 41
/
biliapi.js
232 lines (203 loc) · 7.9 KB
/
biliapi.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const BILIBILI_API_URL = "https://api.bilibili.com"
const NUM_PER_PAGE = 50
async function getUserIdFromVideoLink(videoLink) {
let regex = /.*?bilibili.com\/video\/(.*)$/;
let bvid = videoLink.match(regex)[1];
return await fetch(`${BILIBILI_API_URL}/x/web-interface/view?bvid=${bvid}`)
.then((response) => response.json())
.then((data) => {
return data["data"]["owner"]["mid"];
})
}
userInfoCache = new Map();
function updateWordMap(map, sentence, weight) {
// Remove all URLs
sentence = sentence.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '');
for (let word of IGNORE_WORDS) {
sentence = sentence.replaceAll(word, '');
}
let results = Array.from(new Intl.Segmenter('cn', { granularity: 'word' }).segment(sentence));
let wordMap = map.get("word");
for (let result of results) {
if (result.isWordLike) {
let word = result["segment"];
if (word && !STOP_WORDS.has(word)) {
if (wordMap.has(word)) {
wordMap.set(word, wordMap.get(word) + weight);
} else {
wordMap.set(word, weight);
}
}
}
}
}
function updateTypeMap(map, type) {
let typeMap = map.get("type");
if (typeMap.has(type)) {
typeMap.set(type, typeMap.get(type) + 1);
} else {
typeMap.set(type, 1);
}
}
function videoLengthStringToSeconds(s) {
let regex = /([0-9]*):([0-9]*)/;
let match = s.match(regex);
if (match) {
return parseInt(match[1]) * 60 + parseInt(match[2]);
}
return 0;
}
function convertVideoData(map) {
let data = {};
let typeData = Array.from(map.get("type"));
typeData.sort((a, b) => b[1] - a[1]);
data["word"] = Array.from(map.get("word"));
data["type"] = typeData.slice(0, 3);
return data;
}
async function requestSearchPage(userId, pn, map) {
return fetch(`${BILIBILI_API_URL}/x/space/wbi/arc/search?mid=${userId}&pn=${pn}&ps=${NUM_PER_PAGE}&index=1&order=pubdate&order_avoided=true`)
.then((response) => response.json())
.then((data) => {
if (data["code"] == 0) {
for (let v of data["data"]["list"]["vlist"]) {
updateWordMap(map, v["description"], 1);
updateWordMap(map, v["title"], 3);
updateTypeMap(map, v["typeid"]);
if (v["created"] > Date.now() / 1000 - 30 * 24 * 60 * 60) {
map.set("lastMonthVideoCount", map.get("lastMonthVideoCount") + 1);
}
map.set("totalVideoLength", map.get("totalVideoLength") + videoLengthStringToSeconds(v["length"]));
}
}
return data;
})
}
function updateVideoData(userId, callback) {
let map = new Map();
map.set("word", new Map());
map.set("type", new Map());
map.set("lastMonthVideoCount", 0);
map.set("totalVideoLength", 0);
requestSearchPage(userId, 1, map).then((data) => {
if (data["code"] == 0) {
let count = data["data"]["page"]["count"];
cacheAndUpdate(callback, userId, "count", {"count": count});
if (count > 0) {
let lastVideoTimestamp = data["data"]["list"]["vlist"][0]["created"];
cacheAndUpdate(callback, userId, "lastVideoTimestamp", {"timestamp": lastVideoTimestamp});
} else {
cacheAndUpdate(callback, userId, "lastVideoTimestamp", {"timestamp": null});
}
if (biliScopeOptions.enableWordCloud) {
let promises = [];
if (count > NUM_PER_PAGE) {
let pn = 2;
while (pn * NUM_PER_PAGE < count) {
promises.push(requestSearchPage(userId, pn, map));
pn += 1;
}
Promise.all(promises).then((values) => {
cacheAndUpdate(callback, userId, "wordcloud", convertVideoData(map));
cacheAndUpdate(callback, userId, "totalVideoInfo", {
"lastMonthCount": map.get("lastMonthVideoCount"),
"totalLength": map.get("totalVideoLength")});
})
} else {
cacheAndUpdate(callback, userId, "wordcloud", convertVideoData(map));
cacheAndUpdate(callback, userId, "totalVideoInfo", {
"lastMonthCount": map.get("lastMonthVideoCount"),
"totalLength": map.get("totalVideoLength")});
}
}
} else {
cacheAndUpdate(callback, userId, "count", {"count": null});
cacheAndUpdate(callback, userId, "wordcloud", {"word": [], "type": []});
cacheAndUpdate(callback, userId, "totalVideoInfo", {"lastMonthCount": null, "totalLength": null});
}
});
}
function cacheValid(cache) {
for (let key of ["stat", "info", "wordcloud", "count"]) {
if (!cache[key]) {
return false;
}
}
return true;
}
function cacheAndUpdate(callback, userId, api, payload) {
let cache = {};
if (!userInfoCache.has(userId)) {
userInfoCache.set(userId, cache);
} else {
cache = userInfoCache.get(userId);
}
cache[api] = payload;
callback({"uid": userId, "api": api, "payload": payload});
}
function updateUserInfo(userId, callback) {
this._prevUserId = null;
if (this._prevUserId != userId) {
if (userInfoCache.has(userId) && cacheValid(userInfoCache.get(userId))) {
let cache = userInfoCache.get(userId);
for (let api in cache) {
callback({"uid": userId, "api": api, "payload": cache[api]});
}
} else {
fetch(`${BILIBILI_API_URL}/x/relation/stat?vmid=${userId}&jsonp=jsonp`,)
.then((response) => response.json())
.then((data) => cacheAndUpdate(callback, userId, "stat", data));
fetch(`${BILIBILI_API_URL}/x/space/wbi/acc/info?mid=${userId}`,)
.then((response) => response.json())
.then((data) => cacheAndUpdate(callback, userId, "info", data));
fetch(`${BILIBILI_API_URL}/x/space/acc/relation?mid=${userId}`, {credentials: "include"})
.then((response) => response.json())
.then((data) => {
if (data["code"] == 0) {
cacheAndUpdate(callback, userId, "relation", data)
}
});
updateVideoData(userId, callback);
}
}
}
async function requestGuardPage(roomId, uid, pn, map) {
return fetch(`https://api.live.bilibili.com/xlive/app-room/v2/guardTab/topList?roomid=${roomId}&page=${pn}&ruid=${uid}&page_size=30`)
.then((response) => response.json())
.then((data) => {
if (data["code"] == 0) {
for (let u of data["data"]["top3"]) {
map.set(u.uid, u);
}
for (let u of data["data"]["list"]) {
map.set(u.uid, u);
}
}
return data;
})
}
async function getGuardInfo(roomId, uid) {
let map = new Map();
let promises = [];
let pn = 1;
return requestGuardPage(roomId, uid, pn, map).then((data) => {
if (data["code"] == 0) {
let count = data["data"]["info"]["num"];
if (count > 30) {
let pn = 2;
while (pn * 30 < count) {
promises.push(requestGuardPage(roomId, uid, pn, map));
pn += 1;
}
return Promise.all(promises).then((values) => {
let data = Array.from(map.values());
return data
})
} else {
return Array.from(map.values());
}
} else {
return [];
}
});
}