forked from Prince-cool/dy_protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
188 lines (161 loc) · 7.58 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from websocket import WebSocketApp
import json
import base64
import re
from urllib.parse import unquote_plus
import requests
import new_pb2 as pb
import execjs
ctx=execjs.compile(open('get_handle.js','r').read())
pushproto_PushFrame = pb.pushproto_PushFrame()
pushproto_PushFrame.payloadtype = "hb"
ping_byte = pushproto_PushFrame.SerializeToString()
# print(ping_byte)
def fetch_live_room_info(url):
s=requests.session()
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",
}
response = s.get(url, headers=headers)
cookies_dict = dict(response.cookies)
response = s.get(url, headers=headers, cookies=cookies_dict)
ttwid = dict(response.cookies).get('ttwid')
data_string = re.findall(r'<script id="RENDER_DATA" type="application/json">(.*?)</script>', response.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']
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&im_path=/webcast/im/fetch/&device_platform=web&room_id={room_id}"
# print(wss_url)
# print(room_id, room_title, room_user_count)
return room_id, room_title, room_user_count, wss_url, ttwid
def keymessage(ws,byte,logid,payloadtype):
webcast_im_Response = pb.webcast_im_Response()
webcast_im_Response.ParseFromString(byte)
# print(webcast_im_Response)
for item in webcast_im_Response.messages:
# print(item.method)
if item.method == 'WebcastMemberMessage':
webcast_im_MemberMessage = pb.webcast_im_MemberMessage()
webcast_im_MemberMessage.ParseFromString(item.payload)
# print(webcast_im_MemberMessage)
try:
nickname = webcast_im_MemberMessage.user.nickname
text = webcast_im_MemberMessage.common.displaytext.defaultpattern.strip('{0:user} ').strip('{1:string}')
print(nickname+':'+text)
except:
pass
if item.method == 'WebcastChatMessage':
webcast_im_ChatMessage = pb.webcast_im_ChatMessage()
webcast_im_ChatMessage.ParseFromString(item.payload)
# print(webcast_im_ChatMessage)
try:
nickname = webcast_im_ChatMessage.user.nickname
# print(nickname)
content = webcast_im_ChatMessage.content
# print(content)
# print(nickname, ':', content)
except:
pass
if item.method == 'WebcastGiftMessage':
webcast_im_GiftMessage = pb.webcast_im_GiftMessage()
webcast_im_GiftMessage.ParseFromString(item.payload)
# print(webcast_im_GiftMessage)
print(webcast_im_GiftMessage.common.describe)
try:
user = webcast_im_GiftMessage.user.nickname
gift = webcast_im_GiftMessage.gift.name
# print(webcast_im_GiftMessage.common.describe)
except:
pass
if item.method == 'WebcastSocialMessage':
webcast_im_SocialMessage = pb.webcast_im_SocialMessage()
webcast_im_SocialMessage.ParseFromString(item.payload)
try:
user = webcast_im_SocialMessage.user.nickname
text = webcast_im_SocialMessage.common.displaytext.defaultpattern.strip('{0:user} ')
# print(webcast_im_SocialMessage)
# print(user,text)
except:
pass
if item.method == 'WebcastLikeMessage':
webcast_im_LikeMessage = pb.webcast_im_LikeMessage()
webcast_im_LikeMessage.ParseFromString(item.payload)
try:
user = webcast_im_LikeMessage.user.nickname
# print(user)
text = webcast_im_LikeMessage.common.displaytext.pieces[-1].stringvalue
# print(text)
# print(user,text)
except:
pass
# print(webcast_im_LikeMessage)
if item.method == 'WebcastRoomStatsMessage':
webcast_im_RoomStatsMessage = pb.webcast_im_RoomStatsMessage()
webcast_im_RoomStatsMessage.ParseFromString(item.payload)
print(webcast_im_RoomStatsMessage)
needack = webcast_im_Response.needack
# print(needack)
if needack:
internalext = webcast_im_Response.internalext
# print(webcast_im_Response.cursor)
ackpayload = ctx.call('get_ackpayload', internalext)
# print(ackpayload)
ackpayload = base64.b64decode(ackpayload)
# print(ackpayload)
pushproto_PushFrame2 = pb.pushproto_PushFrame()
pushproto_PushFrame2.payloadtype = 'ack'
pushproto_PushFrame2.payload = ackpayload
pushproto_PushFrame2.logid = logid
# print(pushproto_PushFrame2.SerializeToString())
ws.send(pushproto_PushFrame2.SerializeToString())
if payloadtype == 'close':
ws.close()
def on_open(ws):
print('连接成功!!!')
ws.send(ping_byte)
def on_message(ws, content):
# print('已获得数据:')
pushproto_PushFrame.ParseFromString(content)
logid=pushproto_PushFrame.logid
payloadtype=pushproto_PushFrame.payloadtype
# print(pushproto_PushFrame.headers)
headers_list = {}
for item in pushproto_PushFrame.headers:
headers_list[item.key] = item.value
# print(headers_list)
if 'compress_type' in headers_list and headers_list['compress_type'] == 'gzip':
payload = pushproto_PushFrame.payload
payload = base64.b64encode(payload).decode()
string = ctx.call('handle', payload)
byte = base64.b64decode(string)
keymessage(ws,byte,logid,payloadtype)
else:
payload = pushproto_PushFrame.payload
keymessage(ws, payload, logid,payloadtype)
def on_ping(ws):
ws.send(ping_byte)
def on_error(ws,errormessag):
print("on_error:")
print(errormessag)
def on_close(ws, content):
print("连接已关闭!!!")
def run():
web_url = "https://live.douyin.com/158341241526"
room_id, room_title, room_user_count, wss_url, ttwid = fetch_live_room_info(web_url)
print(f'欢迎来到{room_title}直播间,直播间人数{room_user_count}')
ws = WebSocketApp(
url=wss_url,
header={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
},
cookie=f"ttwid={ttwid}",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close,
on_ping=on_ping
)
ws.run_forever(ping_interval=10)
if __name__ == '__main__':
run()