Skip to content

Commit 5ae5f53

Browse files
committed
feat: 解析弹幕消息内的小表情
resolve #20
1 parent 15eb3c5 commit 5ae5f53

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,22 @@ export interface DanmuMsg {
383383
timestamp: number
384384
/** 是否为天选抽奖弹幕 */
385385
lottery: boolean
386-
/** 弹幕表情 */
386+
/** 表情弹幕内容 */
387387
emoticon?: {
388388
id: string
389389
height: number
390390
width: number
391391
url: string
392392
}
393+
/** 弹幕内小表情映射,key为表情文字,如"[妙]" */
394+
in_message_emoticon?: Record<string, {
395+
id: string
396+
emoticon_id: number
397+
height: number
398+
width: number
399+
url: string
400+
description: string
401+
}>
393402
}
394403
```
395404

src/parser/DANMU_MSG.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,56 @@ export interface DanmuMsg {
99
timestamp: number
1010
/** 是否为天选抽奖弹幕 */
1111
lottery: boolean
12-
/** 弹幕表情 */
12+
/** 表情弹幕内容 */
1313
emoticon?: {
1414
id: string
1515
height: number
1616
width: number
1717
url: string
1818
}
19+
/** 弹幕内小表情映射,key为表情文字,如"[妙]" */
20+
in_message_emoticon?: Record<string, {
21+
id: string
22+
emoticon_id: number
23+
height: number
24+
width: number
25+
url: string
26+
description: string
27+
}>
1928
}
2029

2130
const parser = (data: any, roomId: number): DanmuMsg => {
2231
const rawData = data.info
32+
const content = rawData[1]
33+
// 是否包含中括号
34+
const parseInMessageEmoticon = /\[.*?\]/.test(content)
35+
console.log('parseInMessageEmoticon', parseInMessageEmoticon)
36+
let inMessageEmoticon
37+
if (parseInMessageEmoticon) {
38+
const messageExtraInfo = JSON.parse(rawData[0][15].extra)
39+
const emoctionDict: Record<string, {
40+
id: string
41+
emoticon_id: number
42+
height: number
43+
width: number
44+
url: string
45+
description: string
46+
}> = {}
47+
if (messageExtraInfo.emots) {
48+
inMessageEmoticon = Object.keys(messageExtraInfo.emots).reduce((acc, key) => {
49+
const emoticon = messageExtraInfo.emots[key]
50+
acc[key] = {
51+
id: emoticon.emoticon_unique,
52+
emoticon_id: emoticon.emoticon_id,
53+
height: emoticon.height,
54+
width: emoticon.width,
55+
url: emoticon.url,
56+
description: emoticon.descript,
57+
}
58+
return acc
59+
}, emoctionDict)
60+
}
61+
}
2362
return {
2463
user: {
2564
uid: rawData[2][0],
@@ -47,7 +86,7 @@ const parser = (data: any, roomId: number): DanmuMsg => {
4786
room_admin: rawData[2][2] === 1,
4887
},
4988
},
50-
content: rawData[1],
89+
content,
5190
timestamp: rawData[0][4],
5291
lottery: rawData[0][9] !== 0,
5392
emoticon: rawData[0][13]?.emoticon_unique ? {
@@ -56,6 +95,7 @@ const parser = (data: any, roomId: number): DanmuMsg => {
5695
width: rawData[0][13].width,
5796
url: rawData[0][13].url,
5897
} : undefined,
98+
in_message_emoticon: inMessageEmoticon,
5999
}
60100
}
61101

0 commit comments

Comments
 (0)