video-stream/program/speedtest.py

78 lines
2.4 KiB
Python
Raw Permalink Normal View History

2022-02-22 23:37:00 +00:00
"""
Video + Music Stream Telegram Bot
Copyright (c) 2022-present levina=lab <https://github.com/levina-lab>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but without any warranty; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/licenses.html>
"""
2022-01-31 12:41:47 +00:00
import wget
import speedtest
from PIL import Image
2022-02-22 23:37:00 +00:00
from config import BOT_USERNAME as bname
2022-02-21 08:57:22 +00:00
from driver.filters import command
2022-01-31 12:41:47 +00:00
from driver.decorators import sudo_users_only
2022-02-07 16:18:16 +00:00
from driver.core import bot as app
2022-02-13 06:43:13 +00:00
from driver.utils import remove_if_exists
2022-02-22 23:37:00 +00:00
2022-01-31 12:41:47 +00:00
from pyrogram import Client, filters
from pyrogram.types import Message
@Client.on_message(command(["speedtest", f"speedtest@{bname}"]) & ~filters.edited)
@sudo_users_only
async def run_speedtest(_, message: Message):
m = await message.reply_text("⚡️ running server speedtest")
try:
test = speedtest.Speedtest()
test.get_best_server()
m = await m.edit("⚡️ running download speedtest..")
test.download()
m = await m.edit("⚡️ running upload speedtest...")
test.upload()
test.results.share()
result = test.results.dict()
except Exception as e:
await m.edit(e)
return
m = await m.edit("🔄 sharing speedtest results")
path = wget.download(result["share"])
try:
img = Image.open(path)
c = img.crop((17, 11, 727, 389))
c.save(path)
2022-02-20 11:20:32 +00:00
except BaseException:
pass
2022-01-31 12:41:47 +00:00
output = f"""💡 **SpeedTest Results**
<u>**Client:**</u>
**ISP:** {result['client']['isp']}
**Country:** {result['client']['country']}
<u>**Server:**</u>
**Name:** {result['server']['name']}
**Country:** {result['server']['country']}, {result['server']['cc']}
**Sponsor:** {result['server']['sponsor']}
**Latency:** {result['server']['latency']}
**Ping:** {result['ping']}"""
msg = await app.send_photo(
chat_id=message.chat.id, photo=path, caption=output
)
2022-02-13 06:43:13 +00:00
remove_if_exists(path)
2022-01-31 12:41:47 +00:00
await m.delete()