1.增加了是否使用数据库的判断。
This commit is contained in:
parent
68be3a9a64
commit
f9bff910bb
|
|
@ -1,14 +1,14 @@
|
|||
import argparse
|
||||
import json
|
||||
import sys
|
||||
|
||||
g_args = None
|
||||
json_obj = None
|
||||
print('Config is loaded.')
|
||||
_g_args = None
|
||||
_json_obj = None
|
||||
|
||||
|
||||
def get_servers():
|
||||
global json_obj
|
||||
js = json_obj
|
||||
global _json_obj
|
||||
js = _json_obj
|
||||
_servers = js["servers"]
|
||||
servers_dic = {}
|
||||
for s in _servers:
|
||||
|
|
@ -19,8 +19,8 @@ def get_servers():
|
|||
|
||||
|
||||
def read_json():
|
||||
global g_args
|
||||
json_path = g_args.config
|
||||
global _g_args
|
||||
json_path = _g_args.config
|
||||
if json_path is None:
|
||||
print("specify json path")
|
||||
sys.exit()
|
||||
|
|
@ -30,23 +30,32 @@ def read_json():
|
|||
|
||||
|
||||
def get_config():
|
||||
global json_obj
|
||||
return json_obj
|
||||
global _json_obj
|
||||
return _json_obj
|
||||
|
||||
|
||||
def init_config():
|
||||
global g_args
|
||||
if g_args is not None:
|
||||
return g_args
|
||||
global _g_args
|
||||
if _g_args is not None:
|
||||
return _g_args
|
||||
parser = argparse.ArgumentParser(description="Help")
|
||||
parser.add_argument("-config", type=str)
|
||||
_args = parser.parse_args()
|
||||
g_args = _args
|
||||
global json_obj
|
||||
json_obj = read_json()
|
||||
_g_args = _args
|
||||
global _json_obj
|
||||
_json_obj = read_json()
|
||||
return _args
|
||||
|
||||
|
||||
def get(_setting):
|
||||
if _setting in setting:
|
||||
return setting[_setting]
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
init_config()
|
||||
setting = _json_obj
|
||||
|
||||
if __name__ == "__main__":
|
||||
args = init_config()
|
||||
print(args.config)
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -6,6 +6,9 @@ from download_speed import config, model
|
|||
# file_size if byte
|
||||
# longest_time seconds
|
||||
def download_file(chunk_size=4 * 1024, file_size=100 * 1024 * 1024, longest_time=60):
|
||||
config_file_size = config.get("download_file_size")
|
||||
if config_file_size is not None:
|
||||
file_size = config_file_size
|
||||
execution_datetime = datetime.datetime.now()
|
||||
print("Download at {time}".format(time=execution_datetime))
|
||||
print(
|
||||
|
|
|
|||
|
|
@ -3,13 +3,15 @@ import datetime
|
|||
from download_speed import config
|
||||
|
||||
config.init_config()
|
||||
mysql_addr = config.read_json()
|
||||
mysql_addr = config.get('mysql_addr')
|
||||
mysql_db = MySQLDatabase(
|
||||
"vps", user="dmy", password="abc123+_", host=mysql_addr, port=3306
|
||||
)
|
||||
|
||||
|
||||
def add_record(execution_datetime, server, speed):
|
||||
if not config.get('mysql_enable'):
|
||||
return
|
||||
execution_datetime_utc = execution_datetime - datetime.timedelta(
|
||||
hours=8
|
||||
) # 转换为UTC时间。
|
||||
|
|
@ -22,6 +24,8 @@ def add_record(execution_datetime, server, speed):
|
|||
|
||||
|
||||
def add_ping_record(execution_datetime, server, ping):
|
||||
if not config.get('mysql_enable'):
|
||||
return
|
||||
execution_datetime_utc = execution_datetime - datetime.timedelta(
|
||||
hours=8
|
||||
) # 转换为UTC时间。
|
||||
|
|
|
|||
6
main.py
6
main.py
|
|
@ -3,11 +3,5 @@
|
|||
from download_speed import downloader, config
|
||||
|
||||
if __name__ == "__main__":
|
||||
config.init_config()
|
||||
setting = config.get_config()
|
||||
if "download_file_size" in setting:
|
||||
download_file_size = setting["download_file_size"]
|
||||
downloader.download_file(file_size=download_file_size)
|
||||
else:
|
||||
downloader.download_file()
|
||||
print("Finished.")
|
||||
|
|
|
|||
Loading…
Reference in New Issue