speed_test 支持查询附近 测速点列表

This commit is contained in:
xtaodada 2022-07-17 18:44:10 +08:00
parent 3d883f7e4a
commit 1a13ed75d5
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 20 additions and 4 deletions

View File

@ -342,7 +342,7 @@
},
{
"name": "speed_test",
"version": "1.0",
"version": "1.002",
"section": "chat",
"maintainer": "xtaodada",
"size": "3.02 kb",

View File

@ -65,14 +65,29 @@ async def run_speedtest(message: Message):
return des, "speedtest.png" if exists("speedtest.png") else None
async def get_all_ids():
test = Speedtest()
servers = test.get_closest_servers()
if not servers:
return "附近没有测速点", None
return "附近的测速点有:\n\n" + \
"\n".join(f"`{i['id']}` - `{int(i['d'])}km` - `{i['name']}` - `{i['sponsor']}`" for i in servers), None
@listener(command="speedtest",
description=lang('speedtest_des'),
parameters="(Server ID)")
parameters="(Server ID/测速点列表)")
async def speedtest(message: Message):
""" Tests internet speed using speedtest. """
msg = await message.edit(lang('speedtest_processing'))
if message.arguments == "测速点列表":
msg = message
else:
msg: Message = await message.edit(lang('speedtest_processing'))
try:
des, photo = await run_speedtest(message)
if message.arguments == "测速点列表":
des, photo = await get_all_ids()
else:
des, photo = await run_speedtest(message)
except SpeedtestHTTPError:
return await msg.edit(lang('speedtest_ConnectFailure'))
except ValueError:
@ -87,4 +102,5 @@ async def speedtest(message: Message):
await message.bot.send_photo(message.chat.id, photo, caption=des)
except Exception:
return await msg.edit(des)
await msg.safe_delete()
safe_remove(photo)