-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathratemod.py
executable file
·271 lines (244 loc) · 9.54 KB
/
ratemod.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
# █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
# █▀█ █ █ █ █▀█ █▀▄ █
# © Copyright 2022
# https://t.me/hikariatama
#
# 🔒 Licensed under the GNU AGPLv3
# 🌐 https://www.gnu.org/licenses/agpl-3.0.html
# scope: hikka_min 1.2.10
# meta pic: https://img.icons8.com/external-phatplus-lineal-color-phatplus/512/000000/external-rate-email-phatplus-lineal-color-phatplus.png
# meta banner: https://mods.hikariatama.ru/badges/ratemod.jpg
# meta developer: @hikarimods
import asyncio
import hashlib
import re
import requests
from telethon.tl.types import Message
from .. import loader, utils
@loader.tds
class RateModuleMod(loader.Module):
"""Rates module and suggests fixes"""
strings = {
"name": "RateMod",
"template": (
"👮♂️ <b>Mode rating </b><code>{}</code><b>:</b>\n{} {} <b>[{}]</b>\n\n{}"
),
"no_file": "<b>What should I check?... 🗿</b>",
"cannot_check_file": "<b>Check error</b>",
}
strings_ru = {
"template": (
"👮♂️ <b>Оценка модуля </b><code>{}</code><b>:</b>\n{} {} <b>[{}]</b>\n\n{}"
),
"no_file": "<b>А что проверять то?... 🗿</b>",
"cannot_check_file": "<b>Ошибка проверки</b>",
"_cmd_doc_ratemod": "<код> - Оценить модуль",
"_cls_doc": "Оценивает модуль и дает рекомендации",
}
@loader.unrestricted
async def ratemodcmd(self, message: Message):
"""<reply_to_file|file|link> - Rate code"""
args = utils.get_args_raw(message)
reply = await message.get_reply_message()
if (
not reply
and not getattr(reply, "media", None)
and not getattr(message, "media", None)
and not args
and not utils.check_url(args)
):
return await utils.answer(message, self.strings("no_file"))
checking = (
getattr(reply, "media", None)
if getattr(reply, "media", None) is not None
else (
getattr(message, "media", None)
if getattr(message, "media", None) is not None
else (args if args and utils.check_url(args) else 0)
)
)
if type(checking) is int:
return await utils.answer(message, self.strings("no_file"))
if type(checking) is not str:
try:
file = await self._client.download_file(
(
getattr(reply, "media", None)
if getattr(reply, "media", None) is not None
else getattr(message, "media", None)
),
bytes,
)
except Exception:
return await utils.answer(
message,
self.strings("cannot_check_file"),
)
try:
code = file.decode("utf-8").replace("\r\n", "\n")
except Exception:
return await utils.answer(
message,
self.strings("cannot_check_file"),
)
else:
try:
code = (await utils.run_sync(requests.get, args)).text
except Exception:
return await utils.answer(message, self.strings("cannot_check_file"))
try:
mod_name = re.search(
r"""strings[ ]*=[ ]*{.*?name['"]:[ ]*['"](.*?)['"]""", code, flags=re.S
).group(1)
except Exception:
mod_name = "Unknown"
import_regex = [
r"^[^#]rom ([^\n\r]*) import [^\n\r]*$",
r"^[^#]mport ([^\n\r]*)[^\n\r]*$",
r"""__import__[(]['"]([^'"]*)['"][)]""",
]
imports = [
re.findall(import_re, code, flags=re.M | re.DOTALL)
for import_re in import_regex
]
if ".." in imports:
del imports[imports.index("..")]
splitted = [
_
for _ in list(
zip(
list(
map(
lambda x: len(re.findall(r"[ \t]+(if|elif|else).+:", x)),
re.split(r"[ \t]*async def .*?cmd\(", code),
)
),
[""] + re.findall(r"[ \t]*async def (.*?)cmd\(", code),
)
)
if _[0] > 10
]
comments = ""
score = 4.6
if len(imports) > 10:
comments += (
f"🔻 <code>{{-0.1}}</code> <b>A lot of imports ({len(imports)})"
" </b><i>[memory]</i>\n"
)
score -= 0.1
if "requests" in imports and "utils.run_sync" not in code:
comments += (
"🔻 <code>{-0.5}</code> <b>Sync requests</b> <i>[blocks runtime]</i>\n"
)
score -= 0.5
if "while True" in code or "while 1" in code:
comments += (
"🔻 <code>{-0.1}</code> <b>While true</b> <i>[block runtime*]</i>\n"
)
score -= 0.1
if ".edit(" in code:
comments += (
"🔻 <code>{-0.3}</code> <b>Classic message.edit</b> <i>[no twink"
" support]</i>\n"
)
score -= 0.3
if re.search(r"@.*?[bB][oO][tT]", code) is not None:
bots = " | ".join(re.findall(r"@.*?[bB][oO][tT]", code))
comments += (
"🔻 <code>{-0.2}</code> <b>Bot-abuse"
f" (</b><code>{bots}</code><b>)</b>"
" <i>[module will die some day]</i>\n"
)
score -= 0.2
if re.search(r'[ \t]+async def .*?cmd.*\n[ \t]+[^\'" \t]', code) is not None:
undoc = " | ".join(
list(re.findall(r'[ \t]+async def (.*?)cmd.*\n[ \t]+[^" \t]', code))
)
comments += (
f"🔻 <code>{{-0.4}}</code> <b>No docs (</b><code>{undoc}</code><b>)</b>"
" <i>[all commands must be documented]</i>\n"
)
score -= 0.4
if "time.sleep" in code or "from time import sleep" in code:
comments += (
"🔻 <code>{-2.0}</code> <b>Sync sleep (</b><code>time.sleep</code><b>)"
" replace with (</b><code>await asyncio.sleep</code><b>)</b> <i>[blocks"
" runtime]</i>\n"
)
score -= 2
if [_ for _ in code.split("\n") if len(_) > 300]:
ll = max(len(_) for _ in code.split("\n") if len(_) > 300)
comments += (
f"🔻 <code>{{-0.1}}</code> <b>Long lines ({ll})</b> <i>[PEP"
" violation]</i>\n"
)
score -= 0.1
if re.search(r'[\'"] ?\+ ?.*? ?\+ ?[\'"]', code) is not None:
comments += (
"🔻 <code>{-0.1}</code> <b>Avoiding f-строк</b> <i>[causes"
" problems]</i>\n"
)
score -= 0.1
if splitted:
comments += (
"🔻 <code>{-0.2}</code> <b>Big 'if' trees"
f" (</b><code>{' | '.join([f'{chain} в {fun}' for chain, fun in splitted])}</code><b>)</b>"
" <i>[readability]</i>\n"
)
score -= 0.2
if "== None" in code or "==None" in code:
comments += (
"🔻 <code>{-0.3}</code> <b>Type comparsation via ==</b> <i>[google"
" it]</i>\n"
)
score -= 0.3
if "is not None else" in code:
comments += (
"🔻 <code>{-0.1}</code> <b>Unneccessary ternary operator usage"
" (</b><code>if some_var is not None else another</code> <b>-></b>"
" <code>some_var or another</code><b>)</b> <i>[readability]</i>\n"
)
score -= 0.1
if "utils.answer" in code and ".edit(" not in code:
comments += (
"🔸 <code>{+0.3}</code> <b>utils.answer</b> <i>[twinks support]</i>\n"
)
score += 0.3
if re.search(r'[ \t]+async def .*?cmd.*\n[ \t]+[^\'" \t]', code) is None:
comments += (
"🔸 <code>{+0.3}</code> <b>Docs</b> <i>[all commands are"
" documented]</i>\n"
)
score += 0.3
if "requests" in imports and "utils.run_sync" in code or "aiohttp" in imports:
comments += (
"🔸 <code>{+0.3}</code> <b>Async requests</b> <i>[do not stop"
" runtime]</i>\n"
)
score += 0.3
api_endpoint = "https://mods.hikariatama.ru/check?hash="
sha1 = hashlib.sha1()
sha1.update(code.encode("utf-8"))
try:
check_res = (
await utils.run_sync(requests.get, api_endpoint + str(sha1.hexdigest()))
).text
except Exception:
check_res = ""
if check_res in {"yes", "db"}:
comments += (
"🔸 <code>{+1.0}</code> <b>Module is verified</b> <i>[no scam]</i>\n"
)
score += 1.0
score = round(score, 1)
score = min(score, 5.0)
await utils.answer(
message,
self.strings("template").format(
mod_name,
"⭐️" * round(score),
score,
["Shit", "Bad", "Poor", "Normal", "Ok", "Good"][round(score)],
comments,
),
)