Skip to content

Commit

Permalink
feat: add speaker override for danmu_spread.SpreadFilter
Browse files Browse the repository at this point in the history
feat: add spreaker not match verbose log for danmu_spread.logViewer
  • Loading branch information
Greesea committed Feb 15, 2024
1 parent 2c89d45 commit 98aa7c6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 30 deletions.
9 changes: 5 additions & 4 deletions const/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ class InternalMessage(Enum):
WEBSOCKET_LISTEN_ON_ERROR = "102" # WebSocket - 连接异常

class SpreadEventTypes(Enum):
START = "0" # 开始转发
STOP = "1" # 停止转发
RECEIVE_TRANSLATED = "2" # 捕获到同传弹幕
SENT = "3" # 已转发处理后的同传弹幕
START = "0" # 开始转发
STOP = "1" # 停止转发
RECEIVE_VALID_TRANSLATED = "2" # 捕获到有效的同传弹幕
RECEIVE_INVALID_TRANSLATED = "3" # 捕获到无效(被过滤)的同传弹幕
SENT = "4" # 已转发处理后的同传弹幕
23 changes: 20 additions & 3 deletions frame/danmu_spread.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,12 @@ def SelectRoom(self,slot,index,roomid):
if index>0 and old_rid!=roomid:
self.configs[slot][2][index-1]=""
self.configs[slot][3][index-1]=0
self.configs[slot][4][index-1]=False
else:
self.configs[slot][0].append(roomid)
self.configs[slot][2].append("")
self.configs[slot][3].append(0)
self.configs[slot][4].append(False)
if index>0 and self.configs[slot][1]:
if roomid not in self.websockets.keys():
self.websockets[roomid]=BiliLiveWebSocket(roomid)
Expand Down Expand Up @@ -332,7 +334,7 @@ def OnMessageSpreadEvent(self, eventType: SpreadEventTypes, eventData):
}),
"tan",
)
elif eventType == SpreadEventTypes.RECEIVE_TRANSLATED:
elif eventType == SpreadEventTypes.RECEIVE_VALID_TRANSLATED:
if self.logViewerVerboseMode:
self.AppendLogToLogViewer(
"{internalTimeText} | #{slot}.{fromRoomFull} | 捕获到:{rawContent}".format(**{
Expand All @@ -343,6 +345,17 @@ def OnMessageSpreadEvent(self, eventType: SpreadEventTypes, eventData):
}),
"gray",
)
elif eventType == SpreadEventTypes.RECEIVE_INVALID_TRANSLATED:
if self.logViewerVerboseMode:
self.AppendLogToLogViewer(
"{internalTimeText} | #{slot}.{fromRoomFull} | 捕获到前缀不符:{rawContent}".format(**{
"internalTimeText": internalTimeText,
"slot": slot,
"fromRoomFull": internalData["fromRoom"]["full"],
"rawContent": internalData["rawContent"],
}),
"gray",
)
elif eventType == SpreadEventTypes.SENT:
style = ERR_INFO[eventData["result"]]
self.AppendLogToLogViewer(
Expand Down Expand Up @@ -390,7 +403,7 @@ def __init__(self, parent, slot, index):
self.index=index
pos=parent.GetPosition()
x,y=pos[0]+50,pos[1]+60
wx.Frame.__init__(self, parent, title="房间转发设置", pos=(x,y), size=(300, 170),
wx.Frame.__init__(self, parent, title="房间转发设置", pos=(x,y), size=(300, 220),
style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX | wx.MINIMIZE_BOX) |wx.FRAME_FLOAT_ON_PARENT)
if parent.show_pin:
self.ToggleWindowStyle(wx.STAY_ON_TOP)
Expand All @@ -406,7 +419,10 @@ def __init__(self, parent, slot, index):
speakers=self.configs[slot][2][index]
self.tcFilter=wx.TextCtrl(panel,-1,speakers,pos=(68,38),size=(210,27),style=wx.TE_PROCESS_ENTER)
self.tcFilter.Bind(wx.EVT_TEXT_ENTER,self.Save)
self.btnSave=wx.Button(panel,-1,"保 存",pos=(105,105),size=(80,32))
wx.StaticText(panel,-1,"覆盖前缀",pos=(10,110))
self.ckbOverride=wx.CheckBox(panel, -1, "使用主播简称覆盖原本前缀",pos=(68,110))
self.ckbOverride.SetValue(self.configs[slot][4][index])
self.btnSave=wx.Button(panel,-1,"保 存",pos=(105,145),size=(80,32))
self.btnSave.Bind(wx.EVT_BUTTON,self.Save)
self.Show()

Expand All @@ -420,4 +436,5 @@ def Save(self,event):
speakers="" if speakers==";" else speakers
self.configs[self.slot][2][self.index]=speakers
self.configs[self.slot][3][self.index]=self.sldDelay.GetValue()*100
self.configs[self.slot][4][self.index]=self.ckbOverride.GetValue()
self.Parent.RefreshUI() #该方法包含销毁本窗体的语句
59 changes: 36 additions & 23 deletions frame/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ def OnMessageCoreConfigUpdated(self, before, after):
self.danmuSpreadFrame.StopAll()
self.danmuSpreadFrame.Close()
self.danmuSpreadFrame.Destroy()
self.sp_configs = [[[None],False,[],[]] for _ in range(self.spread_maximum_spread_rooms)] # 同传转发配置列表 每项为[房间号列表,转发开关,限定前缀列表,转发延时列表]
self.sp_configs = [[[None],False,[],[],[]] for _ in range(self.spread_maximum_spread_rooms)] # 同传转发配置列表 每项为[房间号列表,转发开关,限定前缀列表,转发延时列表,覆盖前缀开关列表]
self.danmuSpreadFrame = DanmuSpreadFrame(self)


Expand Down Expand Up @@ -1274,28 +1274,24 @@ def SpreadDanmu(self,roomid,speaker,content,rawContent):
if not self.GetCurrentDanmuConfig(roomid):
self.sp_max_len=20
for slot, cfg in enumerate(self.sp_configs):
to_room,from_rooms,spreading,speaker_filters,delays=cfg[0][0],cfg[0][1:],cfg[1],cfg[2],cfg[3]
to_room,from_rooms,spreading,speaker_filters,delays,override_toggles=cfg[0][0],cfg[0][1:],cfg[1],cfg[2],cfg[3],cfg[4]
if not spreading or to_room is None or roomid not in from_rooms: continue
speaker=self.sp_rooms[roomid][1] if not speaker else speaker
sp_delay_ms=0
# 如果前缀过滤条件不为空,则只转发指定的前缀
speaker_be_filtered=False
for from_roomid,allowed_speakers,delay in zip(from_rooms,speaker_filters,delays):
if roomid==from_roomid:
sp_delay_ms=delay
else:
continue
if allowed_speakers=="":

spreadConfigs = {
"delay": 0, # 转发延迟
"speakerNotMatch": False, # 限定前缀是否不匹配
"speakerOverride": False, # 是否覆盖前缀
}

for from_roomid,allowed_speakers,delay,override in zip(from_rooms,speaker_filters,delays,override_toggles):
if roomid != from_roomid: # room not match
continue
if speaker not in allowed_speakers.split(";"):
speaker_be_filtered=True
if allowed_speakers != "" and speaker not in allowed_speakers.split(";"): # 如果前缀过滤条件不为空,则只转发指定的前缀
spreadConfigs["speakerNotMatch"] = True
break
if speaker_be_filtered:
continue
# 弹幕开头添加标识符U+0592避免循环转发(本工具不会转发以U+0592开头的同传弹幕)
pre="\u0592"+speaker+"【"
msg=self.AntiShield(pre+content,to_room)
suf="】" if msg.count("【")>msg.count("】") else ""
spreadConfigs["delay"] = delay
spreadConfigs["speakerOverride"] = override

# 准备数据
internalData = {
Expand All @@ -1312,16 +1308,33 @@ def SpreadDanmu(self,roomid,speaker,content,rawContent):
"short": self.sp_rooms[to_room][1],
},
"content": content,
"sendContent": msg,
"rawContent": rawContent,
}
pub.sendMessage(InternalMessage.SPREAD_EVENT.value, eventType = SpreadEventTypes.RECEIVE_TRANSLATED, eventData = {

if spreadConfigs["speakerNotMatch"]:
pub.sendMessage(InternalMessage.SPREAD_EVENT.value, eventType = SpreadEventTypes.RECEIVE_INVALID_TRANSLATED, eventData = {
"internalTime": int(time.time()),
"internalData": internalData,
})
continue

# 构成实际发送弹幕
# 弹幕开头添加标识符U+0592避免循环转发(本工具不会转发以U+0592开头的同传弹幕)
actualSpeaker = internalData["fromRoom"]["short"] if spreadConfigs["speakerOverride"] else speaker
pre="\u0592"+actualSpeaker+"【"
msg=self.AntiShield(pre+content,to_room)
suf="】" if msg.count("【")>msg.count("】") else ""

pub.sendMessage(InternalMessage.SPREAD_EVENT.value, eventType = SpreadEventTypes.RECEIVE_VALID_TRANSLATED, eventData = {
"internalTime": int(time.time()),
"internalData": internalData,
"internalData": {
**internalData,
"sendContent": msg,
},
})

# 延迟指定毫秒数后,将弹幕添加到队列
self.pool_dm.submit(self.ThreadOfDelayDanmuQueue,sp_delay_ms,to_room,msg,DanmuSrc.SPREAD,pre,suf,self.sp_max_len,internalData)
self.pool_dm.submit(self.ThreadOfDelayDanmuQueue,spreadConfigs["delay"],to_room,msg,DanmuSrc.SPREAD,pre,suf,self.sp_max_len,internalData)

def StartListening(self,roomid):
"""建立与直播间之间的Websocket连接"""
Expand Down

0 comments on commit 98aa7c6

Please sign in to comment.