sycgram/plugins/speedtest.py

65 lines
2.3 KiB
Python
Raw Normal View History

2022-04-06 14:39:27 +00:00
import asyncio
import re
from core import command
from pyrogram import Client
from pyrogram.errors import FloodWait
from pyrogram.types import Message
2022-04-09 05:11:35 +00:00
from tools.constants import SPEEDTEST_RUN, SYCGRAM_INFO
from tools.helpers import Parameters, delete_this, show_cmd_tip, show_exception
2022-04-06 14:39:27 +00:00
from tools.speedtests import Speedtester
@Client.on_message(command('speedtest'))
async def speedtest(_: Client, msg: Message):
"""服务器测速,用法:-speedtest <节点ID|list|update>"""
cmd, opt = Parameters.get(msg)
await msg.edit_text("⚡️ Speedtest is running.")
async with Speedtester() as tester:
if opt == 'update':
try:
2022-04-09 05:47:04 +00:00
update_res = await tester.install_speedtest_cli('update')
2022-04-06 14:39:27 +00:00
except asyncio.exceptions.TimeoutError:
2022-04-09 05:11:35 +00:00
await show_exception(msg, "Update Timeout")
2022-04-06 14:39:27 +00:00
except Exception as e:
2022-04-09 05:11:35 +00:00
await show_exception(msg, e)
2022-04-06 14:39:27 +00:00
else:
2022-04-09 05:11:35 +00:00
await msg.edit_text(
f"**{SYCGRAM_INFO}**\n> # `{update_res}`",
parse_mode='md'
)
2022-04-06 14:39:27 +00:00
return
elif opt == 'list':
try:
text = await tester.list_servers_ids(f"{SPEEDTEST_RUN} -L")
await msg.edit_text(text, parse_mode='md')
except asyncio.exceptions.TimeoutError:
2022-04-09 05:11:35 +00:00
await show_exception(msg, "Speedtest Timeout")
except Exception as e:
await show_exception(msg, e)
2022-04-06 14:39:27 +00:00
return
elif bool(re.match(r'[0-9]+', opt)) or not opt:
try:
text, link = await tester.running(
f"""{SPEEDTEST_RUN}{'' if not opt else f' -s {opt}'}"""
)
except asyncio.exceptions.TimeoutError:
2022-04-09 05:11:35 +00:00
return await show_exception(msg, "Speedtest Timeout")
2022-04-06 14:39:27 +00:00
else:
2022-04-09 05:11:35 +00:00
return await show_cmd_tip(msg, cmd)
2022-04-06 14:39:27 +00:00
if not link:
2022-04-09 05:47:04 +00:00
return await msg.edit_text(text)
2022-04-06 14:39:27 +00:00
# send speed report
try:
await msg.reply_photo(photo=link, caption=text, parse_mode='md')
except FloodWait as e:
await asyncio.sleep(e.x)
await msg.reply_photo(photo=link, caption=text, parse_mode='md')
except Exception as e:
2022-04-09 05:11:35 +00:00
await show_exception(msg, e)
2022-04-06 14:39:27 +00:00
# delete cmd history
await delete_this(msg)