forked from rabbit2rabbit/bili2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bili_console.py
195 lines (156 loc) · 7.64 KB
/
bili_console.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
import bili_statistics
import printer
import asyncio
import notifier
from cmd import Cmd
import getopt
from tasks.utils import UtilsTask
from tasks.custom import SendLatiaoTask, BuyLatiaoTask, BuyMedalTask
from tasks.main_daily_job import JudgeCaseTask
class Biliconsole(Cmd):
prompt = ''
def __init__(self, loop, room_id, printer_danmu):
self.loop = loop
self.default_roomid = room_id
self._printer_danmu = printer_danmu
super().__init__()
def guide_of_console(self):
print(' __________________ ')
print('| 欢迎使用本控制台 |')
print('| 1 输出本次统计数据 |')
print('| 2 查看目前拥有礼物的统计 |')
print('| 3 查看持有勋章状态 |')
print('| 4 检查主站今日任务的情况 |')
print('| 5 检查直播分站今日任务的情况 |')
print('| 6 获取主站个人的基本信息 |')
print('| 7 获取直播分站个人的基本信息 |')
print('| 8 检查风纪委今日自动投票的情况 |')
print('|11 当前拥有的扭蛋币 |')
print('|12 开扭蛋币(一、十、百) |')
print('|13 直播间的长短号码的转化 |')
print('|14 发送弹幕 |')
print('|15 切换监听的直播间 |')
print('|16 控制弹幕的开关 |')
print('|21 赠指定总数的辣条到房间 |')
print('|22 银瓜子全部购买辣条并送到房间 |')
print('|23 购买勋章(使用银瓜子或者硬币)|')
print('  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ')
def default(self, line):
self.guide_of_console()
def emptyline(self):
self.guide_of_console()
# pattern = '-u:-p:' u(user_id):0,1…;n(num);p(point)指roomid(烂命名因为-r不合适)
def parse(self, arg, pattern, default_u=0, set_roomid=False):
args = arg.split()
try:
opts, args = getopt.getopt(args, pattern)
except getopt.GetoptError:
return []
dict_results = {opt_name: opt_value for opt_name, opt_value in opts}
opt_names = pattern.split(':')[:-1]
results = []
for opt_name in opt_names:
opt_value = dict_results.get(opt_name)
if opt_name == '-u':
if opt_value is not None and opt_value.isdigit():
results.append(int(opt_value))
else:
results.append(default_u)
# -2是一个灾难性的东西
# results.append(-2)
elif opt_name == '-n':
if opt_value is not None and opt_value.isdigit():
results.append(int(opt_value))
else:
results.append(0)
elif opt_name == '-p':
if opt_value is not None and opt_value.isdigit():
room_id = int(opt_value)
else:
room_id = self.default_roomid
if set_roomid:
self.default_roomid = room_id
results.append(self.fetch_real_roomid(room_id))
else:
results.append(opt_value)
return results
def do_1(self, arg):
id, = self.parse(arg, '-u:')
self.exec_func_threads(bili_statistics.coroutine_print_statistics, [id])
def do_2(self, arg):
id, = self.parse(arg, '-u:')
self.exec_notifier_func_threads(id, UtilsTask.print_giftbags, [])
def do_3(self, arg):
id, = self.parse(arg, '-u:')
self.exec_notifier_func_threads(id, UtilsTask.print_medals, [])
def do_4(self, arg):
id, = self.parse(arg, '-u:')
self.exec_notifier_func_threads(id, UtilsTask.print_bilimain_tasks, [])
def do_5(self, arg):
id, = self.parse(arg, '-u:')
self.exec_notifier_func_threads(id, UtilsTask.print_livebili_tasks, [])
def do_6(self, arg):
id, = self.parse(arg, '-u:')
self.exec_notifier_func_threads(id, UtilsTask.print_mainbili_userinfo, [])
def do_7(self, arg):
id, = self.parse(arg, '-u:')
self.exec_notifier_func_threads(id, UtilsTask.print_livebili_userinfo, [])
def do_8(self, arg):
id, = self.parse(arg, '-u:')
self.exec_notifier_func_threads(id, JudgeCaseTask.print_judge_tasks, [])
def do_11(self, arg):
id, = self.parse(arg, '-u:')
self.exec_notifier_func_threads(id, UtilsTask.print_capsule_info, [])
def do_12(self, arg):
id, num_opened = self.parse(arg, '-u:-n:')
self.exec_notifier_func_threads(id, UtilsTask.open_capsule, [num_opened])
def do_13(self, arg):
real_roomid, = self.parse(arg, '-p:')
self.exec_notifier_func_threads(-1, UtilsTask.get_real_roomid, [real_roomid])
def do_14(self, arg):
id, msg, real_roomid = self.parse(arg, '-u:-m:-p:')
self.exec_notifier_func_threads(id, UtilsTask.send_danmu, [msg, real_roomid])
def do_15(self, arg):
real_roomid, = self.parse(arg, '-p:', set_roomid=True)
self.exec_func_threads(self._printer_danmu.reset_roomid, [real_roomid])
def do_16(self, arg):
ctrl, = self.parse(arg, '-c:')
if ctrl == 'T':
printer.control_printer(True)
else:
printer.control_printer(False)
def do_21(self, arg):
real_roomid, num_max = self.parse(arg, '-p:-n:')
self.exec_task_threads(0, SendLatiaoTask, 0, [real_roomid, num_max])
def do_22(self, arg):
id, real_roomid = self.parse(arg, '-u:-p:')
self.exec_notifier_func_threads(id, BuyLatiaoTask.clean_latiao, [real_roomid])
def do_23(self, arg):
id, coin_type, real_roomid = self.parse(arg, '-u:-c:-p:')
self.exec_notifier_func_threads(id, BuyMedalTask.buy_medal, [real_roomid, coin_type])
def fetch_real_roomid(self, room_id):
real_roomid = [-1, UtilsTask.get_real_roomid, room_id]
return real_roomid
# threads指thread safe
def exec_notifier_func_threads(self, *args):
asyncio.run_coroutine_threadsafe(self.exec_notifier_func(*args), self.loop)
def exec_func_threads(self, *args):
asyncio.run_coroutine_threadsafe(self.exec_func(*args), self.loop)
def exec_task_threads(self, *args):
asyncio.run_coroutine_threadsafe(self.exec_task(*args), self.loop)
# 这里args设置为list
async def exec_notifier_func(self, id, func, args):
for i, arg in enumerate(args):
if isinstance(arg, list):
args[i] = await notifier.exec_func(*arg)
await notifier.exec_func(id, func, *args)
async def exec_func(self, func, args):
for i, arg in enumerate(args):
if isinstance(arg, list):
args[i] = await notifier.exec_func(*arg)
await func(*args)
async def exec_task(self, id, task, step, args):
for i, arg in enumerate(args):
if isinstance(arg, list):
args[i] = await notifier.exec_func(*arg)
notifier.exec_task(id, task, step, *args)