This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathbilibiliCilent.py
219 lines (200 loc) · 11.4 KB
/
bilibiliCilent.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from bilibili import bilibili
import asyncio
import random
from struct import *
import json
import datetime
import time
import hashlib
import requests
def CurrentTime():
currenttime = str(int(time.mktime(datetime.datetime.now().timetuple())))
return currenttime
class bilibiliClient(bilibili):
async def connectServer(self):
reader, writer = await asyncio.open_connection(self._ChatHost, self._ChatPort)
self._reader = reader
self._writer = writer
if (await self.SendJoinChannel(self._roomId) == True):
self.connected = True
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), '连接弹幕服务器成功')
await self.ReceiveMessageLoop()
async def HeartbeatLoop(self):
while self.connected == False:
await asyncio.sleep(0.5)
while self.connected == True:
await self.SendSocketData(0, 16, self._protocolversion, 2, 1, "")
await asyncio.sleep(30)
async def SendJoinChannel(self, channelId):
self._uid = (int)(100000000000000.0 + 200000000000000.0 * random.random())
body = '{"roomid":%s,"uid":%s}' % (channelId, self._uid)
await self.SendSocketData(0, 16, self._protocolversion, 7, 1, body)
return True
async def SendSocketData(self, packetlength, magic, ver, action, param, body):
bytearr = body.encode('utf-8')
if packetlength == 0:
packetlength = len(bytearr) + 16
sendbytes = pack('!IHHII', packetlength, magic, ver, action, param)
if len(bytearr) != 0:
sendbytes = sendbytes + bytearr
self._writer.write(sendbytes)
await self._writer.drain()
async def ReceiveMessageLoop(self):
while self.connected == True:
tmp = await self._reader.read(4)
expr, = unpack('!I', tmp)
tmp = await self._reader.read(2)
tmp = await self._reader.read(2)
tmp = await self._reader.read(4)
num, = unpack('!I', tmp)
tmp = await self._reader.read(4)
num2 = expr - 16
if num2 != 0:
num -= 1
if num == 0 or num == 1 or num == 2:
tmp = await self._reader.read(4)
num3, = unpack('!I', tmp)
self._UserCount = num3
continue
elif num == 3 or num == 4:
tmp = await self._reader.read(num2)
# strbytes, = unpack('!s', tmp)
try:
messages = tmp.decode('utf-8')
except:
continue
self.parseDanMu(messages)
continue
elif num == 5 or num == 6 or num == 7:
tmp = await self._reader.read(num2)
continue
else:
if num != 16:
tmp = await self._reader.read(num2)
else:
continue
def parseDanMu(self, messages):
try:
dic = json.loads(messages)
except:
return
cmd = dic['cmd']
if cmd == 'SYS_GIFT':
try:
headers = {
'Accept': 'application/json, text/plain, */*',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
'Accept-Language': 'zh-CN,zh;q=0.9',
'accept-encoding': 'gzip, deflate',
'Host': 'api.live.bilibili.com',
'cookie': self.cookie,
}
text1 = dic['real_roomid']
text2 = dic['url']
url = 'http://api.live.bilibili.com/activity/v1/Raffle/check?roomid=' + str(text1)
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), "检测到房间", str(text1).center(9), "的活动抽奖")
response = requests.get(url, headers=headers)
checklen = response.json()['data']
num = len(checklen)
while num != 0:
for j in range(0,num):
time.sleep(0.5)
resttime = response.json()['data'][j]['time']
raffleid = response.json()['data'][j]['raffleId']
if raffleid not in bilibili.activity_raffleid_list:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), "参与了房间", str(text1).center(9), "的活动抽奖")
bilibili.activity_raffleid_list.append(raffleid)
bilibili.activity_roomid_list.append(text1)
bilibili.activity_time_list.append(resttime)
headers = {
'Accept': 'application/json, text/plain, */*',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
'cookie': self.cookie,
'referer': text2
}
temp_params = 'access_key='+self.access_key+'&actionKey='+self.actionKey+'&appkey='+self.appkey+'&build='+self.build+'&device='+self.device+'&event_type=flower_rain-' + str(
raffleid) + '&mobi_app='+self.mobi_app+'&platform='+self.platform+'&room_id=' + str(
text1) + '&ts=' + CurrentTime()
params = temp_params + self.app_secret
hash = hashlib.md5()
hash.update(params.encode('utf-8'))
true_url = 'http://api.live.bilibili.com/YunYing/roomEvent?' + temp_params + '&sign=' + str(
hash.hexdigest())
pc_url = 'http://api.live.bilibili.com/activity/v1/Raffle/join?roomid=' + str(
text1) + '&raffleId=' + str(raffleid)
response1 = requests.get(true_url,params=params, headers=headers)
pc_response = requests.get(pc_url, headers=headers)
try:
print("# 移动端活动抽奖结果:", response1.json()['data']['gift_desc'])
except:
pass
try:
print("# 网页端活动抽奖状态:", pc_response.json()['message'])
except:
pass
break
except:
pass
return
if cmd == 'SYS_MSG':
try:
TV_url = dic['url']
real_roomid = dic['real_roomid']
#url = "https://api.live.bilibili.com/AppSmallTV/index?access_key=&actionKey=appkey&appkey=1d8b6e7d45233436&build=5230003&device=android&mobi_app=android&platform=android&roomid=939654&ts=1521734039&sign=4f85e1d3ce0e1a3acd46fcf9ca3cbeed"
temp_params = 'access_key=' + self.access_key + '&actionKey=' + self.actionKey + '&appkey=' + self.appkey + '&build=' + self.build + '&device=' + self.device +\
'&mobi_app=' + self.mobi_app + '&platform=' + self.platform + '&roomid=' + str(
real_roomid) + '&ts=' + CurrentTime()
params = temp_params + self.app_secret
hash = hashlib.md5()
hash.update(params.encode('utf-8'))
check_url = 'https://api.live.bilibili.com/AppSmallTV/index?' + temp_params + '&sign=' + str(
hash.hexdigest())
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), "监测到房间", str(real_roomid).center(9), "的小电视抽奖")
# headers = {
# 'Accept': 'application/json, text/plain, */*',
# 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
# 'Accept-Language': 'zh-CN,zh;q=0.9',
# 'accept-encoding': 'gzip, deflate',
# 'Host': 'api.live.bilibili.com',
# 'cookie': self.cookie,
# }
response = requests.get(check_url, headers=self.appheaders)
checklen = response.json()['data']['unjoin']
num = len(checklen)
while num != 0:
for j in range(0,num):
time.sleep(0.5)
resttime = response.json()['data']['unjoin'][j]['dtime']
raffleid = response.json()['data']['unjoin'][j]['id']
if raffleid not in bilibili.TV_raffleid_list:
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), "参与了房间", str(real_roomid).center(9), "的小电视抽奖")
bilibili.TV_raffleid_list.append(raffleid)
bilibili.TV_roomid_list.append(real_roomid)
bilibili.TV_time_list.append(resttime)
#url = "https://api.live.bilibili.com/AppSmallTV/join?access_key=&actionKey=appkey&appkey=1d8b6e7d45233436&build=5230003&device=android&id=41581&mobi_app=android&platform=android&roomid=3566261&ts=1521731305&sign=ae3d61f496c66069bcfd299fe7ce1792"
temp_params = 'access_key='+self.access_key+'&actionKey='+self.actionKey+'&appkey='+self.appkey+'&build='+self.build+'&device='+self.device+'&id=' + str(
raffleid) + '&mobi_app='+self.mobi_app+'&platform='+self.platform+'&roomid=' + str(
real_roomid) + '&ts=' + CurrentTime()
params = temp_params + self.app_secret
hash = hashlib.md5()
hash.update(params.encode('utf-8'))
true_url = 'http://api.live.bilibili.com/AppSmallTV/join?' + temp_params + '&sign=' + str(
hash.hexdigest())
# headers = {
# 'Accept': 'application/json, text/plain, */*',
# 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
# 'Accept-Language': 'zh-CN,zh;q=0.9',
# 'accept-encoding': 'gzip, deflate',
# 'Host': 'api.live.bilibili.com',
# 'cookie': self.cookie,
# 'referer': TV_url
# }
# url1 = 'http://api.live.bilibili.com/gift/v2/smalltv/join?roomid=' + str(
# real_roomid) + '&raffleId=' + str(raffleid)
# #response1 = requests.get(url1, headers=headers)
response2 = requests.get(true_url,headers=self.appheaders)
print("# 小电视道具抽奖状态:",response2.json()['msg'])
break
except:
pass
return