diff --git a/app.pyw b/app.pyw index 3f36247..34646c4 100644 --- a/app.pyw +++ b/app.pyw @@ -1,9 +1,11 @@ import wx +import threading from frame.main import MainFrame - +from utils import local_api if __name__ == '__main__': app = wx.App() frame = MainFrame() + threading.Thread(target=local_api.app.run, kwargs={"host": '127.0.0.1', "port": 8745}, daemon=True).start() app.MainLoop() diff --git a/frame/main.py b/frame/main.py index fa834c2..f824a58 100644 --- a/frame/main.py +++ b/frame/main.py @@ -31,7 +31,7 @@ class MainFrame(wx.Frame): - LD_VERSION = "v1.5.3" + LD_VERSION = "v1.5.3 mod" def __init__(self, parent=None): """B站直播同传/歌词弹幕发送工具""" @@ -56,6 +56,7 @@ def __init__(self, parent=None): pub.subscribe(self.SpreadDanmu,"ws_recv") # 消息:监听到同传弹幕 pub.subscribe(self.StartListening,"ws_start") # 消息:开始监听房间内的弹幕 pub.subscribe(self.SetSpreadButtonState,"ws_error") # 消息:监听过程中出现错误/恢复 + pub.subscribe(self.HandleApi, 'api_control') # API self.blApi = BiliLiveAPI(self.cookies,(self.timeout_s,5)) # B站账号与直播相关接口 self.wyApi = NetEaseMusicAPI() # 网易云音乐接口 @@ -101,6 +102,7 @@ def __init__(self, parent=None): # 其他参数 self.tmp_clipboard="" # 临时剪贴板内容 self.recent_danmu = {"_%d_"%i:0 for i in range(5)} # 近期发送的弹幕字典({弹幕内容:内容重复次数}) + self.last_set_comment = None self.danmu_queue = [] # 待发送弹幕队列 self.recent_history = [] # 评论框历史记录列表 self.tmp_history = [] # 评论框历史记录列表(临时) @@ -1244,6 +1246,13 @@ def SendDanmu(self, roomid, msg, src:DanmuSrc, pre, max_len, try_times=2): self.SendDanmu(roomid,pre+remain_msg,src,pre,max_len) return succ_send + def HandleApi(self, command, payload): + if command == 'set_comment': + if payload['text'] != self.last_set_comment: + self.last_set_comment = payload['text'] + text = re.sub(r'\s+', ' ', payload['text']) + self.tcComment.SetValue(text) + def SpreadDanmu(self,roomid,speaker,content): """转发同传弹幕""" if self.sp_max_len is None: diff --git a/requirements.txt b/requirements.txt index 6776fa3..d97644f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ qrcode==7.3.1 requests==2.25.1 wxPython==4.1.1 websockets==10.2 +flask diff --git a/utils/local_api.py b/utils/local_api.py new file mode 100644 index 0000000..a0b72da --- /dev/null +++ b/utils/local_api.py @@ -0,0 +1,13 @@ +from flask import Flask, Response, request +from pubsub import pub + +app = Flask(__name__) + +@app.route('/') +def index(): + return 'test' + +@app.route('/control', methods=['POST']) +def control(): + pub.sendMessage('api_control', command=request.json['command'], payload=request.json['payload']) + return 'recv'