11from bilibili import bilibili
22from statistics import Statistics
33from printer import Printer
4+ from rafflehandler import Rafflehandler
45import utils
56import asyncio
67import random
@@ -24,13 +25,13 @@ async def handle_1_TV_raffle(num, real_roomid, raffleid):
2425 else :
2526 print (json_response2 )
2627
27- async def handle_1_activity_raffle (num , dic , text1 , text2 , raffleid ):
28+ async def handle_1_activity_raffle (num , giftId , text1 , text2 , raffleid ):
2829 #print('参与')
2930 await asyncio .sleep (random .uniform (1 , min (30 , num * 1.5 )))
3031 response1 = await bilibili ().get_gift_of_events_app (text1 , text2 , raffleid )
3132 pc_response = await bilibili ().get_gift_of_events_web (text1 , text2 , raffleid )
3233
33- Printer ().printlist_append (['join_lottery' , '' , 'user' , "参与了房间{:^9}的{}活动抽奖" .format (text1 , bilibili ().get_giftids_raffle (str (dic [ ' giftId' ] )))], True )
34+ Printer ().printlist_append (['join_lottery' , '' , 'user' , "参与了房间{:^9}的{}活动抽奖" .format (text1 , bilibili ().get_giftids_raffle (str (giftId )))], True )
3435
3536 json_response1 = await response1 .json ()
3637 json_pc_response = await pc_response .json ()
@@ -48,6 +49,61 @@ async def handle_1_activity_raffle(num, dic, text1, text2, raffleid):
4849 Statistics ().append_to_activitylist (raffleid , text1 )
4950 else :
5051 print (json_pc_response )
52+
53+ async def handle_1_room_TV (real_roomid ):
54+ await asyncio .sleep (random .uniform (3 , 5 ))
55+ await bilibili ().post_watching_history (real_roomid )
56+ result = await utils .check_room_true (real_roomid )
57+ if True in result :
58+ Printer ().printlist_append (['join_lottery' , '钓鱼提醒' , 'user' , "WARNING:检测到房间{:^9}的钓鱼操作" .format (real_roomid )], True )
59+ else :
60+ # print(True)
61+ response = await bilibili ().get_giftlist_of_TV (real_roomid )
62+ json_response = await response .json ()
63+ checklen = json_response ['data' ]['unjoin' ]
64+ num = len (checklen )
65+ list_available_raffleid = []
66+ for j in range (0 , num ):
67+ # await asyncio.sleep(random.uniform(0.5, 1))
68+ resttime = json_response ['data' ]['unjoin' ][j ]['dtime' ]
69+ raffleid = json_response ['data' ]['unjoin' ][j ]['id' ]
70+ if Statistics ().check_TVlist (raffleid ):
71+ list_available_raffleid .append (raffleid )
72+ tasklist = []
73+ num_available = len (list_available_raffleid )
74+ for raffleid in list_available_raffleid :
75+ task = asyncio .ensure_future (handle_1_TV_raffle (num_available , real_roomid , raffleid ))
76+ tasklist .append (task )
77+ if tasklist :
78+ await asyncio .wait (tasklist , return_when = asyncio .ALL_COMPLETED )
79+
80+ async def handle_1_room_activity (giftId , text1 , text2 ):
81+ await asyncio .sleep (random .uniform (3 , 5 ))
82+ await bilibili ().post_watching_history (text1 )
83+ result = await utils .check_room_true (text1 )
84+ if True in result :
85+ Printer ().printlist_append (['join_lottery' , '钓鱼提醒' , 'user' , "WARNING:检测到房间{:^9}的钓鱼操作" .format (text1 )], True )
86+ else :
87+ # print(True)
88+ response = await bilibili ().get_giftlist_of_events (text1 )
89+ json_response = await response .json ()
90+ checklen = json_response ['data' ]
91+ num = len (checklen )
92+ list_available_raffleid = []
93+ for j in range (0 , num ):
94+ # await asyncio.sleep(random.uniform(0.5, 1))
95+ resttime = checklen [j ]['time' ]
96+ raffleid = checklen [j ]['raffleId' ]
97+ if Statistics ().check_activitylist (raffleid ):
98+ list_available_raffleid .append (raffleid )
99+ tasklist = []
100+ num_available = len (list_available_raffleid )
101+ for raffleid in list_available_raffleid :
102+ task = asyncio .ensure_future (handle_1_activity_raffle (num_available , giftId , text1 , text2 , raffleid ))
103+ tasklist .append (task )
104+ if tasklist :
105+ await asyncio .wait (tasklist , return_when = asyncio .ALL_COMPLETED )
106+
51107
52108
53109class bilibiliClient ():
@@ -67,6 +123,10 @@ def __init__(self):
67123 'rep' : 'int' ,
68124 'url' : 'str'
69125 }
126+ def close_connection (self ):
127+ self ._writer .close ()
128+ self ._connected = False
129+
70130
71131 async def connectServer (self ):
72132 try :
@@ -134,7 +194,7 @@ async def ReadSocketData(self, len_wanted):
134194 return None
135195
136196 if not tmp :
137- print ("# FIN ,稍后将重连" )
197+ print ("# 主动关闭或者远端主动发来FIN ,稍后将重连" )
138198 self ._writer .close ()
139199 self .connected = False
140200 return None
@@ -203,33 +263,9 @@ async def parseDanMu(self, messages):
203263
204264 text1 = dic ['real_roomid' ]
205265 text2 = dic ['url' ]
206- Printer ().printlist_append (['join_lottery' , '' , 'user' , "检测到房间{:^9}的{}活动抽奖" .format (text1 , bilibili ().get_giftids_raffle (str (dic ['giftId' ])))], True )
207- await asyncio .sleep (random .uniform (3 , 5 ))
208- await bilibili ().post_watching_history (text1 )
209- result = await utils .check_room_true (text1 )
210- if True in result :
211- Printer ().printlist_append (['join_lottery' , '钓鱼提醒' , 'user' , "WARNING:检测到房间{:^9}的钓鱼操作" .format (text1 )], True )
212- else :
213- # print(True)
214- response = await bilibili ().get_giftlist_of_events (text1 )
215- json_response = await response .json ()
216- checklen = json_response ['data' ]
217- num = len (checklen )
218- list_available_raffleid = []
219- for j in range (0 , num ):
220- # await asyncio.sleep(random.uniform(0.5, 1))
221- resttime = checklen [j ]['time' ]
222- raffleid = checklen [j ]['raffleId' ]
223- if Statistics ().check_activitylist (raffleid ):
224- list_available_raffleid .append (raffleid )
225- tasklist = []
226- num_available = len (list_available_raffleid )
227- for raffleid in list_available_raffleid :
228- task = asyncio .ensure_future (handle_1_activity_raffle (num_available , dic , text1 , text2 , raffleid ))
229- tasklist .append (task )
230- if tasklist :
231- await asyncio .wait (tasklist , return_when = asyncio .ALL_COMPLETED )
232-
266+ giftId = dic ['giftId' ]
267+ Printer ().printlist_append (['join_lottery' , '' , 'user' , "检测到房间{:^9}的{}活动抽奖" .format (text1 , bilibili ().get_giftids_raffle (str (giftId )))], True )
268+ Rafflehandler ().append2list_activity (giftId , text1 , text2 )
233269
234270 elif dic ['giftId' ] == 39 :
235271 Printer ().printlist_append (['join_lottery' , '' , 'user' , "节奏风暴" ])
@@ -248,32 +284,9 @@ async def parseDanMu(self, messages):
248284 text2 = dic ['url' ]
249285 Printer ().printlist_append (['join_lottery' , '' , 'debug' , [dic , "请联系开发者" ]])
250286 try :
251- Printer ().printlist_append (['join_lottery' , '' , 'user' , "检测到房间{:^9}的{}活动抽奖" .format (text1 , bilibili ().get_giftids_raffle (str (dic ['giftId' ])))], True )
252- await asyncio .sleep (random .uniform (3 , 5 ))
253- await bilibili ().post_watching_history (text1 )
254- result = await utils .check_room_true (text1 )
255- if True in result :
256- Printer ().printlist_append (['join_lottery' , '钓鱼提醒' , 'user' , "WARNING:检测到房间{:^9}的钓鱼操作" .format (text1 )], True )
257- else :
258- # print(True)
259- response = await bilibili ().get_giftlist_of_events (text1 )
260- json_response = await response .json ()
261- checklen = json_response ['data' ]
262- num = len (checklen )
263- list_available_raffleid = []
264- for j in range (0 , num ):
265- # await asyncio.sleep(random.uniform(0.5, 1))
266- resttime = checklen [j ]['time' ]
267- raffleid = checklen [j ]['raffleId' ]
268- if Statistics ().check_activitylist (raffleid ):
269- list_available_raffleid .append (raffleid )
270- tasklist = []
271- num_available = len (list_available_raffleid )
272- for raffleid in list_available_raffleid :
273- task = asyncio .ensure_future (handle_1_activity_raffle (num_available , dic , text1 , text2 , raffleid ))
274- tasklist .append (task )
275- if tasklist :
276- await asyncio .wait (tasklist , return_when = asyncio .ALL_COMPLETED )
287+ giftId = dic ['giftId' ]
288+ Printer ().printlist_append (['join_lottery' , '' , 'user' , "检测到房间{:^9}的{}活动抽奖" .format (text1 , bilibili ().get_giftids_raffle (str (giftId )))], True )
289+ Rafflehandler ().append2list_activity (giftId , text1 , text2 )
277290
278291 except :
279292 pass
@@ -290,32 +303,8 @@ async def parseDanMu(self, messages):
290303 real_roomid = dic ['real_roomid' ]
291304 Printer ().printlist_append (['join_lottery' , '小电视' , 'user' , "检测到房间{:^9}的小电视抽奖" .format (real_roomid )], True )
292305 # 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"
293- await asyncio .sleep (random .uniform (3 , 5 ))
294- await bilibili ().post_watching_history (real_roomid )
295- result = await utils .check_room_true (real_roomid )
296- if True in result :
297- Printer ().printlist_append (['join_lottery' , '钓鱼提醒' , 'user' , "WARNING:检测到房间{:^9}的钓鱼操作" .format (real_roomid )], True )
298- else :
299- # print(True)
300- response = await bilibili ().get_giftlist_of_TV (real_roomid )
301- json_response = await response .json ()
302- checklen = json_response ['data' ]['unjoin' ]
303- num = len (checklen )
304- list_available_raffleid = []
305- for j in range (0 , num ):
306- # await asyncio.sleep(random.uniform(0.5, 1))
307- resttime = json_response ['data' ]['unjoin' ][j ]['dtime' ]
308- raffleid = json_response ['data' ]['unjoin' ][j ]['id' ]
309- if Statistics ().check_TVlist (raffleid ):
310- list_available_raffleid .append (raffleid )
311- tasklist = []
312- num_available = len (list_available_raffleid )
313- for raffleid in list_available_raffleid :
314- task = asyncio .ensure_future (handle_1_TV_raffle (num_available , real_roomid , raffleid ))
315- tasklist .append (task )
316- if tasklist :
317- await asyncio .wait (tasklist , return_when = asyncio .ALL_COMPLETED )
318-
306+ Rafflehandler ().append2list_TV (real_roomid )
307+
319308
320309
321310
0 commit comments