forked from TeamUltroid/UltroidAddons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
109 lines (96 loc) · 2.86 KB
/
test.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
# Ported From DarkCobra Originally By UNIBORG
#
# Ultroid - UserBot
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
"""
✘ Commands Available -
• `{i}test`
Test Ur Heroku Server Speed.
"""
from datetime import datetime
import speedtest
from . import *
@ultroid_cmd(pattern="test ?(.*)")
async def _(event):
input_str = event.pattern_match.group(1)
as_text = True
as_document = False
if input_str == "image":
as_document = False
elif input_str == "file":
as_document = True
elif input_str == "text":
as_text = True
xx = await event.eor("`Calculating ur Ultroid Server Speed. Please wait!`")
start = datetime.now()
s = speedtest.Speedtest()
s.get_best_server()
s.download()
s.upload() # dchehe
end = datetime.now()
ms = (end - start).seconds
response = s.results.dict()
download_speed = response.get("download")
upload_speed = response.get("upload")
ping_time = response.get("ping")
client_infos = response.get("client")
i_s_p = client_infos.get("isp")
i_s_p_rating = client_infos.get("isprating")
reply_msg_id = event.message.id
if event.reply_to_msg_id:
reply_msg_id = event.reply_to_msg_id
try: # heheh
response = s.results.share()
speedtest_image = response
if as_text:
await xx.edit(
"""`Ultroid Server Speed in {} sec`
`Download: {}`
`Upload: {}`
`Ping: {}`
`Internet Service Provider: {}`
`ISP Rating: {}`""".format(
ms,
convert_from_bytes(download_speed),
convert_from_bytes(upload_speed),
ping_time,
i_s_p,
i_s_p_rating,
)
)
else:
await event.client.send_file(
event.chat_id,
speedtest_image, # heeehe
caption="**SpeedTest** completed in {} seconds".format(ms),
force_document=as_document,
reply_to=reply_msg_id,
allow_cache=False,
)
await event.delete()
except Exception as exc: # dc
await xx.edit(
"""**SpeedTest** completed in {} seconds
Download: {}
Upload: {}
Ping: {}
__With the Following ERRORs__
{}""".format(
ms,
convert_from_bytes(download_speed),
convert_from_bytes(upload_speed),
ping_time,
str(exc),
)
)
def convert_from_bytes(size):
power = 2 ** 10
n = 0
units = {0: "", 1: "kilobytes", 2: "megabytes", 3: "gigabytes", 4: "terabytes"}
while size > power:
size /= power
n += 1
return f"{round(size, 2)} {units[n]}"