speedtest supports custom server (#81)

 speedtest 支持自定义服务器 (#81)
This commit is contained in:
Xtao_dada 2021-05-29 08:34:10 +08:00 committed by GitHub
parent 11ed9c9a98
commit 44335b7f3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,14 +1,15 @@
""" PagerMaid module that contains utilities related to system status. """ """ PagerMaid module that contains utilities related to system status. """
from json import loads
from os import remove, popen from os import remove, popen
from datetime import datetime from datetime import datetime
from speedtest import Speedtest, ShareResultsConnectFailure, ShareResultsSubmitFailure, SpeedtestBestServerFailure from speedtest import distance, Speedtest, ShareResultsConnectFailure, ShareResultsSubmitFailure, NoMatchedServers, SpeedtestBestServerFailure
from telethon import functions from telethon import functions
from platform import python_version, uname from platform import python_version, uname
from wordcloud import WordCloud from wordcloud import WordCloud
from telethon import version as telethon_version from telethon import version as telethon_version
from sys import platform from sys import platform
from re import sub from re import sub, findall
from pathlib import Path from pathlib import Path
from pagermaid import log, config, redis_status from pagermaid import log, config, redis_status
from pagermaid.utils import execute, upload_attachment from pagermaid.utils import execute, upload_attachment
@ -90,18 +91,43 @@ async def status(context):
description=lang('speedtest_des')) description=lang('speedtest_des'))
async def speedtest(context): async def speedtest(context):
""" Tests internet speed using speedtest. """ """ Tests internet speed using speedtest. """
await context.edit(lang('speedtest_processing'))
test = Speedtest() test = Speedtest()
server, server_json = [], False
if len(context.parameter) == 1:
try: try:
server = [int(context.parameter[0])]
except ValueError:
await context.edit(lang('arg_error'))
return
elif len(context.parameter) >= 2:
try:
temp_json = findall(r'{(.*?)}', context.text.replace("'", '"'))
if len(temp_json) == 1:
server_json = loads("{" + temp_json[0] + "}")
server_json['d'] = distance(test.lat_lon, (float(server_json['lat']), float(server_json['lon'])))
test.servers = [server_json]
else:
await context.edit(lang('arg_error'))
return
except:
pass
await context.edit(lang('speedtest_processing'))
try:
if len(server) == 0:
if not server_json:
test.get_best_server() test.get_best_server()
except SpeedtestBestServerFailure: else:
test.get_best_server(servers=test.servers)
else:
test.get_servers(servers=server)
except (SpeedtestBestServerFailure, NoMatchedServers) as e:
await context.edit(lang('speedtest_ServerFailure')) await context.edit(lang('speedtest_ServerFailure'))
return return
test.download() test.download()
test.upload() test.upload()
try: try:
test.results.share() test.results.share()
except ShareResultsConnectFailure or ShareResultsSubmitFailure: except (ShareResultsConnectFailure, ShareResultsSubmitFailure) as e:
await context.edit(lang('speedtest_ConnectFailure')) await context.edit(lang('speedtest_ConnectFailure'))
return return
result = test.results.dict() result = test.results.dict()