danmu-sim/refs/陌陌登陆弹幕备份.py

88 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
import websockets
import time
import json
import datetime
async def connect():
uri = "wss://live-ws.immomo.com/ws/im"
async with websockets.connect(uri) as websocket:
print(f"链接websocket:[{uri}] 完成")
momoid = "1062302597"
roomid = "17066184578990"
token = "b458d6fd04ee38a9e5330e76a3802df9"
ping_msg_id = 2 # 心跳包的id2
client_time = str(int(time.time() * 1000))
data = '{{"msg_id":1,"client_time":{0},"type":"Sauth","data":{{"momoid":"{1}","roomid":"{2}","role":6,"isVisitor":false,"token":"{3}","ua":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0"}}}}'.format(
client_time, momoid, roomid, token)
await websocket.send(data)
print("Send Sauth")
while True:
try:
response = await websocket.recv()
current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
#print(f"[{current_time}] 收到原始消息 => " + response)
# print("收到原始消息 => " + response)
try:
json_data = json.loads(response)
# print("收到JSON消息 => ", json_data)
message_type = json_data['groups'][0]['units'][0]['type']
if message_type == "Message":
nick = json_data['groups'][0]['nick']
text = json_data['groups'][0]['units'][0]['Msg.Message.data']['text']
print(f"[{current_time}] {nick} : {text}")
elif message_type == "EnterRoom":
text = json_data['groups'][0]['units'][0]['Set.EnterRoom.data']['text']
print(f"[{current_time}] 有新人: {text}")
elif message_type == "BiliBili":
nick = json_data['groups'][0]['nick']
text = json_data['groups'][0]['units'][0]['Pay.BiliBili.data']['text']
print(f"[{current_time}] {nick} : {text}")
elif message_type == "GiftV3":
nick = json_data['groups'][0]['nick']
text = json_data['groups'][0]['units'][0]['GiftV3.giftV3']['text']
product_id = json_data['groups'][0]['units'][0]['GiftV3.giftV3']['productId']
print(f"[{current_time}] 用户: {nick} 送出礼物,内容为: {text}产品ID为: {product_id}")
except KeyError as e:
print("JSON数据缺少:", e)
except json.JSONDecodeError as e:
print("无法解析JSON格式: ", e)
except websockets.exceptions.ConnectionClosedError:
print(f"[{current_time}] 与服务器断开链接")
break
async def send_ping(websocket):
ping_msg_id = 2
await asyncio.sleep(30) # 延迟30秒后开始发送心跳包
while True:
client_time = str(int(time.time() * 1000))
ping_data = '{{"msg_id":{0},"client_time":{1},"type":"Ping","data":{{}}}}'.format(ping_msg_id, client_time)
try:
await websocket.send(ping_data)
print("Send Ping", ping_data)
ping_msg_id += 1
await asyncio.sleep(30) # 每隔30秒发送一次
except websockets.exceptions.ConnectionClosedError:
print("与服务器断开链接")
break
async def main():
websocket = await websockets.connect("wss://live-ws.immomo.com/ws/im")
connect_task = asyncio.create_task(connect())
ping_task = asyncio.create_task(send_ping(websocket))
await connect_task
await ping_task
asyncio.run(main())