Skip to content

Commit 55e3050

Browse files
committed
update: 新增主程序
1.新增主程序, 优化使用体验 2.更新部分函数逻辑 3.新增全局变量
1 parent 9903579 commit 55e3050

File tree

8 files changed

+405
-211
lines changed

8 files changed

+405
-211
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313

1414
| 参数 | 说明 |
1515
| ---------- | ------------------------------------------------------------ |
16-
| cookie | 用户`cookie`数据, 使用`get_info.py`获取 |
16+
| cookie | 用户`cookie`数据 |
1717
| uid | 用户游戏内UID, 暂需手动填写, 填写为需要兑换的游戏商品对应的UID, 暂只支持一个 |
18-
| address_id | 用户收货地址ID, 使用`get_info.py`获取 |
18+
| address_id | 用户收货地址ID |
1919

2020
[exchange_info]
2121

22-
| 参数 | 说明 |
23-
| ------- | --------------------------------------- |
24-
| good_id | 需要兑换的商品ID, 使用`get_info.py`获取 |
25-
| time | 兑换商品的开始时间, 需手动填写 |
26-
| thread | 每个商品同时请求兑换的线程数 |
27-
| retry | 请求时重试次数 |
22+
| 参数 | 说明 |
23+
| ------- | ------------------------------ |
24+
| good_id | 需要兑换的商品ID |
25+
| time | 兑换商品的开始时间, 需手动填写 |
26+
| thread | 每个商品同时请求兑换的线程数 |
27+
| retry | 请求时重试次数 |
2828

2929
[check_network]
3030

@@ -67,9 +67,9 @@ enable = true
6767
ntp_server = ntp.aliyun.com
6868
```
6969

70-
### 运行`exchange_gift.py`
70+
### 运行`main.py`
7171

72-
`python exchange_gift.py`
72+
`python main.py`
7373

7474
## 其他
7575

com_tool.py

Lines changed: 169 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55
import os
66
import platform
77
import random
8+
import re
89
import string
910
import sys
1011
import time
1112
import configparser
1213
import ntplib
1314
import requests
1415

16+
import global_var as gl
17+
1518
MAIN_VERSION = '1.0.0'
19+
MYS_SALT = "PVeGWIZACpxXZ1ibMVJPi9inCY4Nd4y2"
20+
MYS_SALT_TWO = "t0qEgfub6cvueAPgR5m9aQWWVciEer7v"
21+
MYS_SALT_WEB = "yUZ3s0Sna1IrSNfk29Vo6vRapdOyqyhB"
1622

1723

1824
def 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+
5197
def 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

config.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ version = 1.0.0
88
cookie =
99
uid =
1010
address_id =
11+
point =
1112

1213
[exchange_info]
1314
good_id =
1415
time = 0000-00-00 00:00:00
1516
thread = 3
16-
retry = 3
17+
retry = 5
1718

1819
[check_network]
1920
enable = true
@@ -25,4 +26,4 @@ enable = false
2526

2627
[ntp]
2728
enable = true
28-
ntp_server = ntp.aliyun.com
29+
ntp_server = ntp.aliyun.com

0 commit comments

Comments
 (0)