This repository has been archived by the owner on Jul 2, 2019. It is now read-only.
forked from Dawnnnnnn/bilibili-live-tools
-
Notifications
You must be signed in to change notification settings - Fork 23
/
rafflehandler.py
322 lines (285 loc) · 12.6 KB
/
rafflehandler.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from online_net import OnlineNet
from statistics import Statistics
import printer
import utils
import asyncio
import time
import random
from bilitimer import BiliTimer
def CurrentTime():
currenttime = int(time.time())
return currenttime
class Rafflehandler:
__slots__ = ('queue_raffle', 'list_raffle_id')
instance = None
def __new__(cls, *args, **kw):
if not cls.instance:
cls.instance = super(Rafflehandler, cls).__new__(cls, *args, **kw)
cls.instance.queue_raffle = asyncio.Queue()
cls.instance.list_raffle_id = []
return cls.instance
async def run(self):
while True:
raffle = await self.queue_raffle.get()
await asyncio.sleep(0.5)
list_raffle0 = [self.queue_raffle.get_nowait() for i in range(self.queue_raffle.qsize())]
list_raffle0.append(raffle)
list_raffle = list(set(list_raffle0))
# print('过滤完毕')
# if len(list_raffle) != len(list_raffle0):
# print('过滤机制起作用')
tasklist = []
for i in list_raffle:
i = list(i)
i[0] = list(i[0])
for j in range(len(i[0])):
if isinstance(i[0][j], tuple):
# print('检测')
# i[0] = list(i[0])
i[0][j] = await i[0][j][1](*(i[0][j][0]))
# print(i)
task = asyncio.ensure_future(i[1](*i[0]))
tasklist.append(task)
# await asyncio.wait(tasklist, return_when=asyncio.ALL_COMPLETED)
@staticmethod
def Put2Queue(value, func):
# print('welcome to appending')
Rafflehandler.instance.queue_raffle.put_nowait((value, func))
# print('appended')
return
@staticmethod
async def Put2Queue_wait(value, func):
# print('welcome to appending')
await Rafflehandler.instance.queue_raffle.put((value, func))
# print('appended')
return
@staticmethod
def getlist():
print('目前TV任务队列状况', Rafflehandler.instance.queue_raffle.qsize())
def add2raffle_id(self, raffle_id):
self.list_raffle_id.append(raffle_id)
if len(self.list_raffle_id) > 150:
# print(self.list_raffle_id)
del self.list_raffle_id[:75]
# print(self.list_raffle_id)
def check_duplicate(self, raffle_id):
return (raffle_id in self.list_raffle_id)
async def handle_1_TV_raffle(num, real_roomid, raffleid, raffle_type):
while True:
json_response2 = await OnlineNet().req('get_gift_of_TV_app', real_roomid, raffleid, raffle_type)
code = json_response2['code']
if not code:
break
elif code == -403:
return True
elif code == -405:
print('没抢到。。。。。')
printer.warn(f'{raffleid} {raffle_type} {num}')
return False
elif code == 400:
print(json_response2)
return
tasklist = []
for i in range(60):
task = asyncio.ensure_future(handle_1_TV_raffle_black(num, real_roomid, raffleid, raffle_type))
tasklist.append(task)
await asyncio.wait(tasklist, return_when=asyncio.FIRST_COMPLETED)
return
elif code != -401 and code != -403:
pass
print('如果循环请联系作者', num, real_roomid, raffleid, raffle_type)
data = json_response2['data']
Statistics.append_to_TVlist(raffleid, real_roomid)
Statistics.add_to_result(data['gift_name'], int(data['gift_num']))
printer.info([f'参与了房间{real_roomid:^9}的道具抽奖'], True)
# printer.info([f'# 道具抽奖状态: {json_response2["msg"]}'])
printer.info([f'# 房间{real_roomid:^9}网页端活动抽奖结果: {data["gift_name"]}X{data["gift_num"]}'])
return True
async def handle_1_guard_raffle(num, roomid, raffleid):
await asyncio.sleep(random.uniform(0.5, min(30, num * 1.3)))
json_response2 = await OnlineNet().req('get_gift_of_guard', roomid, raffleid)
# print(json_response2)
if not json_response2['code']:
print("# 获取到房间 %s 的提督/舰长奖励: " % (roomid), json_response2['data']['message'])
# print(json_response2)
Statistics.append_to_guardlist()
else:
print(json_response2)
return True
async def handle_1_storm_raffle(id):
json_response1 = await OnlineNet().req('get_gift_of_storm', id)
print(json_response1)
async def handle_1_activity_raffle(num, text1, raffleid):
# print('参与')
# await asyncio.sleep(random.uniform(0.5, min(30, num * 1.3)))
json_response1 = await OnlineNet().req('get_gift_of_events_app', text1, raffleid)
# json_pc_response = await OnlineNet().req('get_gift_of_events_web', text1, text2, raffleid)
# print(json_response1)
printer.info([f'参与了房间{text1:^9}的活动抽奖'], True)
if not json_response1['code']:
printer.info([f'# 移动端活动抽奖结果: {json_response1["data"]["gift_desc"]}'])
Statistics.add_to_result(*(json_response1['data']['gift_desc'].split('X')))
elif json_response1['code'] == 400:
print(json_response1)
return
tasklist = []
for i in range(60):
task = asyncio.ensure_future(handle_1_activity_raffle_black(num, text1, raffleid))
tasklist.append(task)
await asyncio.wait(tasklist, return_when=asyncio.FIRST_COMPLETED)
return
# print(json_response1)
else:
printer.info([f'# 移动端活动抽奖结果: {json_response1}'])
# printer.info(
# [f'# 网页端活动抽奖状态: {json_pc_response}'])
# if not json_pc_response['code']:
# Statistics.append_to_activitylist(raffleid, text1)
# else:
# print(json_pc_response)
return True
async def handle_1_room_TV(real_roomid):
result = await utils.enter_room(real_roomid)
if result:
json_response = await OnlineNet().req('get_giftlist_of_TV', real_roomid)
current_time = CurrentTime()
# print(json_response['data']['list'])
checklen = json_response['data']['list']
list_available_raffleid = []
for j in checklen:
raffle_id = j['raffleId']
raffle_type = j['type']
time_wanted = j['time_wait'] + current_time
# 处理一些重复
if not Rafflehandler().check_duplicate(raffle_id):
print(raffle_id)
list_available_raffleid.append((raffle_id, raffle_type, time_wanted))
Rafflehandler().add2raffle_id(raffle_id)
num_available = len(list_available_raffleid)
# print(list_available_raffleid)
for raffle_id, raffle_type, time_wanted in list_available_raffleid:
BiliTimer.append2list_jobs(handle_1_TV_raffle, time_wanted, (num_available, real_roomid, raffle_id, raffle_type))
async def handle_1_room_storm(roomid):
result = await utils.enter_room(roomid)
if result:
temp = await OnlineNet().req('get_giftlist_of_storm', roomid)
check = len(temp['data'])
list_available_raffleid = []
if check != 0 and temp['data']['hasJoin'] != 1:
id = temp['data']['id']
list_available_raffleid.append((id, 0))
for id, time_wanted in list_available_raffleid:
BiliTimer.append2list_jobs(handle_1_storm_raffle, time_wanted, (id,))
async def handle_1_room_activity(text1):
result = await utils.enter_room(text1)
if result:
json_response = await OnlineNet().req('get_giftlist_of_events', text1)
# print(json_response)
checklen = json_response['data']['lotteryInfo']
list_available_raffleid = []
for j in checklen:
# await asyncio.sleep(random.uniform(0.5, 1))
# resttime = j['time']
raffleid = j['eventType']
# if Statistics.check_activitylist(text1, raffleid):
list_available_raffleid.append(raffleid)
tasklist = []
num_available = len(list_available_raffleid)
# print(list_available_raffleid)
for raffleid in list_available_raffleid:
task = asyncio.ensure_future(handle_1_activity_raffle(num_available, text1, raffleid))
tasklist.append(task)
if tasklist:
raffle_results = await asyncio.gather(*tasklist)
if False in raffle_results:
print('有繁忙提示,稍后重新尝试')
Rafflehandler.Put2Queue((text1), handle_1_room_activity)
async def handle_1_room_guard(roomid, raffleid=None):
result = await utils.enter_room(roomid)
if result:
if raffleid is not None:
json_response1 = {'data': [{'id': raffleid}]}
else:
for i in range(10):
json_response1 = await OnlineNet().req('get_giftlist_of_guard', roomid)
# print(json_response1)
if not json_response1['data']:
await asyncio.sleep(1)
else:
break
if not json_response1['data']:
print(f'{roomid}没有guard或者guard已经领取')
return
list_available_raffleid = []
# guard这里领取后,list对应会消失,其实就没有status了,这里是为了统一
for j in json_response1['data']:
raffle_id = j['id']
if not Rafflehandler().check_duplicate(raffle_id):
# print(raffle_id)
list_available_raffleid.append(raffle_id)
Rafflehandler().add2raffle_id(raffle_id)
tasklist = []
num_available = len(list_available_raffleid)
for raffleid in list_available_raffleid:
task = asyncio.ensure_future(handle_1_guard_raffle(num_available, roomid, raffleid))
tasklist.append(task)
if tasklist:
raffle_results = await asyncio.gather(*tasklist)
if False in raffle_results:
print('有繁忙提示,稍后重新尝试')
Rafflehandler.Put2Queue((roomid,), handle_1_room_guard)
async def handle_1_TV_raffle_black(num, real_roomid, raffleid, raffle_type):
# print('ffffffffffggggdgdfddf')
for i in range(50):
json_response2 = await OnlineNet().req('get_gift_of_TV_app', real_roomid, raffleid, raffle_type)
code = json_response2['code']
if not code:
break
elif code == -403:
return True
elif code == -405:
print('没抢到。。。。。')
printer.warn(raffleid)
return False
elif code != -401 and code != -403:
# print('00', json_response2)
pass
# await asyncio.sleep()
code = json_response2['code']
if code:
await asyncio.sleep(5)
return
data = json_response2['data']
Statistics.append_to_TVlist(raffleid, real_roomid)
Statistics.add_to_result(data['gift_name'], int(data['gift_num']))
printer.info([f'参与了房间{real_roomid:^9}的道具抽奖'], True)
# printer.info([f'# 道具抽奖状态: {json_response2["msg"]}'])
printer.info([f'# 房间{real_roomid:^9}网页端活动抽奖结果: {data["gift_name"]}X{data["gift_num"]}'])
return True
async def handle_1_activity_raffle_black(num, text1, raffleid):
# print('参与')
# await asyncio.sleep(random.uniform(0.5, min(30, num * 1.3)))
for i in range(50):
json_response1 = await OnlineNet().req('get_gift_of_events_app', text1, raffleid)
code = json_response1['code']
if not code:
break
elif code == -403:
return True
elif code == -405:
print('没抢到。。。。。')
printer.warn(raffleid)
return False
elif code != -401 and code != -403:
# print('00', json_response2)
pass
# json_pc_response = await OnlineNet().req('get_gift_of_events_web', text1, text2, raffleid)
# print(json_response1)
printer.info([f'参与了房间{text1:^9}的活动抽奖'], True)
if not json_response1['code']:
printer.info([f'# 移动端活动抽奖结果: {json_response1["data"]["gift_desc"]}'])
Statistics.add_to_result(*(json_response1['data']['gift_desc'].split('X')))
else:
# print(json_response1)
printer.info([f'# 移动端活动抽奖结果: {json_response1}'])
return True