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

Commit 0978acc

Browse files
author
yjqiang
committed
修复了console
1 parent ab62742 commit 0978acc

File tree

5 files changed

+97
-51
lines changed

5 files changed

+97
-51
lines changed

bilibili.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ async def request_send_danmu_msg_andriod(self, msg, roomId):
210210
# 必须为 "json"
211211
'type': "json"
212212
}
213-
print('send msg app')
213+
# print('send msg app')
214214

215215
response = await self.bili_section_post(url, headers=self.dic_bilibili['appheaders'], data=data)
216216
return response

biliconsole.py

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from statistics import Statistics
33
from connect import connect
44
from printer import Printer
5+
import threading
56
import asyncio
67

78
def guide_of_console():
@@ -19,28 +20,27 @@ def guide_of_console():
1920
print(' ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄')
2021

2122

22-
async def preprocess_send_danmu_msg_andriod():
23+
def preprocess_send_danmu_msg_andriod():
2324
msg = input('请输入要发送的信息:')
2425
roomid = input('请输入要发送的房间号:')
25-
await utils.send_danmu_msg_andriod(msg, int(roomid))
26+
Biliconsole().append2list_console([[msg, int(roomid)], utils.send_danmu_msg_andriod])
2627

27-
async def preprocess_send_danmu_msg_web():
28+
def preprocess_send_danmu_msg_web():
2829
msg = input('请输入要发送的信息:')
2930
roomid = input('请输入要发送的房间号:')
30-
await utils.send_danmu_msg_web(msg, int(roomid))
31+
Biliconsole().append2list_console([[msg, int(roomid)], utils.send_danmu_msg_web])
3132

32-
async def preprocess_check_room():
33+
def preprocess_check_room():
3334
roomid = input('请输入要转化的房间号:')
34-
await utils.check_room(roomid)
35+
Biliconsole().append2list_console([[roomid], utils.check_room])
3536

3637
def process_send_gift_web():
37-
utils.fetch_bag_list(verbose=True)
38+
Biliconsole().append2list_console([[True], utils.fetch_bag_list])
3839
bagid = input('请输入要发送的礼物编号:')
39-
giftid = utils.fetch_bag_list(bagid=bagid)
4040
# print('是谁', giftid)
4141
giftnum = input('请输入要发送的礼物数目:')
4242
roomid = input('请输入要发送的房间号:')
43-
utils.send_gift_web(roomid, giftid, giftnum, bagid)
43+
Biliconsole().append2list_console([[roomid, [[False, bagid],utils.fetch_bag_list], giftnum, bagid],utils.send_gift_web])
4444

4545
def preprocess_change_danmuji_roomid():
4646
roomid = input('请输入roomid')
@@ -57,14 +57,14 @@ def change_printer_dic_user():
5757
options ={
5858
'1': Statistics().getlist,
5959
'2': Statistics().getresult,
60-
'3': utils.fetch_bag_list,
61-
'4': utils.fetch_medal,
62-
'5': utils.fetch_user_info,
63-
'6': utils.check_taskinfo,
64-
'7': preprocess_send_danmu_msg_andriod,
65-
'8': preprocess_send_danmu_msg_web,
66-
'9': preprocess_check_room,
67-
'10': process_send_gift_web,
60+
'3': utils.fetch_bag_list,#async
61+
'4': utils.fetch_medal,#async
62+
'5': utils.fetch_user_info,#async
63+
'6': utils.check_taskinfo,#async
64+
'7': preprocess_send_danmu_msg_andriod,#input async
65+
'8': preprocess_send_danmu_msg_web,#input async
66+
'9': preprocess_check_room,#input async
67+
'10': process_send_gift_web,#input async !!!
6868
'11': preprocess_change_danmuji_roomid,
6969
'12': change_printer_dic_user,
7070
'help': guide_of_console
@@ -73,25 +73,68 @@ def change_printer_dic_user():
7373
def return_error():
7474
print('命令无法识别,请重新输入')
7575

76-
async def controler():
76+
def controler():
7777
while True:
7878
x = input('')
79-
if x == '7':
79+
# input and async
80+
if x == ['7', '8', '9', '10']:
8081
# func = options.get(x, return_error)
81-
await preprocess_check_room()
82+
args, func = options.get(x, return_error)()
83+
#print(args)
84+
#Biliconsole().append2list_console(answer)
85+
# async
86+
elif x in ['3', '4', '5', '6']:
87+
answer = options.get(x, return_error)
88+
Biliconsole().append2list_console(answer)
89+
# normal
8290
else:
8391
options.get(x, return_error)()
84-
await asyncio.sleep(0.1)
8592

86-
def main():
87-
loop = asyncio.new_event_loop()
88-
tasks = [
89-
controler()
90-
]
91-
92-
loop.run_until_complete(asyncio.wait(tasks))
93-
#console_thread.join()
93+
class Biliconsole():
94+
instance = None
95+
96+
def __new__(cls, *args, **kw):
97+
if not cls.instance:
98+
cls.instance = super(Biliconsole, cls).__new__(cls, *args, **kw)
99+
cls.instance.list_console = []
100+
cls.lock = threading.Lock()
101+
return cls.instance
102+
103+
def append2list_console(self, request):
104+
self.lock.acquire()
105+
self.list_console.append(request)
106+
self.lock.release()
107+
108+
async def run(self):
109+
while True:
110+
len_list_console = len(self.list_console)
111+
tasklist = []
112+
for i in self.list_console:
113+
if isinstance(i, list):
114+
# 对10号单独简陋处理
115+
for j in range(len(i[0])):
116+
if isinstance(i[0][j], list):
117+
i[0][j] = await i[0][j][1](*(i[0][j][0]))
118+
task = asyncio.ensure_future(i[1](*i[0]))
119+
else:
120+
task = asyncio.ensure_future(i())
121+
tasklist.append(task)
122+
if tasklist:
123+
await asyncio.wait(tasklist, return_when=asyncio.ALL_COMPLETED)
124+
#print('本批次结束')
125+
else:
126+
#print('本批次轮空')
127+
pass
128+
self.lock.acquire()
129+
del self.list_console[:len_list_console]
130+
self.lock.release()
131+
if len_list_console == 0:
132+
await asyncio.sleep(1)
133+
else:
134+
await asyncio.sleep(0.3)
135+
136+
137+
94138

95-
loop.close()
96139

97140

rafflehandler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ async def run(self):
4545

4646
del self.list_activity[:len_list_activity]
4747
del self.list_TV[:len_list_TV]
48-
49-
await asyncio.sleep(5)
48+
if len_list_activity == 0 and len_list_TV == 0:
49+
await asyncio.sleep(5)
50+
else:
51+
await asyncio.sleep(1)
5052

5153

5254

run.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
Statistics()
2626

2727
rafflehandler = Rafflehandler()
28-
28+
biliconsole.Biliconsole()
2929

3030
task = OnlineHeart()
3131
task1 = Silver()
@@ -34,7 +34,7 @@
3434
task4 = connect()
3535

3636

37-
console_thread = threading.Thread(target=biliconsole.main)
37+
console_thread = threading.Thread(target=biliconsole.controler)
3838

3939
console_thread.start()
4040

@@ -50,7 +50,8 @@
5050
task4.connect(),
5151
task3.query(),
5252
printer.clean_printlist(),
53-
rafflehandler.run()
53+
rafflehandler.run(),
54+
biliconsole.Biliconsole().run()
5455

5556
]
5657

utils.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async def fetch_medal(printer=True):
6060
return roomid,today_feed,day_limit
6161
async def send_danmu_msg_andriod(msg, roomId):
6262
response = await bilibili().request_send_danmu_msg_andriod(msg, roomId)
63-
print('ggghhhjj')
63+
# print('ggghhhjj')
6464
print(await response.json())
6565

6666
async def send_danmu_msg_web(msg, roomId):
@@ -128,9 +128,9 @@ async def fetch_bag_list(verbose=False, bagid=None,printer=True):
128128
# print(temp)
129129
return temp,gift_list
130130

131-
def check_taskinfo():
132-
response = bilibili().request_check_taskinfo()
133-
json_response = response.json()
131+
async def check_taskinfo():
132+
response = await bilibili().request_check_taskinfo()
133+
json_response = await response.json()
134134
# print(json_response)
135135
if json_response['code'] == 0:
136136
data = json_response['data']
@@ -181,7 +181,7 @@ def check_taskinfo():
181181

182182
async def check_room(roomid):
183183
response = await bilibili().request_check_room(roomid)
184-
json_response = await response.json()
184+
json_response = await response.json(content_type=None)
185185
if json_response['code'] == 0:
186186
print('查询结果:')
187187
data = json_response['data']
@@ -206,15 +206,15 @@ async def send_gift_web(roomid, giftid, giftnum, bagid):
206206
print("# 错误", json_response1['msg'])
207207

208208
async def check_room_true(roomid):
209-
response = await bilibili().request_check_room(roomid)
210-
json_response = await response.json(content_type=None)
211-
212-
if json_response['code'] == 0:
213-
data = json_response['data']
214-
param1 = data['is_hidden']
215-
param2 = data['is_locked']
216-
param3 = data['encrypted']
217-
# print(param1, param2, param3)
218-
return param1, param2, param3
209+
response = await bilibili().request_check_room(roomid)
210+
json_response = await response.json(content_type=None)
211+
212+
if json_response['code'] == 0:
213+
data = json_response['data']
214+
param1 = data['is_hidden']
215+
param2 = data['is_locked']
216+
param3 = data['encrypted']
217+
# print(param1, param2, param3)
218+
return param1, param2, param3
219219

220220

0 commit comments

Comments
 (0)