-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder.py
executable file
·260 lines (243 loc) · 7.61 KB
/
order.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
from orders.canny import Canny
from orders.caption import Caption
from orders.clear import Clear
from orders.color import Color
from orders.detect import Detect
from orders.eightball import EightBall
from orders.info import Info
from orders.kanji import Kanji
from orders.malus import Malus
from orders.manageuser import ManageUser
from orders.ocr import OCR
from orders.oled import OLED
from orders.paint import Paint
from orders.palette import Palette
from orders.pixel import Pixel
from orders.relief import Relief
from orders.replace import Replace
from orders.reverse import Reverse
from orders.roll import Roll
from orders.scale import Scale
from orders.solve import Solve
from orders.stats import Stats
from orders.strip import Strip
from orders.tts import TTS
from orders.urban import Urban
from orders.weather import Weather
from orders.xkcd import Xkcd
from threading import Thread
from time import time
class Order:
def __init__(self, msg, wd, keys, exec_paths, deep_nn, classifiers, rules, kanji_list, font_params):
self.msg = msg
self.user = msg.from_user
self.order = msg.text.split(' ', 1)[0].lower()
self.res = None
# Other params retrieval
self.wd = wd
self.superadmins, self.admins, self.key, self.owm = keys
self.exec_paths = exec_paths
self.deep_nn = deep_nn
self.classifiers = classifiers
self.rules = rules
self.kanji_list = kanji_list
self.font_params = font_params
# Orders saved in usage.csv
#
# 'help' excluded because basic command
# 'info' excluded because command used by developers (or superadmins) only
# 'manuser' excluded because command used by developers (or superadmins) only
# 'stats' excluded because I don't want stats about stats
self.orders = [
'8ball',
'canny',
'clr',
'color',
'cpt',
'detect',
'kanji',
'malus',
'ocr',
'oled',
'paint',
'palette',
'pixel',
'relief',
'rgx',
'rev',
'roll',
'scale',
'solve',
'strip',
'tts',
'urb',
'wtr',
'xkcd'
]
# Clean text before saving in usage.csv
@staticmethod
def clean_text(text) -> str:
text = text.replace(';', '[PV]')
text = text.replace('\n', '[NL]')
return text
# Remember usage activity
def usage(self, res) -> None:
with open('{}/orders/utility/usage.csv'.format(self.wd), 'a') as f:
data = '{};{};{};{};{};{}\n'.format(
self.order,
self.user.id,
time(),
res['status'],
self.clean_text(self.msg.text),
self.clean_text(res['err'])
)
f.write(data)
return
def get_answer(self) -> None:
res = None
# Determine if the user is a superadmin
superadmin = self.user.id in self.superadmins
# Execute commands only for admins and superadmins
if self.user.id not in self.admins and not superadmin:
self.res = res
return
# Mapping every order with its arguments
orders_map = {
'8ball': {
'name': EightBall,
'args': [self.msg]
},
'canny': {
'name': Canny,
'args': [self.msg, self.key]
},
'clr': {
'name': Clear,
'args': [self.msg, self.rules]
},
'color': {
'name': Color,
'args': [self.msg]
},
'cpt': {
'name': Caption,
'args': [self.msg, self.key, self.font_params]
},
'detect': {
'name': Detect,
'args': [self.msg, self.key, self.deep_nn, self.classifiers]
},
'info': {
'name': Info,
'args': [self.msg]
},
'kanji': {
'name': Kanji,
'args': [self.msg, self.wd, self.kanji_list]
},
'malus': {
'name': Malus,
'args': [self.msg, self.wd]
},
'manuser': {
'name': ManageUser,
'args': [self.msg, self.wd, superadmin]
},
'ocr': {
'name': OCR,
'args': [self.msg, self.key]
},
'oled': {
'name': OLED,
'args': [self.msg, self.key]
},
'paint': {
'name': Paint,
'args': [self.msg, self.wd, self.key, self.exec_paths['mogrify']]
},
'palette': {
'name': Palette,
'args': [self.msg, self.key]
},
'pixel': {
'name': Pixel,
'args': [self.msg, self.wd, self.key, self.exec_paths['mogrify']]
},
'relief': {
'name': Relief,
'args': [self.msg, self.key]
},
'rgx': {
'name': Replace,
'args': [self.msg]
},
'rev': {
'name': Reverse,
'args': [self.msg, self.wd, self.key, self.exec_paths['ffmpeg']]
},
'roll': {
'name': Roll,
'args': [self.msg]
},
'scale': {
'name': Scale,
'args': [self.msg, self.wd, self.key, self.exec_paths['mogrify']]
},
'solve': {
'name': Solve,
'args': [self.msg, self.exec_paths['qalc']]
},
'stats': {
'name': Stats,
'args': [self.msg, self.wd]
},
'strip': {
'name': Strip,
'args': [self.msg, self.wd, self.key, (
self.exec_paths['exiftool'],
self.exec_paths['ffmpeg'],
self.exec_paths['gs'],
self.exec_paths['jpegoptim'],
self.exec_paths['optipng'],
)]
},
'tts': {
'name': TTS,
'args': [self.msg]
},
'urb': {
'name': Urban,
'args': [self.msg]
},
'wtr': {
'name': Weather,
'args': [self.msg, self.owm]
},
'xkcd': {
'name': Xkcd,
'args': [self.msg]
}
}
# Executing the order
if self.order in orders_map:
res = orders_map[self.order]['name'](*orders_map[self.order]['args']).get()
self.res = res
# 300s is the maximum response time
def run_order(self) -> dict:
t = Thread(target=self.get_answer)
t.start()
t.join(300)
if t.is_alive():
self.res = {
'type': 'text',
'send': 'I\'m sorry, but I\'m consuming too many resources and time for this command...',
'caption': None,
'filename': None,
'msg_id': self.msg.message_id,
'status': False,
'err': 'TIMEOUT'
}
# Usage statistics if necessary
if self.order in self.orders and self.res is not None:
self.usage(self.res)
return self.res