From ffb6ff35e00bd610a06ebd8715aa54cbf74eacc5 Mon Sep 17 00:00:00 2001 From: ikaros <327209194@qq.com> Date: Sun, 14 Apr 2024 20:19:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8B=BC=E5=A4=9A=E5=A4=9A?= =?UTF-8?q?=20=E5=BC=B9=E5=B9=95=E7=9B=91=E5=90=AC=EF=BC=8C=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=80=BB=E8=BE=91=E5=90=8C=E5=BF=AB=E6=89=8B2?= =?UTF-8?q?=E4=B8=80=E6=A0=B7=EF=BC=8C=E9=87=87=E7=94=A8=E6=B2=B9=E7=8C=B4?= =?UTF-8?q?=E5=89=8D=E7=AB=AF=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...WS\346\234\215\345\212\241\347\253\257.js" | 55 ++++++++++++++++++- main.py | 49 +++++++++++++++++ webui.py | 1 + 3 files changed, 104 insertions(+), 1 deletion(-) diff --git "a/Scripts/\347\233\264\346\222\255ws\350\204\232\346\234\254/\347\233\264\346\222\255\345\274\271\345\271\225\347\233\221\345\220\254 \350\275\254\345\217\221\350\207\263\346\234\254\345\234\260WS\346\234\215\345\212\241\347\253\257.js" "b/Scripts/\347\233\264\346\222\255ws\350\204\232\346\234\254/\347\233\264\346\222\255\345\274\271\345\271\225\347\233\221\345\220\254 \350\275\254\345\217\221\350\207\263\346\234\254\345\234\260WS\346\234\215\345\212\241\347\253\257.js" index cbb11ac7..41c6df80 100644 --- "a/Scripts/\347\233\264\346\222\255ws\350\204\232\346\234\254/\347\233\264\346\222\255\345\274\271\345\271\225\347\233\221\345\220\254 \350\275\254\345\217\221\350\207\263\346\234\254\345\234\260WS\346\234\215\345\212\241\347\253\257.js" +++ "b/Scripts/\347\233\264\346\222\255ws\350\204\232\346\234\254/\347\233\264\346\222\255\345\274\271\345\271\225\347\233\221\345\220\254 \350\275\254\345\217\221\350\207\263\346\234\254\345\234\260WS\346\234\215\345\212\241\347\253\257.js" @@ -1,12 +1,14 @@ // ==UserScript== // @name 直播弹幕监听 转发至本地WS服务端 // @namespace http://tampermonkey.net/ -// @version 0.6 +// @version 0.7 // @description 观察指定 DOM 节点的变化以将数据发送到连接的WebSocket服务端 // @description Github:https://github.com/Ikaros-521/AI-Vtuber/tree/main/Scripts/%E7%9B%B4%E6%92%ADws%E8%84%9A%E6%9C%AC // @author Ikaros // @match https://www.douyu.com/* // @match https://live.kuaishou.com/u/* +// @match https://live.kuaishou.com/u/* +// @match https://mobile.yangkeduo.com/* // @grant none // @namespace https://greasyfork.org/scripts/490966 // @license GPL-3.0 @@ -25,8 +27,13 @@ const hostname = window.location.hostname; if (hostname === "www.douyu.com") { + console.log("当前直播平台:斗鱼"); wsUrl = "ws://127.0.0.1:5000"; } else if (hostname === "live.kuaishou.com") { + console.log("当前直播平台:快手"); + wsUrl = "ws://127.0.0.1:5000"; + } else if (hostname === "mobile.yangkeduo.com") { + console.log("当前直播平台:拼多多"); wsUrl = "ws://127.0.0.1:5000"; } @@ -181,6 +188,52 @@ console.log(username + ":" + content); + // 获取到弹幕数据 + if (username !== "" && content !== "") { + const data = { + type: "comment", + username: username, + content: content, + }; + console.log(data); + // 如果 socket 已经初始化,可以在这里发送数据 + if (socket) { + socket.send(JSON.stringify(data)); + } + } + } + } + }); + } + }); + }); + } else if (hostname === "mobile.yangkeduo.com") { + // 选择需要观察变化的节点 + targetNode = document.querySelector(".MYFlHgGu"); + + // 创建观察器实例 + observer = new MutationObserver((mutations) => { + mutations.forEach((mutation) => { + // 这里处理新增的DOM元素 + if (mutation.type === "childList") { + mutation.addedNodes.forEach((node) => { + // 判断是否是新增的弹幕消息 + if (node.classList.contains("_24Qh0Jmi")) { + // 新增的动态DOM元素处理 + console.log("Added node:", node); + + const usernameElement = node.querySelector(".t6fCgSnz"); + const commentElement = node.querySelector("._16_fPXYP"); + + if ( + usernameElement && + commentElement + ) { + const username = usernameElement.textContent.trim().slice(0, -1); + const content = commentElement.textContent.trim(); + + console.log(username + ":" + content); + // 获取到弹幕数据 if (username !== "" && content !== "") { const data = { diff --git a/main.py b/main.py index db0e00a8..96b3e52e 100644 --- a/main.py +++ b/main.py @@ -2123,6 +2123,55 @@ def run_live(self): i.join() run().run_live() + elif platform == "pdd": + import websockets + + async def on_message(websocket, path): + global last_liveroom_data, last_username_list + global global_idle_time + + async for message in websocket: + # print(f"收到消息: {message}") + # await websocket.send("服务器收到了你的消息: " + message) + + try: + data_json = json.loads(message) + # logging.debug(data_json) + if data_json["type"] == "comment": + # logging.info(data_json) + # 闲时计数清零 + idle_time_auto_clear("comment") + + username = data_json["username"] + content = data_json["content"] + + logging.info(f'[📧直播间弹幕消息] [{username}]:{content}') + + data = { + "platform": platform, + "username": username, + "content": content + } + + my_handle.process_data(data, "comment") + + # 添加用户名到最新的用户名列表 + add_username_to_last_username_list(username) + + except Exception as e: + logging.error(traceback.format_exc()) + logging.error("数据解析错误!") + my_handle.abnormal_alarm_handle("platform") + continue + + async def ws_server(): + ws_url = "127.0.0.1" + ws_port = 5000 + server = await websockets.serve(on_message, ws_url, ws_port) + logging.info(f"WebSocket 服务器已在 {ws_url}:{ws_port} 启动") + await server.wait_closed() + + asyncio.run(ws_server()) elif platform == "tiktok": """ tiktok diff --git a/webui.py b/webui.py index e38a5d6d..c175a1d4 100644 --- a/webui.py +++ b/webui.py @@ -2471,6 +2471,7 @@ def common_textarea_handle(content): 'dy': '抖音', 'ks': '快手', 'ks2': '快手2', + 'pdd': '拼多多', 'wxlive': '微信视频号', 'douyu': '斗鱼', 'youtube': 'YouTube',