Support official CLI to run speedtest. (#113)

 支持官方 CLI 程序运行 speedtest。
This commit is contained in:
Xtao_dada 2021-07-27 12:14:49 +08:00 committed by GitHub
parent 3b3b7f86e4
commit 4268f3bd48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 0 deletions

View File

@ -76,3 +76,6 @@ ipv6: "False"
# Analytics
allow_analytic: "True"
# speed_test
speed_test_path: ""

View File

@ -133,6 +133,60 @@ async def stats(context):
description=lang('speedtest_des'))
async def speedtest(context):
""" Tests internet speed using speedtest. """
try:
speed_test_path = config['speed_test_path']
except KeyError:
speed_test_path = ''
if not speed_test_path == '':
server = None
if len(context.parameter) == 1:
try:
server = int(context.parameter[0])
except ValueError:
await context.edit(lang('arg_error'))
return
speed_test_path += ' -f json'
if server:
speed_test_path += f' -s {server}'
await context.edit(lang('speedtest_processing'))
result = await execute(f'{speed_test_path}')
result = loads(result)
if result['type'] == 'log':
await context.edit(f"{result['level'].upper()}:{result['message']}")
elif result['type'] == 'result':
des = (
f"**Speedtest** \n"
f"Server: `{result['server']['name']} - "
f"{result['server']['location']}` \n"
f"Host: `{result['server']['host']}` \n"
f"Upload: `{unit_convert(result['upload']['bandwidth'] * 8)}` \n"
f"Download: `{unit_convert(result['download']['bandwidth'] * 8)}` \n"
f"Latency: `{result['ping']['latency']}` \n"
f"Jitter: `{result['ping']['jitter']}` \n"
f"Timestamp: `{result['timestamp']}`"
)
# 开始处理图片
data = get(f"{result['result']['url']}.png").content
with open('speedtest.png', mode='wb') as f:
f.write(data)
try:
img = Image.open('speedtest.png')
c = img.crop((17, 11, 727, 389))
c.save('speedtest.png')
except:
pass
try:
await context.client.send_file(context.chat_id, 'speedtest.png', caption=des)
except:
pass
try:
remove('speedtest.png')
except:
pass
await context.delete()
else:
await context.edit(result)
return
try:
test = Speedtest()
except SpeedtestHTTPError: