Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ddafe21708 | ||
|
|
bceebf8fc5 | ||
|
|
f8aa4b141d | ||
|
|
1cbbff42f2 | ||
|
|
2c054f3c59 |
30
liveMan.py
30
liveMan.py
@@ -38,7 +38,7 @@ async def send_message(message):
|
||||
await websocket.send(message) # 发送消息到 WebSocket 服务器
|
||||
print(f"已发送消息: {message}")
|
||||
|
||||
asyncio.run(send_message(f"【进场msg】111 进入了直播间"))
|
||||
# asyncio.run(send_message(f"【进场msg】111 进入了直播间"))
|
||||
|
||||
|
||||
@contextmanager
|
||||
@@ -313,6 +313,8 @@ class DouyinLiveWebFetcher:
|
||||
user_id = message.user.id
|
||||
content = message.content
|
||||
print(f"【聊天msg】[{user_id}]{user_name}: {content}")
|
||||
asyncio.run(send_message(f"【聊天msg】[{user_id}]{user_name}: {content}"))
|
||||
|
||||
|
||||
def _parseGiftMsg(self, payload):
|
||||
"""礼物消息"""
|
||||
@@ -321,6 +323,7 @@ class DouyinLiveWebFetcher:
|
||||
gift_name = message.gift.name
|
||||
gift_cnt = message.combo_count
|
||||
print(f"【礼物msg】{user_name} 送出了 {gift_name}x{gift_cnt}")
|
||||
asyncio.run(send_message(f"【礼物msg】{user_name} 送出了 {gift_name}x{gift_cnt}"))
|
||||
|
||||
def _parseLikeMsg(self, payload):
|
||||
'''点赞消息'''
|
||||
@@ -328,19 +331,16 @@ class DouyinLiveWebFetcher:
|
||||
user_name = message.user.nick_name
|
||||
count = message.count
|
||||
print(f"【点赞msg】{user_name} 点了{count}个赞")
|
||||
asyncio.run(send_message(f"【点赞msg】{user_name} 点了{count}个赞"))
|
||||
|
||||
|
||||
async def _parseMemberMsg(self, payload):
|
||||
def _parseMemberMsg(self, payload):
|
||||
'''进入直播间消息'''
|
||||
message = MemberMessage().parse(payload)
|
||||
user_name = message.user.nick_name
|
||||
user_id = message.user.id
|
||||
gender = ["女", "男"][message.user.gender]
|
||||
print(f"【进场msg】[{user_id}][{gender}]{user_name} 进入了直播间")
|
||||
# asyncio.create_task(broadcast_message(f"【进场msg】[{user_id}][{gender}]{user_name} 进入了直播间"))
|
||||
# asyncio.run(send_message('123'))
|
||||
# 调用函数发送消息
|
||||
# await asyncio.to_thread(connect_and_send_message, 'test')
|
||||
# await broadcast_message(f"【进场msg】[{user_id}][{gender}]{user_name} 进入了直播间")
|
||||
asyncio.run(send_message(f"【进场msg】[{user_id}][{gender}]{user_name} 进入了直播间"))
|
||||
|
||||
def _parseSocialMsg(self, payload):
|
||||
@@ -349,6 +349,8 @@ class DouyinLiveWebFetcher:
|
||||
user_name = message.user.nick_name
|
||||
user_id = message.user.id
|
||||
print(f"【关注msg】[{user_id}]{user_name} 关注了主播")
|
||||
asyncio.run(send_message(f"【关注msg】[{user_id}]{user_name} 关注了主播"))
|
||||
|
||||
|
||||
def _parseRoomUserSeqMsg(self, payload):
|
||||
'''直播间统计'''
|
||||
@@ -356,12 +358,16 @@ class DouyinLiveWebFetcher:
|
||||
current = message.total
|
||||
total = message.total_pv_for_anchor
|
||||
print(f"【统计msg】当前观看人数: {current}, 累计观看人数: {total}")
|
||||
# asyncio.run(send_message(f"【统计msg】当前观看人数: {current}, 累计观看人数: {total}"))
|
||||
|
||||
|
||||
def _parseFansclubMsg(self, payload):
|
||||
'''粉丝团消息'''
|
||||
message = FansclubMessage().parse(payload)
|
||||
content = message.content
|
||||
print(f"【粉丝团msg】 {content}")
|
||||
asyncio.run(send_message(f"【粉丝团msg】 {content}"))
|
||||
|
||||
|
||||
def _parseEmojiChatMsg(self, payload):
|
||||
'''聊天表情包消息'''
|
||||
@@ -371,22 +377,29 @@ class DouyinLiveWebFetcher:
|
||||
common = message.common
|
||||
default_content = message.default_content
|
||||
print(f"【聊天表情包id】 {emoji_id},user:{user},common:{common},default_content:{default_content}")
|
||||
asyncio.run(send_message(f"【聊天表情包id】 {emoji_id},user:{user},common:{common},default_content:{default_content}"))
|
||||
|
||||
|
||||
def _parseRoomMsg(self, payload):
|
||||
message = RoomMessage().parse(payload)
|
||||
common = message.common
|
||||
room_id = common.room_id
|
||||
print(f"【直播间msg】直播间id:{room_id}")
|
||||
# asyncio.run(send_message(f"【直播间msg】直播间id:{room_id}"))
|
||||
|
||||
|
||||
def _parseRoomStatsMsg(self, payload):
|
||||
message = RoomStatsMessage().parse(payload)
|
||||
display_long = message.display_long
|
||||
print(f"【直播间统计msg】{display_long}")
|
||||
# asyncio.run(send_message(f"【直播间统计msg】{display_long}"))
|
||||
|
||||
|
||||
def _parseRankMsg(self, payload):
|
||||
message = RoomRankMessage().parse(payload)
|
||||
ranks_list = message.ranks_list
|
||||
print(f"【直播间排行榜msg】{ranks_list}")
|
||||
# asyncio.run(send_message(f"【直播间排行榜msg】{ranks_list}"))
|
||||
|
||||
def _parseControlMsg(self, payload):
|
||||
'''直播间状态消息'''
|
||||
@@ -394,9 +407,12 @@ class DouyinLiveWebFetcher:
|
||||
|
||||
if message.status == 3:
|
||||
print("直播间已结束")
|
||||
# asyncio.run(send_message(f"直播间已结束"))
|
||||
self.stop()
|
||||
|
||||
def _parseRoomStreamAdaptationMsg(self, payload):
|
||||
message = RoomStreamAdaptationMessage().parse(payload)
|
||||
adaptationType = message.adaptation_type
|
||||
print(f'直播间adaptation: {adaptationType}')
|
||||
# asyncio.run(send_message(f'直播间adaptation: {adaptationType}'))
|
||||
|
||||
|
||||
2
main.py
2
main.py
@@ -6,7 +6,7 @@ import asyncio
|
||||
from ws_server import start_ws_server
|
||||
|
||||
if __name__ == '__main__':
|
||||
live_id = '139226983757'
|
||||
live_id = '523570254212'
|
||||
room = DouyinLiveWebFetcher(live_id)
|
||||
room.get_room_status()
|
||||
room.start()
|
||||
|
||||
@@ -11,7 +11,9 @@ async def ws_handler(websocket, path=None):
|
||||
try:
|
||||
async for message in websocket:
|
||||
print(f"收到前端消息: {message}")
|
||||
await broadcast_message(f"回送消息:{message}") # 发送回客户端的消息
|
||||
# await broadcast_message(f"回送消息:{message}") # 发送回客户端的消息
|
||||
await broadcast_message(f"{message}") # 发送回客户端的消息
|
||||
|
||||
except websockets.exceptions.ConnectionClosed as e:
|
||||
print(f"连接关闭: {e}")
|
||||
finally:
|
||||
@@ -29,6 +31,8 @@ async def broadcast_message(msg):
|
||||
async def start_ws_server():
|
||||
"""启动WebSocket服务器"""
|
||||
server = await websockets.serve(ws_handler, '127.0.0.1', 8888)
|
||||
# server = await websockets.serve(ws_handler, '0.0.0.0', 8888)
|
||||
|
||||
print("WebSocket 服务器已启动 ws://127.0.0.1:8888")
|
||||
try:
|
||||
await server.wait_closed()
|
||||
|
||||
Reference in New Issue
Block a user