55import os
66import platform
77import random
8+ import re
89import string
910import sys
1011import time
1112import configparser
1213import ntplib
1314import requests
1415
16+ import global_var as gl
17+
1518MAIN_VERSION = '1.0.0'
19+ MYS_SALT = "PVeGWIZACpxXZ1ibMVJPi9inCY4Nd4y2"
20+ MYS_SALT_TWO = "t0qEgfub6cvueAPgR5m9aQWWVciEer7v"
21+ MYS_SALT_WEB = "yUZ3s0Sna1IrSNfk29Vo6vRapdOyqyhB"
1622
1723
1824def check_plat ():
@@ -48,6 +54,46 @@ def load_config():
4854 print (err )
4955
5056
57+ def get_cookie_str (target ):
58+ '''
59+ 获取cookie字符串中相应的数据
60+ '''
61+ try :
62+ pattern_str = re .compile (target + '=(.*?);' )
63+ result_str = pattern_str .search (gl .MI_COOKIE )
64+ if result_str :
65+ return result_str .group (1 )
66+ return ''
67+ except KeyboardInterrupt :
68+ print ("强制退出" )
69+ sys .exit ()
70+ except Exception as err :
71+ print (f"运行出错, 错误为: { err } , 错误行数为: { err .__traceback__ .tb_lineno } " )
72+ return None
73+
74+
75+ def write_config_file (section , key , value ):
76+ '''
77+ 写入配置文件
78+ '''
79+ try :
80+ gl .INI_CONFIG .set (section , key , value )
81+ path = os .path .abspath (__file__ )
82+ path = os .path .dirname (path ) + '/config.ini'
83+ with open (path , "w" , encoding = "utf-8" ) as config_file :
84+ gl .INI_CONFIG .write (config_file )
85+ print ("写入成功" )
86+ gl .INI_CONFIG = load_config ()
87+ if key == 'cookie' :
88+ gl .MI_COOKIE = value
89+ except KeyboardInterrupt :
90+ print ("强制退出" )
91+ sys .exit ()
92+ except Exception as err :
93+ print (f"运行出错, 错误为: { err } , 错误行数为: { err .__traceback__ .tb_lineno } " )
94+ input ("按回车键继续" )
95+
96+
5197def compare_version (old_version , new_version ):
5298 '''
5399 版本号比较
@@ -67,12 +113,12 @@ def compare_version(old_version, new_version):
67113 sys .exit ()
68114
69115
70- def check_update (ini_config ):
116+ def check_update ():
71117 '''
72118 检查更新
73119 '''
74120 try :
75- config_version = ini_config .get ('app' , 'version' )
121+ config_version = gl . INI_CONFIG .get ('app' , 'version' )
76122 if MAIN_VERSION == config_version :
77123 print (f"当前程序版本为v{ MAIN_VERSION } , 配置文件版本为v{ config_version } " )
78124 # 远程检查更新
@@ -128,30 +174,136 @@ def get_time(ntp_enable, ntp_server):
128174 return time .time ()
129175
130176
131- # class CheckNetwork:
132- # a = 123
177+ def md5_encode (text ):
178+ '''
179+ md5加密
180+ '''
181+ try :
182+ md5_str = hashlib .md5 ()
183+ md5_str .update (text .encode ('utf-8' ))
184+ return md5_str .hexdigest ()
185+ except KeyboardInterrupt :
186+ print ("强制退出" )
187+ sys .exit ()
188+ except Exception as err :
189+ print (err )
190+ sys .exit ()
133191
134- # def __init__(self) -> None:
135- # print(CheckNetwork)
136- # print(CheckNetwork.a)
137- # CheckNetwork.a = time.time()
138- # print(CheckNetwork.a)
139192
140- # def __del__(self):
141- # print('del')
193+ def get_new_ds (_b , _q ):
194+ '''
195+ 生成请求 Header 里的 DS
196+ 参考:
197+ https://github.com/Womsxd/AutoMihoyoBBS/blob/master/tools.py
198+ https://github.com/Womsxd/AutoMihoyoBBS/blob/master/setting.py
199+ 保留此函数以备后用
200+ '''
201+ try :
202+ t_param = str (int (time .time ()))
203+ r_param = str (random .randint (100001 , 200000 ))
204+ b_param = '传入参数, 待查明'
205+ q_param = '传入参数, 待查明'
206+ c_param = md5_encode (f"salt={ MYS_SALT_TWO } &t={ t_param } &r={ r_param } &b={ b_param } &q={ q_param } " )
207+ return f"{ t_param } ,{ r_param } ,{ c_param } "
208+ except KeyboardInterrupt :
209+ print ("强制退出" )
210+ sys .exit ()
211+ except Exception as err :
212+ print (err )
213+ return None
142214
143215
144- def get_ds ( ):
216+ def get_old_ds ( web : bool ):
145217 '''
146218 生成请求 Header 里的 DS
147219 参考:
148220 https://github.com/Womsxd/AutoMihoyoBBS/blob/master/tools.py
149221 https://github.com/Womsxd/AutoMihoyoBBS/blob/master/setting.py
222+ 保留此函数以备后用
150223 '''
151- android_salt = "t0qEgfub6cvueAPgR5m9aQWWVciEer7v"
224+ if web :
225+ old_salt = MYS_SALT_WEB
226+ else :
227+ old_salt = MYS_SALT
152228 t_param = str (int (time .time ()))
153229 r_param = '' .join (random .sample (string .ascii_lowercase + string .digits , 6 ))
154- md5 = hashlib .md5 ()
155- md5 .update (f'salt={ android_salt } &t={ t_param } &r={ r_param } ' .encode ())
156- c_param = md5 .hexdigest ()
157- return f"{ t_param } ,{ r_param } ,{ c_param } "
230+ c_param = md5_encode (f'salt={ old_salt } &t={ t_param } &r={ r_param } ' )
231+ return f"{ t_param } ,{ r_param } ,{ c_param } "
232+
233+
234+ def update_cookie ():
235+ '''
236+ 更新cookie
237+ 需要stoken
238+ '''
239+ try :
240+ update_cookie_url = gl .MI_URL + "/auth/api/getCookieAccountInfoBySToken"
241+ update_cookie_url_params = {
242+ "uid" : get_cookie_str ("account_id" ),
243+ "stoken" : get_cookie_str ("stoken" )
244+ }
245+ print ("开始更新cookie" )
246+ update_cookie_url_req = requests .get (update_cookie_url , params = update_cookie_url_params )
247+ update_cookie_url_req = update_cookie_url_req .json ()
248+ if update_cookie_url_req ['data' ] is None :
249+ print (f"获取出错,错误原因为: { update_cookie_url_req ['message' ]} " )
250+ new_mi_cookie = re .sub (get_cookie_str ("cookie_token" ),
251+ update_cookie_url_req ['data' ]['cookie_token' ], gl .MI_COOKIE )
252+ write_config_file ("user_info" , "cookie" , new_mi_cookie )
253+ input ("cookie更新成功, 按回车键继续" )
254+ return True
255+ except Exception as err :
256+ print (f"运行出错, 错误为: { err } , 错误行数为: { err .__traceback__ .tb_lineno } " )
257+ input ("按回车键继续" )
258+ return False
259+
260+
261+ def get_point ():
262+ '''
263+ 获取米游币数量
264+ 需要stoken与stuid
265+ '''
266+ try :
267+ point_url = gl .BBS_URL + '/apihub/sapi/getUserMissionsState'
268+ point_hearders = {
269+ 'Cookie' : gl .MI_COOKIE ,
270+ }
271+ point_req = requests .get (point_url , headers = point_hearders )
272+ if point_req .status_code != 200 :
273+ print (f"获取米游币数量失败, 返回状态码为{ point_req .status_code } " )
274+ input ("按回车键继续" )
275+ return False
276+ point_req = point_req .json ()
277+ if point_req ['retcode' ] != 0 :
278+ print (f"获取米游币数量失败, 原因为{ point_req ['message' ]} " )
279+ input ("按回车键继续" )
280+ return False
281+ return point_req ['data' ]['total_points' ], point_req ['data' ]['can_get_points' ]
282+ except Exception as err :
283+ print (f"运行出错, 错误为: { err } , 错误行数为: { err .__traceback__ .tb_lineno } " )
284+ input ("按回车键继续" )
285+ return False
286+
287+
288+ def check_cookie () -> bool :
289+ '''
290+ 检查cookie是否过期
291+ '''
292+ try :
293+ check_cookie_url = gl .MI_URL + '/account/address/list'
294+ check_cookie_hearders = {
295+ 'Cookie' : gl .MI_COOKIE ,
296+ }
297+ check_cookie_req = requests .get (check_cookie_url , headers = check_cookie_hearders )
298+ if check_cookie_req .status_code != 200 :
299+ print (f"检查Cookie失败, 返回状态码为{ check_cookie_req .status_code } " )
300+ return False
301+ check_cookie_req = check_cookie_req .json ()
302+ if check_cookie_req ['retcode' ] != 0 :
303+ print ("Cookie已过期" )
304+ return False
305+ return True
306+ except Exception as err :
307+ print (f"运行出错, 错误为: { err } , 错误行数为: { err .__traceback__ .tb_lineno } " )
308+ input ("按回车键继续" )
309+ return False
0 commit comments