2021-09-07 01:07:31 +00:00
|
|
|
import os
|
2021-09-06 21:52:48 +00:00
|
|
|
import time
|
|
|
|
import platform
|
2021-12-24 13:53:40 +00:00
|
|
|
import dotenv, yaml
|
2021-09-06 21:52:48 +00:00
|
|
|
|
|
|
|
from hashlib import md5
|
|
|
|
from urllib.request import getproxies
|
|
|
|
|
2021-09-07 01:07:31 +00:00
|
|
|
logs = ''
|
2022-10-31 23:56:53 +00:00
|
|
|
CONFIG_VERSION_REQUIRE: str = 'v1.5.1'
|
2021-09-07 01:07:31 +00:00
|
|
|
|
|
|
|
|
2021-09-06 21:52:48 +00:00
|
|
|
def md5_crypto(passwd: str) -> str:
|
|
|
|
return md5(passwd.encode('utf8')).hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
def show_info(tip, info):
|
|
|
|
return "{}: {}".format(tip, info)
|
|
|
|
|
|
|
|
|
|
|
|
def system_info():
|
2021-12-24 13:53:40 +00:00
|
|
|
w_log(show_info('操作系统平台', platform.platform()))
|
|
|
|
w_log(show_info('操作系统版本', platform.version()))
|
|
|
|
w_log(show_info('操作系统名称', platform.system()))
|
|
|
|
w_log(show_info('操作系统位元', platform.architecture()))
|
|
|
|
w_log(show_info('操作系统类型', platform.machine()))
|
|
|
|
w_log(show_info('处理器信息', platform.processor()))
|
|
|
|
w_log(show_info('Python 版本', str(platform.python_version()) + ' ' + str(platform.python_build())))
|
2021-09-06 21:52:48 +00:00
|
|
|
if getproxies():
|
|
|
|
w_log(show_info('系统代理', getproxies()))
|
|
|
|
|
|
|
|
|
|
|
|
def get_config() -> dict:
|
2021-12-24 13:53:40 +00:00
|
|
|
config = {'account': []}
|
|
|
|
config_path_legacy = dotenv.find_dotenv(filename='config.env')
|
|
|
|
config_path_yaml = dotenv.find_dotenv(filename='config.yaml')
|
|
|
|
|
2022-10-31 23:56:53 +00:00
|
|
|
# the old legacy config
|
2021-12-24 13:53:40 +00:00
|
|
|
if config_path_legacy:
|
|
|
|
w_log('正在使用 ' + config_path_legacy + ' 作为配置文件')
|
|
|
|
legacy_config = dotenv.dotenv_values(config_path_legacy)
|
|
|
|
config['account'].append({'uid': legacy_config.get('MI_ID')})
|
|
|
|
config['account'][0]['password'] = legacy_config.get('MI_PASSWORD')
|
|
|
|
config['account'][0]['user-agent'] = legacy_config.get('USER_AGENT')
|
|
|
|
if legacy_config.get('SIGN_IN') and legacy_config.get('SIGN_IN').upper() in ('Y', 'YES'):
|
|
|
|
config['account'][0]['check-in'] = True
|
|
|
|
else:
|
|
|
|
config['account'][0]['check-in'] = False
|
2022-08-25 16:26:04 +00:00
|
|
|
if legacy_config.get('CARROT_PULL') and legacy_config.get('CARROT_PULL').upper() in ('Y', 'YES'):
|
|
|
|
config['account'][0]['carrot-pull'] = True
|
|
|
|
else:
|
|
|
|
config['account'][0]['carrot-pull'] = False
|
2021-12-24 13:53:40 +00:00
|
|
|
if legacy_config.get('LOG_SAVE') and legacy_config.get('LOG_SAVE').upper() in ('Y', 'YES'):
|
|
|
|
config['logging'] = True
|
|
|
|
else:
|
|
|
|
config['logging'] = False
|
|
|
|
return config
|
2022-10-31 23:56:53 +00:00
|
|
|
|
|
|
|
# the new version yaml config
|
2021-12-24 13:53:40 +00:00
|
|
|
elif config_path_yaml:
|
2022-10-31 23:56:53 +00:00
|
|
|
w_log('正在加载 ' + config_path_yaml + ' 配置文件')
|
2021-12-24 13:53:40 +00:00
|
|
|
with open(config_path_yaml, "rb") as stream:
|
|
|
|
try:
|
|
|
|
config = yaml.safe_load(stream)
|
2022-10-31 23:56:53 +00:00
|
|
|
config_version: str = config.get('version')
|
|
|
|
|
|
|
|
# check config file version
|
|
|
|
# if config version not meet the requirement
|
|
|
|
if CONFIG_VERSION_REQUIRE != config_version:
|
|
|
|
w_log('配置文件版本和程序运行要求版本不匹配,请检查配置文件')
|
|
|
|
w_log('配置文件版本: ' + config_version)
|
|
|
|
w_log('运行程序配置版本要求: ' + CONFIG_VERSION_REQUIRE)
|
|
|
|
exit(1) # exit the program
|
|
|
|
w_log('配置文件已成功加载,文件版本 ' + config_version)
|
|
|
|
|
2021-12-24 13:53:40 +00:00
|
|
|
except yaml.YAMLError as e:
|
|
|
|
w_log('配置文件载入错误')
|
|
|
|
w_log(e)
|
|
|
|
return config
|
2021-10-26 16:35:08 +00:00
|
|
|
else:
|
2021-12-24 13:53:40 +00:00
|
|
|
w_log('配置文件不存在')
|
|
|
|
exit(1)
|
|
|
|
|
2021-09-06 21:52:48 +00:00
|
|
|
|
|
|
|
def w_log(text):
|
2021-09-07 01:07:31 +00:00
|
|
|
global logs
|
2021-09-06 21:52:48 +00:00
|
|
|
now_localtime = time.strftime("%H:%M:%S", time.localtime())
|
2021-09-07 01:07:31 +00:00
|
|
|
logs += now_localtime + ' | ' + str(text) + '\n'
|
2021-09-06 21:52:48 +00:00
|
|
|
print(now_localtime + ' | ' + str(text))
|
|
|
|
|
|
|
|
|
2021-12-24 13:53:40 +00:00
|
|
|
def s_log(flag):
|
2022-10-31 23:56:53 +00:00
|
|
|
if flag:
|
2021-09-07 01:07:31 +00:00
|
|
|
global logs
|
|
|
|
folder = os.path.exists('./logs')
|
|
|
|
if not folder:
|
|
|
|
os.makedirs('./logs')
|
|
|
|
now_localtime = time.strftime("%Y-%m-%d", time.localtime())
|
|
|
|
fname = now_localtime + '.log'
|
|
|
|
with open('./logs/' + fname, 'a+', encoding='utf-8') as f:
|
|
|
|
f.write(logs)
|
|
|
|
|
|
|
|
|
2021-12-24 13:53:40 +00:00
|
|
|
def check_config(config: dict) -> bool:
|
|
|
|
if config.get('accounts'):
|
|
|
|
for i in config.get('accounts'):
|
2022-08-22 00:17:35 +00:00
|
|
|
if not i.get('uid') or not i.get('password') or not i.get('user-agent'):
|
2021-12-24 13:53:40 +00:00
|
|
|
return False
|
2022-08-22 00:17:35 +00:00
|
|
|
if not isinstance(i.get('check-in'), bool):
|
2021-12-24 13:53:40 +00:00
|
|
|
return False
|
2022-08-25 16:26:04 +00:00
|
|
|
if not isinstance(i.get('carrot-pull'), bool):
|
|
|
|
return False
|
2021-12-24 13:53:40 +00:00
|
|
|
else:
|
2021-09-07 01:07:31 +00:00
|
|
|
return False
|
2021-12-24 13:53:40 +00:00
|
|
|
if not isinstance(config.get('logging'), bool):
|
2021-09-07 01:07:31 +00:00
|
|
|
return False
|
|
|
|
return True
|
2021-12-24 13:53:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def format_config(config: dict) -> dict:
|
|
|
|
for i in config.get('accounts'):
|
|
|
|
i['uid'] = str(i.get('uid'))
|
|
|
|
i['user-agent'] = str(i.get('user-agent'))
|
|
|
|
if len(i.get('password')) != 32:
|
|
|
|
i['password'] = md5_crypto(i.get('password')).upper()
|
2021-12-29 06:31:06 +00:00
|
|
|
else:
|
|
|
|
i['password'] = str(i.get('password')).upper()
|
2021-12-24 13:53:40 +00:00
|
|
|
if i.get('device-id'):
|
|
|
|
i['device-id'] = str(i.get('device-id'))
|
|
|
|
else:
|
|
|
|
i['device-id'] = None
|
|
|
|
return config
|