forked from sxchou/douyin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv6.py
97 lines (75 loc) · 6.11 KB
/
v6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from websocket import WebSocketApp
import json
import re
import gzip
from urllib.parse import unquote_plus
import requests
from douyin_pb2 import PushFrame, Response, ChatMessage
def fetch_live_room_info(url):
res = requests.get(
url=url,
headers={
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
},
cookies={
"__ac_nonce": "063abcffa00ed8507d599" # 可以是任意值
}
)
data_string = re.findall(r'<script id="RENDER_DATA" type="application/json">(.*?)</script>', res.text)[0]
data_dict = json.loads(unquote_plus(data_string))
room_id = data_dict['app']['initialState']['roomStore']['roomInfo']['roomId']
room_title = data_dict['app']['initialState']['roomStore']['roomInfo']["room"]['title']
room_user_count = data_dict['app']['initialState']['roomStore']['roomInfo']["room"]['user_count_str']
print(room_id,room_title,room_user_count)
# wss_url = f"wss://webcast3-ws-web-lq.douyin.com/webcast/im/push/v2/?app_name=douyin_web&version_code=180800&webcast_sdk_version=1.3.0&update_version_code=1.3.0&compress=gzip&internal_ext=internal_src:dim|wss_push_room_id:{room_id}|wss_push_did:7140459943756301854|dim_log_id:202212281349305A73D850664DB518C21B|fetch_time:1672206570185|seq:1|wss_info:0-1672206570185-0-0|wrds_kvs:WebcastRoomStatsMessage-1672206566915058992_InputPanelComponentSyncData-1672187049066887013_WebcastRoomRankMessage-1672206560973484605&cursor=t-1672206570185_r-1_d-1_u-1_h-1&host=https://live.douyin.com&aid=6383&live_id=1&did_rule=3&debug=false&endpoint=live_pc&support_wrds=1&im_path=/webcast/im/fetch/&device_platform=web&cookie_enabled=true&screen_width=1920&screen_height=1080&browser_language=zh-CN&browser_platform=MacIntel&browser_name=Mozilla&browser_version=5.0%20(Macintosh;%20Intel%20Mac%20OS%20X%2010_15_7)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/108.0.0.0%20Safari/537.36&browser_online=true&tz_name=Asia/Shanghai&identity=audience&room_id={room_id}&heartbeatDuration=0"
# wss_url = f"wss://webcast3-ws-web-hl.douyin.com/webcast/im/push/v2/?app_name=douyin_web&version_code=180800&webcast_sdk_version=1.3.0&update_version_code=1.3.0&compress=gzip&internal_ext=internal_src:dim|wss_push_room_id:{room_id}|wss_push_did:7226994872320853536|dim_log_id:202304291308475D8B6F9874DF2738389E|fetch_time:1682744927517|seq:1|wss_info:0-1682744927517-0-0|wrds_kvs:InputPanelComponentSyncData-1682736868802666217_WebcastRoomStatsMessage-1682744926239473678_HighlightContainerSyncData-2_WebcastRoomRankMessage-1682744902282424664&cursor=t-1682744927517_r-1_d-1_u-1_h-1&host=https://live.douyin.com&aid=6383&live_id=1&did_rule=3&debug=false&maxCacheMessageNumber=20&endpoint=live_pc&support_wrds=1&im_path=/webcast/im/fetch/&user_unique_id=7226994872320853536&device_platform=web&cookie_enabled=true&screen_width=1280&screen_height=720&browser_language=zh-CN&browser_platform=Win32&browser_name=Mozilla&browser_version=5.0%20(Windows%20NT%2010.0;%20Win64;%20x64)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/112.0.0.0%20Safari/537.36%20Edg/112.0.1722.58&browser_online=true&tz_name=Asia/Shanghai&identity=audience&room_id=7227299538485250875&heartbeatDuration=0&signature=WhNLEMkQwySOnKw4"
wss_url = f"wss://webcast3-ws-web-lf.douyin.com/webcast/im/push/v2/?app_name=douyin_web&version_code=180800&webcast_sdk_version=1.3.0&update_version_code=1.3.0&compress=gzip&internal_ext=internal_src:dim|wss_push_room_id:7227287237401103162|wss_push_did:7226994872320853536|dim_log_id:202304291350225A1A70E68A8F1A7D3C8C|fetch_time:1682747422362|seq:1|wss_info:0-1682747422362-0-0|wrds_kvs:WebcastRoomRankMessage-1682747416033110963_WebcastRoomStatsMessage-1682747422005489045&cursor=d-1_u-1_h-1_t-1682747422362_r-1&host=https://live.douyin.com&aid=6383&live_id=1&did_rule=3&debug=false&maxCacheMessageNumber=20&endpoint=live_pc&support_wrds=1&im_path=/webcast/im/fetch/&user_unique_id=7226994872320853536&device_platform=web&cookie_enabled=true&screen_width=1280&screen_height=720&browser_language=zh-CN&browser_platform=Win32&browser_name=Mozilla&browser_version=5.0%20(Windows%20NT%2010.0;%20Win64;%20x64)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/112.0.0.0%20Safari/537.36%20Edg/112.0.1722.58&browser_online=true&tz_name=Asia/Shanghai&identity=audience&room_id=7227287237401103162&heartbeatDuration=0&signature=WKnJeNoE8JA6S4W1"
# print(wss_url)
ttwid = res.cookies.get_dict()['ttwid']
return room_id, room_title, room_user_count, wss_url, ttwid
def on_open(ws, content):
print('on_open')
def on_message(ws, content):
frame = PushFrame()
frame.ParseFromString(content)
# 对PushFrame的 payload 内容进行gzip解压
origin_bytes = gzip.decompress(frame.payload)
# 根据Response+gzip解压数据,生成数据对象
response = Response()
response.ParseFromString(origin_bytes)
if response.needAck:
s = PushFrame()
s.payloadType = "ack"
s.payload = response.internalExt.encode('utf-8')
s.logId = frame.logId
ws.send(s.SerializeToString())
# 获取数据内容(需根据不同method,使用不同的结构对象对 数据 进行解析)
# 注意:此处只处理 WebcastChatMessage ,其他处理方式都是类似的。
for item in response.messagesList:
if item.method != "WebcastChatMessage":
continue
message = ChatMessage()
message.ParseFromString(item.payload)
info = f"【{message.user.nickName}】{message.content} "
print(info)
def on_error(ws, content):
print("on_error")
def on_close(ws, content):
print("on_close")
def run():
# web_url = "https://live.douyin.com/80017709309"
web_url = "https://live.douyin.com/685317364746"
room_id, room_title, room_user_count, wss_url, ttwid = fetch_live_room_info(web_url)
print(ttwid)
ws = WebSocketApp(
url=wss_url,
header={},
cookie=f"ttwid={ttwid}",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close,
)
ws.run_forever()
if __name__ == '__main__':
run()