-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeedtest.py
More file actions
161 lines (147 loc) · 6.61 KB
/
Copy pathspeedtest.py
File metadata and controls
161 lines (147 loc) · 6.61 KB
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
# ______ ___ ___ _ _
# ____ | ___ \ | \/ | | | | |
# / __ \| |_/ / _| . . | ___ __| |_ _| | ___
# / / _` | __/ | | | |\/| |/ _ \ / _` | | | | |/ _ \
# | | (_| | | | |_| | | | | (_) | (_| | |_| | | __/
# \ \__,_\_| \__, \_| |_/\___/ \__,_|\__,_|_|\___|
# \____/ __/ |
# |___/
# На модуль распространяется лицензия "GNU General Public License v3.0"
# https://github.com/all-licenses/GNU-General-Public-License-v3.0
# meta developer: @pymodule
# meta fhsdesc: tool, tools, test, speedtest
# requires: speedtest-cli
import speedtest
from .. import loader, utils
@loader.tds
class SpeedTestMod(loader.Module):
"""Checking your internet speed"""
strings = {
"name": "SpeedTest",
"starting": "Running Speedtest…",
"ping": "Ping: <i>{:.2f} ms</i>",
"download": "Download: <i>{:.2f} Mbps</i>",
"upload": "Upload: <i>{:.2f} Mbps</i>",
"finished": "<b>Speedtest completed!</b>",
"error": "Speedtest error: <code>{}</code>",
"progress_ping": "Testing \"Ping\"...",
"progress_download": "Testing \"Download\"...",
"progress_upload": "Testing \"Upload\"...",
"cfg_timeout": "Server request timeout (sec)",
"cfg_retries": "Number of retry attempts",
"quality_website": "Websites: {}",
"quality_video": "Video: {}",
"quality_gaming": "Gaming: {}",
"quality_calls": "Video calls: {}",
}
strings_ru = {
"_cls_doc": "Проверка скорости интернета",
"starting": "Запускаем Speedtest…",
"ping": "Ping: <i>{:.2f} мс</i>",
"download": "Загрузка: <i>{:.2f} Мбит/с</i>",
"upload": "Отдача: <i>{:.2f} Мбит/с</i>",
"finished": "<b>Speedtest завершён!</b>",
"error": "Ошибка при выполнении Speedtest: <code>{}</code>",
"progress_ping": "Тестируем пинг...",
"progress_download": "Тестируем скачивание...",
"progress_upload": "Тестируем загрузку...",
"cfg_timeout": "Таймаут запросов к серверу (сек)",
"cfg_retries": "Кол‑во попыток при неудаче",
"quality_website": "Сайты: {}",
"quality_video": "Видео: {}",
"quality_gaming": "Игры: {}",
"quality_calls": "Видеосвязь: {}",
}
def __init__(self):
self.config = loader.ModuleConfig(
loader.ConfigValue(
"timeout",
30,
lambda: self.strings("cfg_timeout"),
validator=loader.validators.Integer(minimum=10, maximum=120),
),
loader.ConfigValue(
"retries",
2,
lambda: self.strings("cfg_retries"),
validator=loader.validators.Integer(minimum=0, maximum=5),
),
)
def _get_quality_rating(self, category: str, ping: float, download: float, upload: float) -> str:
if category == "website":
if ping < 50 and download > 5:
return "🟢🟢🟢🟢🟢"
elif ping < 100 and download > 3:
return "🟠🟠🟠🟠"
elif ping < 200 and download > 1:
return "🟡🟡🟡"
elif ping < 300 and download > 0.5:
return "🔴🔴"
else:
return "⚫"
elif category == "video":
if ping < 50 and download > 25:
return "🟢🟢🟢🟢🟢"
elif ping < 75 and download > 5:
return "🟠🟠🟠🟠"
elif ping < 100 and download > 3:
return "🟡🟡🟡"
elif ping < 150 and download > 1:
return "🔴🔴"
else:
return "⚫"
elif category == "gaming":
if ping < 50 and download > 5 and upload > 3:
return "🟢🟢🟢🟢🟢"
elif ping < 100 and download > 3 and upload > 1:
return "🟠🟠🟠🟠"
elif ping < 150 and download > 1 and upload > 0.5:
return "🟡🟡🟡"
elif ping < 200 and download > 0.5:
return "🔴🔴"
else:
return "⚫"
elif category == "calls":
if ping < 50 and download > 4 and upload > 4:
return "🟢🟢🟢🟢🟢"
elif ping < 100 and download > 1.5 and upload > 1.5:
return "🟡🟡🟡🟡"
elif ping < 150 and download > 1 and upload > 1:
return "🟠🟠🟠"
elif ping < 200 and download > 0.5:
return "🔴🔴"
else:
return "⚫"
return "⚫"
@loader.command(
ru_doc="(.st) - Запускает тест скорости интернета",
en_doc="(.st) - Runs an internet speed test",
alias="st",
)
async def speedcmd(self, message):
msg = await utils.answer(message, self.strings("starting"))
try:
s = speedtest.Speedtest()
s.get_best_server()
await utils.answer(msg, self.strings("progress_ping"))
ping = s.results.ping
await utils.answer(msg, self.strings("progress_download"))
download = s.download() / 1_000_000
await utils.answer(msg, self.strings("progress_upload"))
upload = s.upload() / 1_000_000
text = (
f"{self.strings('finished')}\n\n"
f"{self.strings('ping').format(ping)}\n"
f"{self.strings('download').format(download)}\n"
f"{self.strings('upload').format(upload)}\n\n"
f"{self.strings('quality_website').format(self._get_quality_rating('website', ping, download, upload))}\n"
f"{self.strings('quality_video').format(self._get_quality_rating('video', ping, download, upload))}\n"
f"{self.strings('quality_gaming').format(self._get_quality_rating('gaming', ping, download, upload))}\n"
f"{self.strings('quality_calls').format(self._get_quality_rating('calls', ping, download, upload))}"
)
await utils.answer(msg, text)
except Exception as exc:
await utils.answer(
msg,
self.strings("error").format(utils.escape_html(str(exc))),
)