Skip to content
This repository was archived by the owner on Jun 6, 2023. It is now read-only.

Commit 1614d93

Browse files
author
yjqiang
committed
大更新
弹幕模块基本非堵塞了,而且尽量避免了不必要的重复访问同一房间,抽奖并行
1 parent 4f3d5e0 commit 1614d93

File tree

4 files changed

+142
-85
lines changed

4 files changed

+142
-85
lines changed

bilibiliCilent.py

Lines changed: 71 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from bilibili import bilibili
22
from statistics import Statistics
33
from printer import Printer
4+
from rafflehandler import Rafflehandler
45
import utils
56
import asyncio
67
import 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

53109
class 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

connect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async def connect(self):
4242
def reconnect(self, roomid):
4343
self.tag_reconnect = True
4444
if self.danmuji is not None:
45-
self.danmuji.connected = False
45+
self.danmuji.close_connection()
4646
bilibili().dic_bilibili['roomid'] = roomid
4747
print('已经切换roomid')
4848
self.tag_reconnect = False

rafflehandler.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import asyncio
2+
import bilibiliCilent
3+
4+
class Rafflehandler:
5+
instance = None
6+
7+
def __new__(cls, *args, **kw):
8+
if not cls.instance:
9+
cls.instance = super(Rafflehandler, cls).__new__(cls, *args, **kw)
10+
cls.instance.list_activity = []
11+
cls.instance.list_TV = []
12+
return cls.instance
13+
14+
async def run(self):
15+
while True:
16+
len_list_activity = len(self.list_activity)
17+
len_list_TV = len(self.list_TV)
18+
19+
#print('准备执行')
20+
21+
# 过滤相同房间
22+
# set_activity = set(self.list_activity)
23+
set_activity = []
24+
for i in self.list_activity:
25+
if i not in set_activity:
26+
set_activity.append(i)
27+
set_TV = set(self.list_TV)
28+
#print('过滤完毕')
29+
#if len(set_activity) != len_list_activity or len(set_TV) != len_list_TV:
30+
#print('过滤机制起作用')
31+
32+
tasklist = []
33+
for i in set_TV:
34+
task = asyncio.ensure_future(bilibiliCilent.handle_1_room_TV(i))
35+
tasklist.append(task)
36+
for i in set_activity:
37+
task = asyncio.ensure_future(bilibiliCilent.handle_1_room_activity(i[0], i[1], i[2]))
38+
tasklist.append(task)
39+
if tasklist:
40+
await asyncio.wait(tasklist, return_when=asyncio.ALL_COMPLETED)
41+
#print('本批次结束')
42+
else:
43+
#print('本批次轮空')
44+
pass
45+
46+
del self.list_activity[:len_list_activity]
47+
del self.list_TV[:len_list_TV]
48+
49+
await asyncio.sleep(5)
50+
51+
52+
53+
def append2list_TV(self, real_roomid):
54+
#print('welcome to appending')
55+
self.list_TV.append(real_roomid)
56+
print('appended TV')
57+
return
58+
59+
def append2list_activity(self, giftId, text1, text2):
60+
#print('welcome to appending')
61+
self.list_activity.append([giftId, text1, text2])
62+
print('appended activity')
63+
return
64+
65+

run.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from LotteryResult import LotteryResult
44
from Tasks import Tasks
55
from connect import connect
6+
from rafflehandler import Rafflehandler
67
import asyncio
78
import utils
89
from printer import Printer
@@ -23,7 +24,7 @@
2324

2425
Statistics()
2526

26-
27+
rafflehandler = Rafflehandler()
2728

2829

2930
task = OnlineHeart()
@@ -48,7 +49,9 @@
4849
task2.run(),
4950
task4.connect(),
5051
task3.query(),
51-
printer.clean_printlist()
52+
printer.clean_printlist(),
53+
rafflehandler.run()
54+
5255
]
5356

5457
loop.run_until_complete(asyncio.wait(tasks))

0 commit comments

Comments
 (0)