@floating-live/bilibili 0.15.3 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/parser.js CHANGED
@@ -1,422 +1,472 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseInfo = exports.parseMessage = void 0;
4
- const enums_1 = require("floating-live/enums");
5
- const utils_1 = require("floating-live/utils");
6
- // utils
7
- // 获取当前时间戳,以替代无法从数据中获取时间戳的情况,精度为1s
8
- function getDateTimestamp() {
9
- return Math.floor(Date.now() / 1000) * 1000;
10
- }
11
- const parsingFunction = {
12
- DANMU_MSG: (msg, room) => {
13
- const { info, msg_id } = msg;
14
- let content = info[1];
15
- let mode = info[0][1];
16
- let color = info[0][3];
17
- let uname = info[2][1];
18
- let uid = info[2][0];
19
- let timestamp = info[0][4];
20
- let guard_level = info[7]; // 舰长级别
21
- let extra = JSON.parse(info[0][15].extra);
22
- let medal = info[3][0] != null
23
- ? {
24
- level: info[3][0],
25
- name: info[3][1],
26
- // user: info[3][2],
27
- id: info[3][12],
28
- membership: info[3][10] || 0,
29
- }
30
- : null;
31
- let identity = 0;
32
- let emoticon = undefined;
33
- if (extra.emots) {
34
- emoticon = {};
35
- for (let key in extra.emots) {
36
- let emotItem = extra.emots[key];
37
- emoticon[key] = {
38
- id: emotItem.emoticon_unique,
39
- url: emotItem.url,
40
- name: emotItem.emoji,
41
- };
42
- }
43
- }
44
- if (info[2][2]) {
45
- identity = enums_1.UserType.admin;
46
- }
47
- else if (uid === room?.anchor.id) {
48
- identity = enums_1.UserType.anchor;
49
- }
50
- let danmaku = {
51
- platform: "bilibili",
52
- room: room?.id || 0,
53
- type: "comment",
54
- timestamp: timestamp,
55
- id: msg_id,
56
- info: {
57
- color: color,
58
- mode: mode,
59
- content: content,
60
- image: info[0][12]
61
- ? {
62
- name: content,
63
- id: info[0][13].emoticon_unique,
64
- url: info[0][13].url,
65
- size: info[0][13].width == info[0][13].height
66
- ? enums_1.ImageSize.large
67
- : enums_1.ImageSize.small,
68
- }
69
- : undefined,
70
- emoticon: emoticon,
71
- user: {
72
- name: uname,
73
- id: uid,
74
- medal: medal,
75
- membership: guard_level,
76
- type: identity,
77
- },
78
- },
79
- };
80
- danmaku.id ?? (danmaku.id = (0, utils_1.generateId)(danmaku));
81
- return danmaku;
82
- },
83
- INTERACT_WORD: (msg, room) => {
84
- const { data, msg_id, send_time } = msg;
85
- let type;
86
- switch (data.msg_type) {
87
- case 1:
88
- type = "entry"; // 进入直播间
89
- break;
90
- case 2:
91
- type = "follow"; // 关注直播间
92
- break;
93
- case 3:
94
- type = "share"; // 分享直播间
95
- break;
96
- default:
97
- return;
98
- }
99
- let interact = {
100
- platform: "bilibili",
101
- room: room?.id || 0,
102
- type: type,
103
- id: msg_id,
104
- timestamp: send_time || Math.floor(data.trigger_time / 1000000),
105
- info: {
106
- user: {
107
- name: data.uname,
108
- id: data.uid,
109
- medal: data.fans_medal?.medal_level
110
- ? {
111
- level: data.fans_medal.medal_level,
112
- name: data.fans_medal.medal_name,
113
- id: data.fans_medal.target_id,
114
- membership: data.fans_medal.guard_level,
115
- }
116
- : null,
117
- },
118
- },
119
- };
120
- interact.id ?? (interact.id = (0, utils_1.generateId)(interact));
121
- return interact;
122
- },
123
- SEND_GIFT: (msg, room) => {
124
- const { data, msg_id, send_time } = msg;
125
- let gift = {
126
- platform: "bilibili",
127
- room: room?.id || 0,
128
- type: "gift",
129
- id: msg_id || data.combo_id,
130
- timestamp: send_time || data.timestamp * 1000,
131
- info: {
132
- user: {
133
- name: data.uname,
134
- id: data.uid,
135
- medal: data.fans_medal && data.fans_medal.medal_level
136
- ? {
137
- level: data.fans_medal.medal_level,
138
- name: data.fans_medal.medal_name,
139
- id: data.fans_medal.target_id,
140
- membership: data.fans_medal.guard_level,
141
- }
142
- : null,
143
- membership: data.guard_level,
144
- avatar: data.face,
145
- },
146
- gift: {
147
- name: data.giftName,
148
- id: data.giftId,
149
- num: data.num,
150
- value: data.total_coin,
151
- currency: data.coin_type,
152
- action: data.action,
153
- comboId: data.batch_combo_id,
154
- },
155
- },
156
- };
157
- gift.id ?? (gift.id = (0, utils_1.generateId)(gift));
158
- return gift;
159
- },
160
- GUARD_BUY: (msg, room) => {
161
- const { data, msg_id, send_time } = msg;
162
- let gift = {
163
- platform: "bilibili",
164
- room: room?.id || 0,
165
- type: "membership",
166
- id: msg_id,
167
- timestamp: send_time || data.start_time * 1000,
168
- info: {
169
- user: {
170
- name: data.username,
171
- id: data.uid,
172
- membership: data.guard_level,
173
- },
174
- gift: {
175
- name: data.gift_name,
176
- id: data.gift_id,
177
- num: data.num,
178
- value: data.price,
179
- currency: "gold",
180
- },
181
- name: data.gift_name,
182
- level: data.guard_level,
183
- duration: data.num * 30,
184
- },
185
- };
186
- gift.id ?? (gift.id = (0, utils_1.generateId)(gift));
187
- return gift;
188
- },
189
- SUPER_CHAT_MESSAGE: (msg, room) => {
190
- const { data, msg_id, send_time } = msg;
191
- let sc = {
192
- platform: "bilibili",
193
- room: room?.id || 0,
194
- type: "superchat",
195
- id: msg_id,
196
- timestamp: send_time || data.ts * 1000,
197
- info: {
198
- id: data.id,
199
- user: {
200
- name: data.user_info.uname,
201
- id: data.uid,
202
- avatar: data.user_info.face,
203
- medal: data.medal_info && data.medal_info.medal_level
204
- ? {
205
- level: data.medal_info.medal_level,
206
- name: data.medal_info.medal_name,
207
- id: data.medal_info.target_id,
208
- membership: data.medal_info.guard_level,
209
- }
210
- : null,
211
- membership: data.user_info.guard_level,
212
- },
213
- content: data.message,
214
- color: data.background_bottom_color,
215
- duration: data.time * 1000,
216
- gift: {
217
- id: data.gift.gift_id,
218
- name: data.gift.gift_name,
219
- num: data.gift.num,
220
- value: data.price * 1000,
221
- currency: "gold",
222
- },
223
- },
224
- };
225
- sc.id ?? (sc.id = (0, utils_1.generateId)(sc));
226
- return sc;
227
- },
228
- WATCHED_CHANGE: (msg, room) => {
229
- const { data, msg_id, send_time } = msg;
230
- const stats = {
231
- platform: "bilibili",
232
- room: room?.id || 0,
233
- type: "live_stats",
234
- id: msg_id,
235
- timestamp: send_time || getDateTimestamp(),
236
- info: {
237
- view: data.num,
238
- },
239
- };
240
- stats.id ?? (stats.id = (0, utils_1.generateId)(stats));
241
- return stats;
242
- },
243
- LIKE_INFO_V3_UPDATE: (msg, room) => {
244
- const { data, msg_id, send_time } = msg;
245
- const stats = {
246
- platform: "bilibili",
247
- room: room?.id || 0,
248
- type: "live_stats",
249
- id: msg_id,
250
- timestamp: send_time || getDateTimestamp(),
251
- info: {
252
- like: data.click_count,
253
- },
254
- };
255
- stats.id ?? (stats.id = (0, utils_1.generateId)(stats));
256
- return stats;
257
- },
258
- ONLINE_RANK_COUNT: (msg, room) => {
259
- const { data, msg_id, send_time } = msg;
260
- if (data.online_count == null)
261
- return;
262
- const stats = {
263
- platform: "bilibili",
264
- room: room?.id || 0,
265
- type: "live_stats",
266
- id: msg_id,
267
- timestamp: send_time || getDateTimestamp(),
268
- info: {
269
- online: data.online_count,
270
- },
271
- };
272
- stats.id ?? (stats.id = (0, utils_1.generateId)(stats));
273
- return stats;
274
- },
275
- ROOM_BLOCK_MSG: (msg, room) => {
276
- const { data, msg_id, send_time } = msg;
277
- const block = {
278
- platform: "bilibili",
279
- room: room?.id || 0,
280
- type: "block",
281
- id: msg_id,
282
- timestamp: send_time || getDateTimestamp(),
283
- info: {
284
- user: {
285
- id: data.uid,
286
- name: data.uname,
287
- },
288
- operator: {
289
- type: data.operator == 2 ? enums_1.UserType.anchor : enums_1.UserType.admin,
290
- },
291
- },
292
- };
293
- block.id ?? (block.id = (0, utils_1.generateId)(block));
294
- return block;
295
- },
296
- LIVE: (msg, room) => {
297
- const { msg_id, send_time } = msg;
298
- // 直播间开播
299
- if (msg.live_time) {
300
- let live = {
301
- platform: "bilibili",
302
- room: room?.id || 0,
303
- type: "live_start",
304
- id: msg_id,
305
- timestamp: send_time || msg.live_time * 1000,
306
- info: {
307
- id: msg.live_key.toString(),
308
- },
309
- };
310
- live.id ?? (live.id = (0, utils_1.generateId)(live));
311
- return live;
312
- }
313
- },
314
- CUT_OFF: (msg, room) => {
315
- const { msg_id, send_time } = msg;
316
- // 直播间被切断
317
- let cut = {
318
- platform: "bilibili",
319
- room: room?.id || 0,
320
- type: "live_cut",
321
- id: msg_id,
322
- timestamp: send_time || getDateTimestamp(),
323
- info: {
324
- message: msg.msg,
325
- },
326
- };
327
- cut.id ?? (cut.id = (0, utils_1.generateId)(cut));
328
- return cut;
329
- },
330
- PREPARING: (msg, room) => {
331
- const { msg_id, send_time } = msg;
332
- let off = {
333
- platform: "bilibili",
334
- room: room?.id || 0,
335
- type: "live_end",
336
- id: msg_id,
337
- timestamp: send_time || getDateTimestamp(),
338
- info: {
339
- status: msg.round ? enums_1.RoomStatus.round : enums_1.RoomStatus.off,
340
- },
341
- };
342
- off.id ?? (off.id = (0, utils_1.generateId)(off));
343
- return off;
344
- },
345
- ROOM_CHANGE: (msg, room) => {
346
- const { data, msg_id, send_time } = msg;
347
- let change = {
348
- platform: "bilibili",
349
- room: room?.id || 0,
350
- type: "live_detail",
351
- id: msg_id,
352
- timestamp: send_time || getDateTimestamp(),
353
- info: {
354
- title: data.title,
355
- area: [data.parent_area_name, data.area_name],
356
- },
357
- };
358
- change.id ?? (change.id = (0, utils_1.generateId)(change));
359
- return change;
360
- },
361
- ANCHOR_LOT_START: (msg, room) => {
362
- const { data, msg_id, send_time } = msg;
363
- let m = {
364
- platform: "bilibili",
365
- room: room?.id || 0,
366
- type: "lottery",
367
- timestamp: send_time || data.current_time * 1000,
368
- id: msg_id,
369
- info: {
370
- id: data.id,
371
- award: {
372
- name: data.award_name,
373
- num: data.award_num,
374
- image: data.award_image,
375
- id: -1,
376
- value: 0,
377
- currency: "",
378
- },
379
- },
380
- };
381
- return m;
382
- },
383
- };
384
- /** 转换bilibili直播弹幕为floating通用格式 */
385
- function parseMessage(data, room) {
386
- return parsingFunction[data.cmd]?.(data, room);
387
- }
388
- exports.parseMessage = parseMessage;
389
- /** 转换bilibili直播间信息为floating通用格式 */
390
- function parseInfo(data) {
391
- const { room_info, anchor_info, like_info_v3, watched_show } = data;
392
- return {
393
- platform: "bilibili",
394
- id: room_info.room_id,
395
- roomId: room_info.room_id,
396
- shortId: room_info.short_id,
397
- key: `bilibili:${room_info.room_id}`,
398
- detail: {
399
- title: room_info.title,
400
- area: [room_info.parent_area_name, room_info.area_name],
401
- cover: room_info.cover,
402
- },
403
- anchor: {
404
- id: room_info.uid,
405
- name: anchor_info.base_info.uname,
406
- avatar: anchor_info.base_info.face,
407
- },
408
- stats: {
409
- view: watched_show.num,
410
- like: like_info_v3.total_likes,
411
- },
412
- liveId: room_info.live_id_str,
413
- available: room_info.lock_status ? false : true,
414
- status: room_info.lock_status ? enums_1.RoomStatus.banned : room_info.live_status,
415
- timestamp: room_info.live_status == enums_1.RoomStatus.live
416
- ? room_info.live_start_time * 1000
417
- : 0,
418
- opened: false,
419
- connection: 0,
420
- };
421
- }
422
- exports.parseInfo = parseInfo;
1
+ import { UserType, ImageSize, LiveRoomStatus, } from "floating-live";
2
+ // utils
3
+ const generateId = (m) => `${m.platform}:${m.roomId}-${m.type}:${m.userId}@${Date.now()}`;
4
+ // 获取当前时间戳,以替代无法从数据中获取时间戳的情况,精度为1s
5
+ function getDateTimestamp() {
6
+ return Math.floor(Date.now() / 1000) * 1000;
7
+ }
8
+ const parsingFunction = {
9
+ DANMU_MSG: (msg, room) => {
10
+ const { info, msg_id } = msg;
11
+ let content = info[1];
12
+ let mode = info[0][1];
13
+ let color = info[0][3];
14
+ let uname = info[2][1];
15
+ let uid = info[2][0];
16
+ let timestamp = info[0][4];
17
+ let guard_level = info[7]; // 舰长级别
18
+ let extra = JSON.parse(info[0][15].extra);
19
+ let medal = info[3][0] != null
20
+ ? {
21
+ level: info[3][0],
22
+ name: info[3][1],
23
+ // user: info[3][2],
24
+ id: info[3][12],
25
+ membership: info[3][10] || 0,
26
+ }
27
+ : null;
28
+ let identity = 0;
29
+ let emoticon = undefined;
30
+ if (extra.emots) {
31
+ emoticon = {};
32
+ for (let key in extra.emots) {
33
+ let emotItem = extra.emots[key];
34
+ emoticon[key] = {
35
+ id: emotItem.emoticon_unique,
36
+ url: emotItem.url,
37
+ name: emotItem.emoji,
38
+ };
39
+ }
40
+ }
41
+ if (info[2][2]) {
42
+ identity = UserType.admin;
43
+ }
44
+ else if (uid === room?.anchor.id) {
45
+ identity = UserType.anchor;
46
+ }
47
+ let danmaku = {
48
+ platform: "bilibili",
49
+ roomId: room?.id || 0,
50
+ userId: uid,
51
+ type: "comment",
52
+ timestamp: timestamp,
53
+ id: msg_id,
54
+ info: {
55
+ color: color,
56
+ mode: mode,
57
+ content: content,
58
+ image: info[0][12]
59
+ ? {
60
+ name: content,
61
+ id: info[0][13].emoticon_unique,
62
+ url: info[0][13].url,
63
+ size: info[0][13].width == info[0][13].height
64
+ ? ImageSize.large
65
+ : ImageSize.small,
66
+ }
67
+ : undefined,
68
+ emoticon: emoticon,
69
+ user: {
70
+ name: uname,
71
+ id: uid,
72
+ medal: medal,
73
+ membership: guard_level,
74
+ type: identity,
75
+ },
76
+ },
77
+ };
78
+ danmaku.id ?? (danmaku.id = generateId(danmaku));
79
+ return danmaku;
80
+ },
81
+ INTERACT_WORD: (msg, room) => {
82
+ const { data, msg_id, send_time } = msg;
83
+ let type;
84
+ switch (data.msg_type) {
85
+ case 1:
86
+ type = "entry"; // 进入直播间
87
+ break;
88
+ case 2:
89
+ type = "follow"; // 关注直播间
90
+ break;
91
+ case 3:
92
+ type = "share"; // 分享直播间
93
+ break;
94
+ default:
95
+ return;
96
+ }
97
+ let interact = {
98
+ platform: "bilibili",
99
+ roomId: room?.id || 0,
100
+ userId: data.uid,
101
+ type: type,
102
+ id: msg_id,
103
+ timestamp: send_time || Math.floor(data.trigger_time / 1000000),
104
+ info: {
105
+ user: {
106
+ name: data.uname,
107
+ id: data.uid,
108
+ medal: data.fans_medal?.medal_level
109
+ ? {
110
+ level: data.fans_medal.medal_level,
111
+ name: data.fans_medal.medal_name,
112
+ id: data.fans_medal.target_id,
113
+ membership: data.fans_medal.guard_level,
114
+ }
115
+ : null,
116
+ },
117
+ },
118
+ };
119
+ interact.id ?? (interact.id = generateId(interact));
120
+ return interact;
121
+ },
122
+ SEND_GIFT: (msg, room) => {
123
+ const { data, msg_id, send_time } = msg;
124
+ let gift = {
125
+ platform: "bilibili",
126
+ roomId: room?.id || 0,
127
+ userId: data.uid,
128
+ type: "gift",
129
+ id: msg_id || data.combo_id,
130
+ timestamp: send_time || data.timestamp * 1000,
131
+ info: {
132
+ user: {
133
+ name: data.uname,
134
+ id: data.uid,
135
+ medal: data.fans_medal && data.fans_medal.medal_level
136
+ ? {
137
+ level: data.fans_medal.medal_level,
138
+ name: data.fans_medal.medal_name,
139
+ id: data.fans_medal.target_id,
140
+ membership: data.fans_medal.guard_level,
141
+ }
142
+ : null,
143
+ membership: data.guard_level,
144
+ avatar: data.face,
145
+ },
146
+ gift: {
147
+ name: data.giftName,
148
+ id: data.giftId,
149
+ num: data.num,
150
+ value: data.total_coin,
151
+ currency: data.coin_type,
152
+ action: data.action,
153
+ comboId: data.batch_combo_id,
154
+ },
155
+ },
156
+ };
157
+ gift.id ?? (gift.id = generateId(gift));
158
+ return gift;
159
+ },
160
+ GUARD_BUY: (msg, room) => {
161
+ const { data, msg_id, send_time } = msg;
162
+ let gift = {
163
+ platform: "bilibili",
164
+ roomId: room?.id || 0,
165
+ userId: data.uid,
166
+ type: "membership",
167
+ id: msg_id,
168
+ timestamp: send_time || data.start_time * 1000,
169
+ info: {
170
+ user: {
171
+ name: data.username,
172
+ id: data.uid,
173
+ membership: data.guard_level,
174
+ },
175
+ gift: {
176
+ name: data.gift_name,
177
+ id: data.gift_id,
178
+ num: data.num,
179
+ value: data.price,
180
+ currency: "gold",
181
+ },
182
+ name: data.gift_name,
183
+ level: data.guard_level,
184
+ duration: data.num * 30,
185
+ },
186
+ };
187
+ gift.id ?? (gift.id = generateId(gift));
188
+ return gift;
189
+ },
190
+ SUPER_CHAT_MESSAGE: (msg, room) => {
191
+ const { data, msg_id, send_time } = msg;
192
+ let sc = {
193
+ platform: "bilibili",
194
+ roomId: room?.id || 0,
195
+ userId: data.uid,
196
+ type: "superchat",
197
+ id: msg_id,
198
+ timestamp: send_time || data.ts * 1000,
199
+ info: {
200
+ id: data.id,
201
+ user: {
202
+ name: data.user_info.uname,
203
+ id: data.uid,
204
+ avatar: data.user_info.face,
205
+ medal: data.medal_info && data.medal_info.medal_level
206
+ ? {
207
+ level: data.medal_info.medal_level,
208
+ name: data.medal_info.medal_name,
209
+ id: data.medal_info.target_id,
210
+ membership: data.medal_info.guard_level,
211
+ }
212
+ : null,
213
+ membership: data.user_info.guard_level,
214
+ },
215
+ content: data.message, // 文字
216
+ color: data.background_bottom_color, // 颜色
217
+ duration: data.time * 1000, // 持续时间(ms)
218
+ gift: {
219
+ id: data.gift.gift_id,
220
+ name: data.gift.gift_name,
221
+ num: data.gift.num,
222
+ value: data.price * 1000, // 花费
223
+ currency: "gold",
224
+ },
225
+ },
226
+ };
227
+ sc.id ?? (sc.id = generateId(sc));
228
+ return sc;
229
+ },
230
+ WATCHED_CHANGE: (msg, room) => {
231
+ const { data, msg_id, send_time } = msg;
232
+ const stats = {
233
+ platform: "bilibili",
234
+ roomId: room?.id || 0,
235
+ userId: 0,
236
+ type: "live_stats",
237
+ id: msg_id,
238
+ timestamp: send_time || getDateTimestamp(),
239
+ info: {
240
+ view: data.num,
241
+ },
242
+ };
243
+ stats.id ?? (stats.id = generateId(stats));
244
+ return stats;
245
+ },
246
+ LIKE_INFO_V3_UPDATE: (msg, room) => {
247
+ const { data, msg_id, send_time } = msg;
248
+ const stats = {
249
+ platform: "bilibili",
250
+ roomId: room?.id || 0,
251
+ userId: 0,
252
+ type: "live_stats",
253
+ id: msg_id,
254
+ timestamp: send_time || getDateTimestamp(),
255
+ info: {
256
+ like: data.click_count,
257
+ },
258
+ };
259
+ stats.id ?? (stats.id = generateId(stats));
260
+ return stats;
261
+ },
262
+ ONLINE_RANK_COUNT: (msg, room) => {
263
+ const { data, msg_id, send_time } = msg;
264
+ if (data.online_count == null)
265
+ return;
266
+ const stats = {
267
+ platform: "bilibili",
268
+ roomId: room?.id || 0,
269
+ userId: 0,
270
+ type: "live_stats",
271
+ id: msg_id,
272
+ timestamp: send_time || getDateTimestamp(),
273
+ info: {
274
+ online: data.online_count,
275
+ },
276
+ };
277
+ stats.id ?? (stats.id = generateId(stats));
278
+ return stats;
279
+ },
280
+ ROOM_BLOCK_MSG: (msg, room) => {
281
+ const { data, msg_id, send_time } = msg;
282
+ const block = {
283
+ platform: "bilibili",
284
+ roomId: room?.id || 0,
285
+ userId: data.uid,
286
+ type: "block",
287
+ id: msg_id,
288
+ timestamp: send_time || getDateTimestamp(),
289
+ info: {
290
+ user: {
291
+ id: data.uid,
292
+ name: data.uname,
293
+ },
294
+ operator: {
295
+ type: data.operator == 2 ? UserType.anchor : UserType.admin,
296
+ },
297
+ },
298
+ };
299
+ block.id ?? (block.id = generateId(block));
300
+ return block;
301
+ },
302
+ LIVE: (msg, room) => {
303
+ const { msg_id, send_time } = msg;
304
+ // 直播间开播
305
+ if (msg.live_time) {
306
+ let live = {
307
+ platform: "bilibili",
308
+ roomId: room?.id || 0,
309
+ userId: 0,
310
+ type: "live_start",
311
+ id: msg_id,
312
+ timestamp: send_time || msg.live_time * 1000,
313
+ info: {
314
+ id: msg.live_key.toString(),
315
+ },
316
+ };
317
+ live.id ?? (live.id = generateId(live));
318
+ return live;
319
+ }
320
+ },
321
+ CUT_OFF: (msg, room) => {
322
+ const { msg_id, send_time } = msg;
323
+ // 直播间被切断
324
+ let cut = {
325
+ platform: "bilibili",
326
+ roomId: room?.id || 0,
327
+ userId: 0,
328
+ type: "live_cut",
329
+ id: msg_id,
330
+ timestamp: send_time || getDateTimestamp(),
331
+ info: {
332
+ message: msg.msg,
333
+ },
334
+ };
335
+ cut.id ?? (cut.id = generateId(cut));
336
+ return cut;
337
+ },
338
+ PREPARING: (msg, room) => {
339
+ const { msg_id, send_time } = msg;
340
+ let off = {
341
+ platform: "bilibili",
342
+ roomId: room?.id || 0,
343
+ userId: 0,
344
+ type: "live_end",
345
+ id: msg_id,
346
+ timestamp: send_time || getDateTimestamp(),
347
+ info: {
348
+ status: msg.round ? LiveRoomStatus.round : LiveRoomStatus.off,
349
+ },
350
+ };
351
+ off.id ?? (off.id = generateId(off));
352
+ return off;
353
+ },
354
+ ROOM_CHANGE: (msg, room) => {
355
+ const { data, msg_id, send_time } = msg;
356
+ let change = {
357
+ platform: "bilibili",
358
+ roomId: room?.id || 0,
359
+ userId: 0,
360
+ type: "live_detail",
361
+ id: msg_id,
362
+ timestamp: send_time || getDateTimestamp(),
363
+ info: {
364
+ title: data.title,
365
+ area: [data.parent_area_name, data.area_name],
366
+ },
367
+ };
368
+ change.id ?? (change.id = generateId(change));
369
+ return change;
370
+ },
371
+ ANCHOR_LOT_START: (msg, room) => {
372
+ const { data, msg_id, send_time } = msg;
373
+ let m = {
374
+ platform: "bilibili",
375
+ roomId: room?.id || 0,
376
+ userId: 0,
377
+ type: "lottery",
378
+ timestamp: send_time || data.current_time * 1000,
379
+ id: msg_id,
380
+ info: {
381
+ id: data.id,
382
+ award: {
383
+ name: data.award_name,
384
+ num: data.award_num,
385
+ image: data.award_image,
386
+ id: -1,
387
+ value: 0,
388
+ currency: "",
389
+ },
390
+ },
391
+ };
392
+ return m;
393
+ },
394
+ };
395
+ /** 转换bilibili直播弹幕为floating通用格式 */
396
+ export function parseMessage(data, room) {
397
+ return parsingFunction[data.cmd]?.(data, room);
398
+ }
399
+ /** 转换bilibili直播间信息为floating通用格式 */
400
+ export function parseGetInfoByRoom(data) {
401
+ const { room_info, anchor_info, like_info_v3, watched_show, } = data;
402
+ return {
403
+ platform: "bilibili",
404
+ id: room_info.room_id,
405
+ roomId: room_info.room_id,
406
+ shortId: room_info.short_id,
407
+ key: `bilibili:${room_info.room_id}`,
408
+ detail: {
409
+ title: room_info.title,
410
+ area: [room_info.parent_area_name, room_info.area_name],
411
+ cover: room_info.cover,
412
+ },
413
+ anchor: {
414
+ id: room_info.uid,
415
+ name: anchor_info.base_info.uname,
416
+ avatar: anchor_info.base_info.face,
417
+ },
418
+ stats: {
419
+ view: watched_show.num,
420
+ like: like_info_v3.total_likes,
421
+ },
422
+ liveId: room_info.live_id_str,
423
+ available: room_info.lock_status ? false : true,
424
+ status: room_info.lock_status
425
+ ? LiveRoomStatus.banned
426
+ : room_info.live_status,
427
+ timestamp: room_info.live_status == LiveRoomStatus.live
428
+ ? room_info.live_start_time * 1000
429
+ : 0,
430
+ openStatus: 0,
431
+ opened: false,
432
+ connectionStatus: 0,
433
+ };
434
+ }
435
+ /** 转换bilibili直播间信息为floating通用格式 */
436
+ export function parseRoomBaseInfo(data) {
437
+ return {
438
+ platform: "bilibili",
439
+ id: data.room_id,
440
+ roomId: data.room_id,
441
+ shortId: data.short_id,
442
+ key: `bilibili:${data.room_id}`,
443
+ detail: {
444
+ title: data.title,
445
+ area: [data.parent_area_name, data.area_name],
446
+ cover: data.cover,
447
+ },
448
+ anchor: {
449
+ id: data.uid,
450
+ name: data.uname,
451
+ },
452
+ stats: {},
453
+ liveId: data.live_id_str,
454
+ available: data.lock_status ? false : true,
455
+ status: data.lock_status
456
+ ? LiveRoomStatus.banned
457
+ : data.live_status,
458
+ timestamp: data.live_status == LiveRoomStatus.live
459
+ ? new Date(`${data.live_time} GMT+0800`).getTime() * 1000
460
+ : 0,
461
+ openStatus: 0,
462
+ opened: false,
463
+ connectionStatus: 0,
464
+ };
465
+ }
466
+ export function mergeLiveRoomData(...list) {
467
+ const room = Object.assign({}, ...list);
468
+ const detail = Object.assign({}, ...list.map((d) => d.detail));
469
+ const anchor = Object.assign({}, ...list.map((d) => d.anchor));
470
+ const stats = Object.assign({}, ...list.map((d) => d.stats));
471
+ return { ...room, detail, anchor, stats };
472
+ }