-
Notifications
You must be signed in to change notification settings - Fork 141
/
console_cmd.py
290 lines (239 loc) · 11.7 KB
/
console_cmd.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
import asyncio
import re
import argparse
from cmd import Cmd
from distutils.util import strtobool
import bili_statistics
import printer
import notifier
from tasks.utils import UtilsTask
from tasks.bili_console import (
PrintGiftbagsTask,
PrintMedalsTask,
PrintMainBiliDailyJobTask,
PrintLiveBiliDailyJobTask,
PrintMainBiliUserInfoTask,
PrintLiveBiliUserInfoTask,
PrintJudgeTask,
PrintCapsuleTask,
OpenCapsuleTask,
SendDanmuTask,
PrintUserStatusTask
)
from tasks.custom import SendLatiaoTask, BuyLatiaoTask, BuyMedalTask
class FuncCore:
def __init__(self, function, *args):
self.function = function
self.args = args
async def exec(self):
args = list(self.args)
# 递归
for i, arg in enumerate(args):
if isinstance(arg, FuncCore):
args[i] = await arg.exec()
if asyncio.iscoroutinefunction(self.function):
return await self.function(*args)
return self.function(*args)
class ArgumentParserError(Exception):
pass
class ThrowingArgumentParser(argparse.ArgumentParser):
# https://github.com/python/cpython/blob/3.7/Lib/argparse.py
def exit(self, status=0, message=None):
raise ArgumentParserError(message)
def error(self, message):
raise ArgumentParserError(message)
class ConsoleCmd(Cmd):
PARSE_RE = re.compile(r'(--\w{2,})/(-[A-Za-z]) {(int|str|bool|room_id)(?:\?(\S*|%\S+))?}')
prompt = ''
def __init__(self, loop: asyncio.AbstractEventLoop, room_id, printer_danmu):
self.loop = loop
self.default_roomid = room_id
self._printer_danmu = printer_danmu
# || 用于分割,其中首个是标号,后面是全是一个个参数
# 参数格式是"--长指令/-短指令 {参数类型?默认值}",其中默认值可以省略
self._parser_1 = self.compile_parser('1 || --user_id/-u {int?0}')
self._parser_2 = self.compile_parser('2 || --user_id/-u {int?0}')
self._parser_3 = self.compile_parser('3 || --user_id/-u {int?0}')
self._parser_4 = self.compile_parser('4 || --user_id/-u {int?0}')
self._parser_5 = self.compile_parser('5 || --user_id/-u {int?0}')
self._parser_6 = self.compile_parser('6 || --user_id/-u {int?0}')
self._parser_7 = self.compile_parser('7 || --user_id/-u {int?0}')
self._parser_8 = self.compile_parser('8 || --user_id/-u {int?0}')
self._parser_9 = self.compile_parser('9 || --user_id/-u {int?0}')
self._parser_11 = self.compile_parser('11 || --user_id/-u {int?0}')
self._parser_12 = self.compile_parser('12 || --user_id/-u {int?0} || --num/-n {int}')
self._parser_13 = self.compile_parser('13 || --room_id/-p {room_id?%default_roomid}')
self._parser_14 = self.compile_parser('14 || --user_id/-u {int?0} || --msg/-m {str}'
' || --room_id/-p {room_id?%default_roomid}')
self._parser_15 = self.compile_parser('15 || --room_id/-p {room_id?%default_roomid}')
self._parser_16 = self.compile_parser('16 || --ctrl/-c {bool}')
self._parser_21 = self.compile_parser('21 || --room_id/-p {room_id?%default_roomid}'
' || --num/-n {int}')
self._parser_22 = self.compile_parser('22 || --room_id/-p {room_id?%default_roomid}'
' || --num/-n {int}')
self._parser_23 = self.compile_parser('23 || --user_id/-u {int?0} || --coin_type/-c {str}'
' || --room_id/-p {room_id?%default_roomid}')
super().__init__()
def compile_parser(self, text: str) -> ThrowingArgumentParser:
entries = [entry.strip() for entry in text.split('||')]
result = ThrowingArgumentParser(prog=entries[0], add_help=False)
for entry in entries[1:]:
long_ctrl, short_ctrl, str_value_type, default = self.PARSE_RE.fullmatch(entry).groups()
# print(f'{long_ctrl}, {short_ctrl}, {str_value_type}, {default}')
if default is None:
required = True
help_msg = f'(必填: 类型 {str_value_type})'
else:
required = False
help_msg = f'(可缺: 类型 {str_value_type} 默认 %(default)s)'
if str_value_type == 'int':
convert = self.str2int
elif str_value_type == 'bool':
convert = self.str2bool
elif str_value_type == 'room_id':
convert = self.str2room_id
else: # 其他的忽略,全部为str
convert = str
result.add_argument(long_ctrl, short_ctrl, required=required, help=help_msg, default=default, type=convert)
return result
@staticmethod
def parse(arg: str, parser: ThrowingArgumentParser):
try:
result = parser.parse_args(arg.split())
# print('parse_result', result, parser)
return tuple(vars(result).values())
except ArgumentParserError as e:
print('解析错误', e)
parser.print_help()
raise
@staticmethod
def str2int(orig: str) -> int:
return int(orig)
@staticmethod
def str2bool(orig: str) -> bool:
return bool(strtobool(orig))
def str2room_id(self, orig: str) -> FuncCore:
if orig == '%default_roomid':
return self.fetch_real_roomid(self.default_roomid)
return self.fetch_real_roomid(self.str2int(orig))
@staticmethod
def guide_of_console():
print(' __________________ ')
print('| 欢迎使用本控制台 |')
print('| 1 输出本次统计数据 |')
print('| 2 查看目前拥有礼物的统计 |')
print('| 3 查看持有勋章状态 |')
print('| 4 检查主站今日任务的情况 |')
print('| 5 检查直播分站今日任务的情况 |')
print('| 6 获取主站个人的基本信息 |')
print('| 7 获取直播分站个人的基本信息 |')
print('| 8 检查风纪委今日自动投票的情况 |')
print('| 9 检查脚本判断的用户小黑屋情况 |')
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()
def onecmd(self, *args, **kwargs):
try:
return super().onecmd(*args, **kwargs)
except ArgumentParserError:
# print('test_onecmd', args, kwargs)
pass
def postcmd(self, stop, line):
# print('test_post_cmd', stop, line)
if line == 'EOF':
return True
# 永远不退出
return None
def do_1(self, arg):
user_id, = self.parse(arg, self._parser_1)
self.exec_func_threads(
FuncCore(bili_statistics.print_statistics, user_id))
def do_2(self, arg):
user_id, = self.parse(arg, self._parser_2)
self.exec_func_threads(
FuncCore(notifier.exec_task, PrintGiftbagsTask, 'cmd', user_id))
def do_3(self, arg):
user_id, = self.parse(arg, self._parser_3)
self.exec_func_threads(
FuncCore(notifier.exec_task, PrintMedalsTask, 'cmd', user_id))
def do_4(self, arg):
user_id, = self.parse(arg, self._parser_4)
self.exec_func_threads(
FuncCore(notifier.exec_task, PrintMainBiliDailyJobTask, 'cmd', user_id))
def do_5(self, arg):
user_id, = self.parse(arg, self._parser_5)
self.exec_func_threads(
FuncCore(notifier.exec_task, PrintLiveBiliDailyJobTask, 'cmd', user_id))
def do_6(self, arg):
user_id, = self.parse(arg, self._parser_6)
self.exec_func_threads(
FuncCore(notifier.exec_task, PrintMainBiliUserInfoTask, 'cmd', user_id))
def do_7(self, arg):
user_id, = self.parse(arg, self._parser_7)
self.exec_func_threads(
FuncCore(notifier.exec_task, PrintLiveBiliUserInfoTask, 'cmd', user_id))
def do_8(self, arg):
user_id, = self.parse(arg, self._parser_8)
self.exec_func_threads(
FuncCore(notifier.exec_task, PrintJudgeTask, 'cmd', user_id))
def do_9(self, arg):
user_id, = self.parse(arg, self._parser_9)
self.exec_func_threads(
FuncCore(notifier.exec_task, PrintUserStatusTask, 'cmd', user_id))
def do_11(self, arg):
user_id, = self.parse(arg, self._parser_11)
self.exec_func_threads(
FuncCore(notifier.exec_task, PrintCapsuleTask, 'cmd', user_id))
def do_12(self, arg):
user_id, num_opened = self.parse(arg, self._parser_12)
self.exec_func_threads(
FuncCore(notifier.exec_task, OpenCapsuleTask, 'cmd', user_id, num_opened))
def do_13(self, arg):
real_roomid, = self.parse(arg, self._parser_13)
self.exec_func_threads(
FuncCore(notifier.exec_func, UtilsTask.get_real_roomid, real_roomid))
def do_14(self, arg):
user_id, msg, real_roomid = self.parse(arg, self._parser_14)
self.exec_func_threads(
FuncCore(notifier.exec_task, SendDanmuTask, 'cmd', user_id, msg, real_roomid))
def do_15(self, arg):
real_roomid, = self.parse(arg, self._parser_15)
self.default_roomid = real_roomid
self.exec_func_threads(
FuncCore(self._printer_danmu.reset_roomid, real_roomid))
def do_16(self, arg):
ctrl, = self.parse(arg, self._parser_16)
self.exec_func_threads(
FuncCore(printer.control_printer, ctrl))
def do_21(self, arg):
real_roomid, num_max = self.parse(arg, self._parser_21)
self.exec_func_threads(
FuncCore(notifier.exec_task, SendLatiaoTask, 'cmd', real_roomid, num_max))
def do_22(self, arg):
real_roomid, num_wanted = self.parse(arg, self._parser_22)
self.exec_func_threads(
FuncCore(notifier.exec_task, BuyLatiaoTask, 'cmd', real_roomid, num_wanted))
def do_23(self, arg):
user_id, coin_type, real_roomid = self.parse(arg, self._parser_23) # coin_type = 'silver' / 'metal'
self.exec_func_threads(
FuncCore(notifier.exec_task, BuyMedalTask, 'cmd', user_id, real_roomid, coin_type))
@staticmethod
def fetch_real_roomid(room_id):
return FuncCore(notifier.exec_func, UtilsTask.get_real_roomid, room_id)
# 直接执行,不需要user_id
def exec_func_threads(self, func_core: FuncCore):
asyncio.run_coroutine_threadsafe(self.exec_func(func_core), self.loop)
@staticmethod
async def exec_func(func_core: FuncCore):
await func_core.exec()