MihoyoBBSTools/tools.py

88 lines
2.2 KiB
Python
Raw Normal View History

2021-06-06 09:50:00 +00:00
import os
2021-05-24 08:16:52 +00:00
import uuid
2021-05-23 13:24:20 +00:00
import time
2021-05-24 08:16:52 +00:00
import config
2021-05-23 13:24:20 +00:00
import random
import string
2021-05-24 08:16:52 +00:00
import logging
2021-05-23 13:24:20 +00:00
import hashlib
import setting
2021-10-25 14:53:34 +00:00
# Log输出这里提供了自定义logging输出的机会只需要创建一个logging.ini并且写入配置文件即可自定义输出
2021-06-06 13:19:28 +00:00
if os.path.exists(f"{config.path}/logging.ini"):
import logging.config
2021-10-25 14:53:34 +00:00
2021-06-06 09:50:00 +00:00
logging.config.fileConfig(f"{config.path}/logging.ini")
log = logging.getLogger("AutoMihoyoBBS")
2021-06-06 09:50:00 +00:00
else:
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%Y-%m-%dT%H:%M:%S')
log = logger = logging
2021-05-23 13:24:20 +00:00
2021-10-25 14:53:34 +00:00
# md5计算
def MD5(text: str) -> str:
2021-05-23 13:24:20 +00:00
md5 = hashlib.md5()
md5.update(text.encode())
2021-06-06 13:19:28 +00:00
return md5.hexdigest()
2021-05-23 13:24:20 +00:00
2021-10-25 14:53:34 +00:00
# 随机文本
def Random_text(num: int) -> str:
return ''.join(random.sample(string.ascii_lowercase + string.digits, num))
# 时间戳
2021-05-23 13:24:20 +00:00
def Timestamp() -> int:
2021-06-06 13:19:28 +00:00
return int(time.time())
2021-05-23 13:24:20 +00:00
2021-10-25 14:53:34 +00:00
# 获取请求Header里的DS 当web为true则生成网页端的DS
def Get_ds(web: bool, web_old: bool) -> str:
2021-09-30 13:00:12 +00:00
if web:
if web_old:
2021-05-23 13:24:20 +00:00
n = setting.mihoyobbs_Salt_web_old
else:
n = setting.mihoyobbs_Salt_web
else:
n = setting.mihoyobbs_Salt
i = str(Timestamp())
r = Random_text(6)
c = MD5("salt=" + n + "&t=" + i + "&r=" + r)
2021-06-06 13:19:28 +00:00
return f"{i},{r},{c}"
2021-05-23 13:24:20 +00:00
2021-10-25 14:53:34 +00:00
# 生成一个device id
def Get_deviceid() -> str:
2021-06-06 13:19:28 +00:00
return str(uuid.uuid3(uuid.NAMESPACE_URL, config.mihoyobbs_Cookies)).replace(
2021-10-25 14:53:34 +00:00
'-', '').upper()
2021-05-24 08:16:52 +00:00
2021-10-25 14:53:34 +00:00
# 获取签到的奖励名称
def Get_item(raw_data: dict) -> str:
temp_Name = raw_data["name"]
temp_Cnt = raw_data["cnt"]
return f"{temp_Name}x{temp_Cnt}"
2021-10-25 14:53:34 +00:00
# 获取明天早晨0点的时间戳
2021-05-23 13:24:20 +00:00
def Nextday() -> int:
now_time = int(time.time())
nextday_time = now_time - now_time % 86400 + time.timezone + 86400
return nextday_time
2021-10-25 14:53:34 +00:00
# 获取Openssl版本
def Get_openssl_Version() -> int:
2021-06-18 13:41:30 +00:00
try:
import ssl
except ImportError:
log.error("Openssl Lib Error !!")
2021-10-25 14:53:34 +00:00
# return -99
# 建议直接更新Python的版本有特殊情况请提交issues
2021-06-18 13:41:30 +00:00
exit(-1)
temp_List = ssl.OPENSSL_VERSION_INFO
return int(f"{str(temp_List[0])}{str(temp_List[1])}{str(temp_List[2])}")