-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathimaotai.py
351 lines (326 loc) · 12.3 KB
/
imaotai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import base64
import datetime
import json
import math
import os
import random
import time
from config import redis_info, message_tokens, RedisUtil
import requests
from Crypto.Cipher import AES
class Encrypt:
def __init__(self, key, iv):
self.key = key.encode("utf-8")
self.iv = iv.encode("utf-8")
def pkcs7padding(self, text):
"""明文使用PKCS7填充"""
bs = 16
length = len(text)
bytes_length = len(text.encode("utf-8"))
padding_size = length if (bytes_length == length) else bytes_length
padding = bs - padding_size % bs
padding_text = chr(padding) * padding
self.coding = chr(padding)
return text + padding_text
def aes_encrypt(self, content):
"""AES加密"""
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
content_padding = self.pkcs7padding(content)
encrypt_bytes = cipher.encrypt(content_padding.encode("utf-8"))
result = str(base64.b64encode(encrypt_bytes), encoding="utf-8")
return result
def aes_decrypt(self, content):
"""AES解密"""
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
content = base64.b64decode(content)
text = cipher.decrypt(content).decode("utf-8")
return text.rstrip(self.coding)
class IMAOTAI():
name = "i茅台"
def __init__(self, check_item):
self.check_item = check_item
self.RESERVE_RULE = 0
self.mt_r = "clips_OlU6TmFRag5rCXwbNAQ/Tz1SKlN8THcecBp/"
self.ITEM_MAP = {
"10941": "53%vol 500ml贵州茅台酒(甲辰龙年)",
"10942": "53%vol 375ml×2贵州茅台酒(甲辰龙年)",
"10056": "53%vol 500ml茅台1935",
"2478": "53%vol 500ml贵州茅台酒(珍品)",
}
self.ITEM_CODES = ["10941", "10942"]
AES_KEY = "qbhajinldepmucsonaaaccgypwuvcjaa"
AES_IV = "2018534749963515"
self.encrypt = Encrypt(key=AES_KEY, iv=AES_IV)
self.mt_version = json.loads(
requests.get("https://itunes.apple.com/cn/lookup?id=1600482450").text
)["results"][0]["version"]
self.headers = {}
self.header_context = """
MT-Lat: 28.499562
MT-K: 1675213490331
MT-Lng: 102.182324
Host: app.moutai519.com.cn
MT-User-Tag: 0
Accept: */*
MT-Network-Type: WIFI
MT-Token: 1
MT-Info: 028e7f96f6369cafe1d105579c5b9377
MT-Device-ID: 2F2075D0-B66C-4287-A903-DBFF6358342A
MT-Bundle-ID: com.moutai.mall
Accept-Language: en-CN;q=1, zh-Hans-CN;q=0.9
MT-Request-ID: 167560018873318465
MT-APP-Version: 1.3.7
User-Agent: iOS;16.3;Apple;?unrecognized?
MT-R: clips_OlU6TmFRag5rCXwbNAQ/Tz1SKlN8THcecBp/HGhHdw==
Content-Length: 93
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: application/json
userId: 2
"""
def init_headers(
self,
user_id: str = "1",
token: str = "2",
lat: str = "29.83826",
lng: str = "119.74375",
):
for k in self.header_context.strip().split("\n"):
temp_l = k.split(": ")
dict.update(self.headers, {temp_l[0]: temp_l[1]})
dict.update(self.headers, {"userId": user_id})
dict.update(self.headers, {"MT-Token": token})
dict.update(self.headers, {"MT-Lat": lat})
dict.update(self.headers, {"MT-Lng": lng})
dict.update(self.headers, {"MT-APP-Version": self.mt_version})
def get_current_session_id(self):
day_time = int(time.mktime(datetime.date.today().timetuple())) * 1000
my_url = f"https://static.moutai519.com.cn/mt-backend/xhr/front/mall/index/session/get/{day_time}"
responses = requests.get(my_url)
if responses.status_code != 200:
print(
f"get_current_session_id : params : {day_time}, response code : {responses.status_code}, response body : {responses.text}"
)
current_session_id = responses.json()["data"]["sessionId"]
dict.update(self.headers, {"current_session_id": str(current_session_id)})
def get_map(self, lat: str = "28.499562", lng: str = "102.182324"):
p_c_map = {}
url = "https://static.moutai519.com.cn/mt-backend/xhr/front/mall/resource/get"
headers = {
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 15_0_1 like Mac OS X)",
"Referer": "https://h5.moutai519.com.cn/gux/game/main?appConfig=2_1_2",
"Client-User-Agent": "iOS;16.0.1;Apple;iPhone 14 ProMax",
"MT-R": "clips_OlU6TmFRag5rCXwbNAQ/Tz1SKlN8THcecBp/HGhHdw==",
"Origin": "https://h5.moutai519.com.cn",
"MT-APP-Version": self.mt_version,
"MT-Request-ID": f"{int(time.time() * 1000)}{random.randint(1111111, 999999999)}{int(time.time() * 1000)}",
"Accept-Language": "zh-CN,zh-Hans;q=1",
"MT-Device-ID": f"{int(time.time() * 1000)}{random.randint(1111111, 999999999)}{int(time.time() * 1000)}",
"Accept": "application/json, text/javascript, */*; q=0.01",
"mt-lng": f"{lng}",
"mt-lat": f"{lat}",
}
res = requests.get(url, headers=headers)
mtshops = res.json().get("data", {}).get("mtshops_pc", {})
urls = mtshops.get("url")
r = requests.get(urls)
for k, v in dict(r.json()).items():
provinceName = v.get("provinceName")
cityName = v.get("cityName")
if not p_c_map.get(provinceName):
p_c_map[provinceName] = {}
if not p_c_map[provinceName].get(cityName, None):
p_c_map[provinceName][cityName] = [k]
else:
p_c_map[provinceName][cityName].append(k)
return p_c_map, dict(r.json())
def max_shop(self, city, item_code, p_c_map, province, shops):
max_count = 0
max_shop_id = "0"
shop_ids = p_c_map[province][city]
for shop in shops:
shopId = shop["shopId"]
items = shop["items"]
if shopId not in shop_ids:
continue
for item in items:
if item["itemId"] != str(item_code):
continue
if item["inventory"] > max_count:
max_count = item["inventory"]
max_shop_id = shopId
print(
f"item code {item_code}, max shop id : {max_shop_id}, max count : {max_count}"
)
return max_shop_id
def distance_shop(
self,
item_code,
shops,
source_data,
lat: str = "28.499562",
lng: str = "102.182324",
):
temp_list = []
for shop in shops:
shopId = shop["shopId"]
items = shop["items"]
item_ids = [i["itemId"] for i in items]
if str(item_code) not in item_ids:
continue
shop_info = source_data.get(shopId)
d = math.sqrt(
(float(lat) - shop_info["lat"]) ** 2
+ (float(lng) - shop_info["lng"]) ** 2
)
temp_list.append((d, shopId))
temp_list = sorted(temp_list, key=lambda x: x[0])
if len(temp_list) > 0:
return temp_list[0][1]
else:
return "0"
def get_location_count(
self,
province: str,
city: str,
item_code: str,
p_c_map: dict,
source_data: dict,
lat: str = "29.83826",
lng: str = "102.182324",
reserve_rule: int = 0,
):
day_time = int(time.mktime(datetime.date.today().timetuple())) * 1000
session_id = self.headers["current_session_id"]
responses = requests.get(
f"https://static.moutai519.com.cn/mt-backend/xhr/front/mall/shop/list/slim/v3/{session_id}/{province}/{item_code}/{day_time}"
)
if responses.status_code != 200:
print(
f"get_location_count : params : {day_time}, response code : {responses.status_code}, response body : {responses.text}"
)
shops = responses.json()["data"]["shops"]
if reserve_rule == 0:
return self.distance_shop(item_code, shops, source_data, lat, lng)
if reserve_rule == 1:
return self.max_shop(city, item_code, p_c_map, province, shops)
def act_params(self, shop_id: str, item_id: str):
session_id = self.headers["current_session_id"]
userId = self.headers["userId"]
params = {
"itemInfoList": [{"count": 1, "itemId": item_id}],
"sessionId": int(session_id),
"userId": userId,
"shopId": shop_id,
}
s = json.dumps(params)
act = self.encrypt.aes_encrypt(s)
params.update({"actParam": act})
return params
def reservation(self, params: dict):
params.pop("userId")
responses = requests.post(
"https://app.moutai519.com.cn/xhr/front/mall/reservation/add",
json=params,
headers=self.headers,
).json()
if responses.get("code") == 401:
msg = {
"name": "申购结果",
"value": "token失效, 请重新抓包获取",
}
elif responses.get("code") != 2000:
msg = {
"name": "申购结果",
"value": responses.get("message"),
}
else:
msg = {
"name": "申购结果",
"value": responses.get("data", {}).get("successDesc"),
}
return msg
def getUserEnergyAward(self):
"""
耐力值
"""
cookies = {
"MT-Device-ID-Wap": self.headers["MT-Device-ID"],
"MT-Token-Wap": self.headers["MT-Token"],
"YX_SUPPORT_WEBP": "1",
}
response = requests.post(
url="https://h5.moutai519.com.cn/game/isolationPage/getUserEnergyAward",
cookies=cookies,
headers=self.headers,
json={},
).json()
if response.get("code") == 200:
msg = {
"name": "耐力",
"value": "✅领取耐力成功",
}
else:
msg = {
"name": "耐力",
"value": response.get("message"),
}
return msg
def main(self):
msg = []
mobile = self.check_item.get("mobile")
province = self.check_item.get("province")
city = self.check_item.get("city")
token = self.check_item.get("token")
userId = self.check_item.get("userid")
lat = self.check_item.get("lat")
lng = self.check_item.get("lng")
item_codes = self.check_item.get("item_codes", self.ITEM_CODES)
reserve_rule = self.check_item.get("reserve_rule", 0)
msg = [
{
"name": "手机号",
"value": f"{mobile}",
},
{
"name": "省份城市",
"value": f"{province}{city}",
},
]
p_c_map, source_data = self.get_map(lat=lat, lng=lng)
self.get_current_session_id()
self.init_headers(user_id=userId, token=token, lng=lng, lat=lat)
try:
for item in item_codes:
max_shop_id = self.get_location_count(
province=province,
city=city,
item_code=item,
p_c_map=p_c_map,
source_data=source_data,
lat=lat,
lng=lng,
reserve_rule=reserve_rule,
)
if max_shop_id == "0":
continue
reservation_params = self.act_params(max_shop_id, item)
reservation_msg = self.reservation(reservation_params)
time.sleep(20)
award_msg = self.getUserEnergyAward()
msg.append(reservation_msg)
msg.append(award_msg)
except BaseException as e:
msg.append(
{
"name": "申购结果",
"value": e,
}
)
msg = "\n".join([f"{one.get('name')}: {one.get('value')}" for one in msg])
return msg
if __name__ == "__main__":
datas = RedisUtil(redis_info).get('IMAOTAI') # 获取Redis取得茅台配置数据
_check_item = datas.get("IMAOTAI", [])[0]
print(IMAOTAI(check_item=_check_item).main())