Skip to content

Commit

Permalink
支持拼多多 弹幕监听,实现逻辑同快手2一样,采用油猴前端监听
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikaros-521 committed Apr 14, 2024
1 parent 6be88e4 commit ffb6ff3
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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";
}

Expand Down Expand Up @@ -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 = {
Expand Down
49 changes: 49 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2471,6 +2471,7 @@ def common_textarea_handle(content):
'dy': '抖音',
'ks': '快手',
'ks2': '快手2',
'pdd': '拼多多',
'wxlive': '微信视频号',
'douyu': '斗鱼',
'youtube': 'YouTube',
Expand Down

0 comments on commit ffb6ff3

Please sign in to comment.