forked from yjqiang/bili2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.py
226 lines (182 loc) · 6.65 KB
/
printer.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
import sys
import time
from typing import Optional
from collections import defaultdict
if sys.platform == 'ios':
import console
class BiliLogger:
__slots__ = ()
# 格式化数据
@staticmethod
def format(
*objects,
extra_info: Optional[str] = None,
need_timestamp: bool = True):
timestamp = time.strftime("[%Y-%m-%d %H:%M:%S]", time.localtime())\
if need_timestamp else '>'
extra_info = f' ({extra_info})' if extra_info is not None else ''
if objects:
first_value, *others = objects
others = [f'# {i}' for i in others]
return (f'{timestamp} {first_value}{extra_info}', *others)
return f'{timestamp} NULL{extra_info}',
def info(
self,
*objects,
extra_info: Optional[str] = None,
need_timestamp: bool = True):
texts = self.format(
*objects,
extra_info=extra_info,
need_timestamp=need_timestamp)
for i in texts:
print(i)
def warn(
self,
*objects,
extra_info: Optional[str] = None):
texts = self.format(
*objects,
extra_info=extra_info,
need_timestamp=True)
for i in texts:
print(i, file=sys.stderr)
with open('bili.log', 'a', encoding='utf-8') as f:
for i in texts:
f.write(f'{i}\n')
def debug(
self,
*objects,
**kwargs):
self.warn(*objects, **kwargs)
def error(
self,
*objects,
**kwargs):
self.warn(*objects, **kwargs)
sys.exit(-1)
class PythonistaPrinter(BiliLogger):
__slots__ = ('dic_color', 'danmu_control',)
def __init__(self):
self.dic_color = {
'user-level': defaultdict(list),
'fans-level': defaultdict(list),
'others': defaultdict(list)
}
self.danmu_control = False
def init_config(self, dic_color, print_control_danmu):
self.dic_color = dic_color
self.danmu_control = print_control_danmu
def control_printer(self, danmu_control=None):
if danmu_control is not None:
self.danmu_control = danmu_control
# "#969696"
@staticmethod
def hex_to_rgb_percent(hex_str):
return tuple(
int(n, 16)/255 for n in (hex_str[1:3], hex_str[3:5], hex_str[5:7]))
# 弹幕 礼物 。。。。type
def print_danmu(self, danmu_msg: dict):
if not self.danmu_control:
return
danmu_msg_info = danmu_msg['info']
list_msg = []
list_color = []
if danmu_msg_info[7] == 3:
# print('舰', end=' ')
list_msg.append('⚓️ ')
list_color.append([])
else:
if danmu_msg_info[2][3] == 1:
if danmu_msg_info[2][4] == 0:
list_msg.append('爷 ')
list_color.append(self.dic_color['others']['vip'])
else:
list_msg.append('爷 ')
list_color.append(self.dic_color['others']['svip'])
if danmu_msg_info[2][2] == 1:
list_msg.append('房管 ')
list_color.append(self.dic_color['others']['admin'])
# 勋章
if danmu_msg_info[3]:
list_color.append(self.dic_color['fans-level'][f'fl{danmu_msg_info[3][0]}'])
list_msg.append(f'{danmu_msg_info[3][1]}|{danmu_msg_info[3][0]} ')
# 等级
if not danmu_msg_info[5]:
list_color.append(self.dic_color['user-level'][f'ul{danmu_msg_info[4][0]}'])
list_msg.append(f'UL{danmu_msg_info[4][0]} ')
list_msg.append(danmu_msg_info[2][1] + ':')
try:
if danmu_msg_info[2][7]:
list_color.append(self.hex_to_rgb_percent(danmu_msg_info[2][7]))
else:
list_color.append(self.dic_color['others']['default_name'])
except IndexError:
print("# 小电视降临本直播间")
list_color.append(self.dic_color['others']['default_name'])
list_msg.append(danmu_msg_info[1])
list_color.append([])
for i, j in zip(list_msg, list_color):
console.set_color(*j)
print(i, end='')
print()
console.set_color()
class NormalPrinter(BiliLogger):
__slots__ = ('danmu_control',)
def __init__(self):
self.danmu_control = False
def init_config(self, _, print_control_danmu):
self.danmu_control = print_control_danmu
def control_printer(self, danmu_control=None):
if danmu_control is not None:
self.danmu_control = danmu_control
def print_danmu(self, danmu_msg: dict):
if not self.danmu_control:
return
danmu_msg_info = danmu_msg['info']
list_msg = []
if danmu_msg_info[7] == 3:
# print('舰', end=' ')
list_msg.append('⚓️ ')
else:
if danmu_msg_info[2][3] == 1:
if danmu_msg_info[2][4] == 0:
list_msg.append('爷 ')
else:
list_msg.append('爷 ')
if danmu_msg_info[2][2] == 1:
list_msg.append('房管 ')
# 勋章
if danmu_msg_info[3]:
list_msg.append(f'{danmu_msg_info[3][1]}|{danmu_msg_info[3][0]} ')
# 等级
if not danmu_msg_info[5]:
list_msg.append(f'UL{danmu_msg_info[4][0]} ')
list_msg.append(danmu_msg_info[2][1] + ':')
try:
if danmu_msg_info[2][7]:
pass
else:
pass
except IndexError:
print("# 小电视降临本直播间")
list_msg.append(danmu_msg_info[1])
print(''.join(list_msg))
if sys.platform == 'ios':
printer = PythonistaPrinter()
else:
printer = NormalPrinter()
def init_config(dic_color, print_control_danmu):
printer.init_config(dic_color, print_control_danmu)
def print_danmu(danmu_msg: dict):
printer.print_danmu(danmu_msg)
def control_printer(danmu_control=None, _=None):
printer.control_printer(danmu_control)
def info(*objects, **kwargs):
printer.info(*objects, **kwargs)
def warn(*objects, **kwargs):
printer.warn(*objects, **kwargs)
def error(*objects, **kwargs):
printer.error(*objects, **kwargs)
def debug(*objects, **kwargs):
printer.debug(*objects, **kwargs)