forked from cycz/jdBuyMask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jdBuyMask_V2.py
551 lines (475 loc) · 18.7 KB
/
jdBuyMask_V2.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# -*- coding=utf-8 -*-
'''
2020/2/13
(避免滥用,代码已经废弃,现已不更新,有需要请适量使用exe版本)
京东抢购口罩程序
通过商品的skuid、地区id抢购
'''
import sys
import requests
from bs4 import BeautifulSoup
from config import Config
from jdProgram import *
from message import message
from util import getconfigMd5, _setDNSCache
global cookies_String, mail, sc_key, messageType, modelType, area, skuidsString, skuids, captchaUrl, eid, fp, payment_pwd
def getconfig():
global cookies_String, mail, sc_key, messageType, modelType, area, skuidsString, skuids, captchaUrl, eid, fp, payment_pwd
global_config = Config()
# cookie 网页获取
cookies_String = global_config.getRaw('config', 'cookies_String')
# 有货通知 收件邮箱
mail = global_config.getRaw('config', 'mail')
# 方糖微信推送的key 不知道的请看http://sc.ftqq.com/3.version
sc_key = global_config.getRaw('config', 'sc_key')
# 推送方式 1(mail)或 2(wechat)
messageType = global_config.getRaw('config', 'messageType')
# 下单模式
modelType = global_config.getRaw('V2', 'model')
# 地区id
area = global_config.getRaw('config', 'area')
# 商品id
skuidsString = global_config.getRaw('V2', 'skuids')
skuids = str(skuidsString).split(',')
# 验证码服务地址
captchaUrl = global_config.getRaw('Temporary', 'captchaUrl')
if not modelType:
logger.error('请在configDemo.ini文件填写下单model')
if len(skuids[0]) == 0:
logger.error('请在configDemo.ini文件中输入你的商品id')
sys.exit(1)
'''
备用
'''
# eid
eid = global_config.getRaw('Temporary', 'eid')
fp = global_config.getRaw('Temporary', 'fp')
# 支付密码
payment_pwd = global_config.getRaw('config', 'payment_pwd')
# 初次
configTime = int(time.time())
getconfig()
configMd5 = getconfigMd5()
message = message(messageType=messageType, sc_key=sc_key, mail=mail)
is_Submit_captcha = False
submit_captcha_rid = ''
submit_captcha_text = ''
encryptClientInfo = ''
submit_Time = 0
session = requests.session()
session.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/531.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Connection": "keep-alive"
}
checksession = requests.session()
checksession.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/531.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Connection": "keep-alive"
}
manual_cookies = {}
def get_tag_value(tag, key='', index=0):
if key:
value = tag[index].get(key)
else:
value = tag[index].text
return value.strip(' \t\r\n')
for item in cookies_String.split(';'):
name, value = item.strip().split('=', 1)
# 用=号分割,分割1次
manual_cookies[name] = value
# 为字典cookies添加内容
cookiesJar = requests.utils.cookiejar_from_dict(manual_cookies, cookiejar=None, overwrite=True)
session.cookies = cookiesJar
def validate_cookies():
for flag in range(1, 3):
try:
targetURL = 'https://order.jd.com/center/list.action'
payload = {
'rid': str(int(time.time() * 1000)),
}
resp = session.get(url=targetURL, params=payload, allow_redirects=False)
if resp.status_code == requests.codes.OK:
logger.info('校验是否登录[成功]')
return True
else:
logger.info('校验是否登录[失败]')
logger.info('请在configDemo.ini文件下更新cookie')
time.sleep(5)
continue
except Exception as e:
logger.info('第【%s】次请重新获取cookie', flag)
time.sleep(5)
continue
message.sendAny('脚本登录cookie失效了,请重新登录')
sys.exit(1)
def getUsername():
userName_Url = 'https://passport.jd.com/new/helloService.ashx?callback=jQuery339448&_=' + str(
int(time.time() * 1000))
session.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/531.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Referer": "https://order.jd.com/center/list.action",
"Connection": "keep-alive"
}
resp = session.get(url=userName_Url, allow_redirects=True)
resultText = resp.text
resultText = resultText.replace('jQuery339448(', '')
resultText = resultText.replace(')', '')
usernameJson = json.loads(resultText)
logger.info('登录账号名称[%s]', usernameJson['nick'])
'''
检查是否有货
'''
def check_item_stock(itemUrl):
response = session.get(itemUrl)
if (response.text.find('无货') > 0):
return True
else:
return False
'''
取消勾选购物车中的所有商品
'''
def cancel_select_all_cart_item():
url = "https://cart.jd.com/cancelAllItem.action"
data = {
't': 0,
'outSkus': '',
'random': random.random()
}
resp = session.post(url, data=data)
if resp.status_code != requests.codes.OK:
print('Status: %u, Url: %s' % (resp.status_code, resp.url))
return False
return True
'''
购物车详情
'''
def cart_detail():
url = 'https://cart.jd.com/cart.action'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/531.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Referer": "https://order.jd.com/center/list.action",
"Host": "cart.jd.com",
"Connection": "keep-alive"
}
resp = session.get(url, headers=headers)
soup = BeautifulSoup(resp.text, "html.parser")
cart_detail = dict()
for item in soup.find_all(class_='item-item'):
try:
sku_id = item['skuid'] # 商品id
except Exception as e:
logger.info('购物车中有套装商品,跳过')
continue
try:
# 例如:['increment', '8888', '100001071956', '1', '13', '0', '50067652554']
# ['increment', '8888', '100002404322', '2', '1', '0']
item_attr_list = item.find(class_='increment')['id'].split('_')
p_type = item_attr_list[4]
promo_id = target_id = item_attr_list[-1] if len(item_attr_list) == 7 else 0
cart_detail[sku_id] = {
'name': get_tag_value(item.select('div.p-name a')), # 商品名称
'verder_id': item['venderid'], # 商家id
'count': int(item['num']), # 数量
'unit_price': get_tag_value(item.select('div.p-price strong'))[1:], # 单价
'total_price': get_tag_value(item.select('div.p-sum strong'))[1:], # 总价
'is_selected': 'item-selected' in item['class'], # 商品是否被勾选
'p_type': p_type,
'target_id': target_id,
'promo_id': promo_id
}
except Exception as e:
logger.error("商品%s在购物车中的信息无法解析,报错信息: %s,该商品自动忽略", sku_id, e)
logger.info('购物车信息:%s', cart_detail)
return cart_detail
'''
修改购物车商品的数量
'''
def change_item_num_in_cart(sku_id, vender_id, num, p_type, target_id, promo_id):
url = "https://cart.jd.com/changeNum.action"
data = {
't': 0,
'venderId': vender_id,
'pid': sku_id,
'pcount': num,
'ptype': p_type,
'targetId': target_id,
'promoID': promo_id,
'outSkus': '',
'random': random.random(),
# 'locationId'
}
session.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/531.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Referer": "https://cart.jd.com/cart",
"Connection": "keep-alive"
}
resp = session.post(url, data=data)
return json.loads(resp.text)['sortedWebCartResult']['achieveSevenState'] == 2
'''
添加商品到购物车
'''
def add_item_to_cart(sku_id):
url = 'https://cart.jd.com/gate.action'
payload = {
'pid': sku_id,
'pcount': 1,
'ptype': 1,
}
resp = session.get(url=url, params=payload)
if 'https://cart.jd.com/cart.action' in resp.url: # 套装商品加入购物车后直接跳转到购物车页面
result = True
else: # 普通商品成功加入购物车后会跳转到提示 "商品已成功加入购物车!" 页面
soup = BeautifulSoup(resp.text, "html.parser")
result = bool(soup.select('h3.ftx-02')) # [<h3 class="ftx-02">商品已成功加入购物车!</h3>]
if result:
logger.info('%s 已成功加入购物车', sku_id)
else:
logger.error('%s 添加到购物车失败', sku_id)
def get_checkout_page_detail():
"""获取订单结算页面信息
该方法会返回订单结算页面的详细信息:商品名称、价格、数量、库存状态等。
:return: 结算信息 dict
"""
url = 'http://trade.jd.com/shopping/order/getOrderInfo.action'
# url = 'https://cart.jd.com/gotoOrder.action'
payload = {
'rid': str(int(time.time() * 1000)),
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/531.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Referer": "https://cart.jd.com/cart.action",
"Connection": "keep-alive",
'Host': 'trade.jd.com',
}
try:
resp = session.get(url=url, params=payload, headers=headers)
if not response_status(resp):
logger.error('获取订单结算页信息失败')
return ''
if '刷新太频繁了' in resp.text:
return '刷新太频繁了'
soup = BeautifulSoup(resp.text, "html.parser")
risk_control = get_tag_value(soup.select('input#riskControl'), 'value')
showCheckCode = get_tag_value(soup.select('input#showCheckCode'), 'value')
if not showCheckCode:
pass
else:
if showCheckCode == 'true':
logger.info('提交订单需要验证码')
global is_Submit_captcha, encryptClientInfo
encryptClientInfo = get_tag_value(soup.select('input#encryptClientInfo'), 'value')
is_Submit_captcha = True
order_detail = {
'address': soup.find('span', id='sendAddr').text[5:], # remove '寄送至: ' from the begin
'receiver': soup.find('span', id='sendMobile').text[4:], # remove '收件人:' from the begin
'total_price': soup.find('span', id='sumPayPriceId').text[1:], # remove '¥' from the begin
'items': []
}
logger.info("下单信息:%s", order_detail)
return risk_control
except requests.exceptions.RequestException as e:
logger.error('订单结算页面获取异常:%s' % e)
except Exception as e:
logger.error('下单页面数据解析异常:%s', e)
return ''
'''
下柜商品检测
'''
def item_removed(sku_id):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/531.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Referer": "http://trade.jd.com/shopping/order/getOrderInfo.action",
"Connection": "keep-alive",
'Host': 'item.jd.com',
}
url = 'https://item.jd.com/{}.html'.format(sku_id)
page = requests.get(url=url, headers=headers)
return '该商品已下柜' not in page.text
'''
购买环节
测试三次
'''
def normalModeBuyMask(sku_id):
cancel_select_all_cart_item()
cart = cart_detail()
if sku_id in cart:
logger.info('%s 已在购物车中,调整数量为 %s', sku_id, 1)
cart_item = cart.get(sku_id)
change_item_num_in_cart(
sku_id=sku_id,
vender_id=cart_item.get('vender_id'),
num=1,
p_type=cart_item.get('p_type'),
target_id=cart_item.get('target_id'),
promo_id=cart_item.get('promo_id')
)
else:
add_item_to_cart(sku_id)
risk_control = get_checkout_page_detail()
if risk_control == '刷新太频繁了':
return False
if len(risk_control) > 0:
if submit_order(session, risk_control, sku_id, skuids, submit_Time, encryptClientInfo, is_Submit_captcha,
payment_pwd, submit_captcha_text, submit_captcha_rid):
return True
return False
def fastModeBuyMask(sku_id):
add_item_to_cart(sku_id)
risk_control = get_checkout_page_detail()
if risk_control == '刷新太频繁了':
return False
if len(risk_control) > 0:
if submit_order(session, risk_control, sku_id, skuids, submit_Time, encryptClientInfo, is_Submit_captcha,
payment_pwd, submit_captcha_text, submit_captcha_rid):
return True
return False
'''
删除购物车选中商品
'''
def remove_item():
url = "https://cart.jd.com/batchRemoveSkusFromCart.action"
data = {
't': 0,
'null': '',
'outSkus': '',
'random': random.random(),
'locationId': '19-1607-4773-0'
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.37",
"Accept": "application/json, text/javascript, */*; q=0.01",
"Referer": "https://cart.jd.com/cart.action",
"Host": "cart.jd.com",
"Content-Type": "application/x-www-form-urlencoded",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Encoding": "zh-CN,zh;q=0.9,ja;q=0.8",
"Origin": "https://cart.jd.com",
"Connection": "keep-alive"
}
resp = session.post(url, data=data, headers=headers)
logger.info('清空购物车')
if resp.status_code != requests.codes.OK:
print('Status: %u, Url: %s' % (resp.status_code, resp.url))
return False
return True
def select_all_cart_item():
url = "https://cart.jd.com/selectAllItem.action"
data = {
't': 0,
'outSkus': '',
'random': random.random()
}
resp = session.post(url, data=data)
if resp.status_code != requests.codes.OK:
print('Status: %u, Url: %s' % (resp.status_code, resp.url))
return False
return True
def normalModeAutoBuy(inStockSkuid):
for skuId in inStockSkuid:
if item_removed(skuId):
global submit_Time
submit_Time = int(time.time() * 1000)
logger.info('[%s]类型口罩有货啦!马上下单', skuId)
skuidUrl = 'https://item.jd.com/' + skuId + '.html'
if normalModeBuyMask(skuId):
message.send(skuidUrl, True)
sys.exit(1)
else:
message.send(skuidUrl, False)
else:
logger.info('[%s]类型口罩有货,但已下柜商品', skuId)
def fastModeAutoBuy(inStockSkuid):
for skuId in inStockSkuid:
global submit_Time
submit_Time = int(time.time() * 1000)
logger.info('[%s]类型口罩有货啦!马上下单', skuId)
skuidUrl = 'https://item.jd.com/' + skuId + '.html'
if fastModeBuyMask(skuId):
message.send(skuidUrl, True)
sys.exit(1)
else:
if item_removed(skuId):
message.send(skuidUrl, False)
else:
logger.info('[%s]商品已下柜,商品列表中踢出', skuId)
skuids.remove(skuId)
select_all_cart_item()
remove_item()
def check_Config():
global configMd5, configTime
nowMd5 = getconfigMd5()
configTime = time.time()
if not nowMd5 == configMd5:
logger.info('配置文件修改,重新读取文件')
getconfig()
configMd5 = nowMd5
def normalMode():
flag = 1
while (1):
try:
if flag == 1:
validate_cookies()
getUsername()
# 检测配置文件是否修改
if int(time.time()) - configTime >= 60:
check_Config()
# modelType
logger.info('第' + str(flag) + '次 ')
flag += 1
# 检测库存
inStockSkuid = check_stock(checksession, skuids, area)
# 下单任务
normalModeAutoBuy(inStockSkuid)
# 休眠模块
timesleep = random.randint(1, 3) / 10
time.sleep(timesleep)
# 校验是否还在登录模块
if flag % 100 == 0:
logger.info('校验是否还在登录')
validate_cookies()
except Exception as e:
print(traceback.format_exc())
time.sleep(10)
def fastMode():
flag = 1
while (1):
try:
if flag == 1:
validate_cookies()
getUsername()
select_all_cart_item()
remove_item()
# 检测配置文件修改
if int(time.time()) - configTime >= 600:
check_Config()
# modelType
logger.info('第' + str(flag) + '次 ')
flag += 1
# 检测库存
inStockSkuid = check_stock(checksession, skuids, area)
# 下单任务
fastModeAutoBuy(inStockSkuid)
# 休眠模块
timesleep = random.randint(1, 3) / 10
time.sleep(timesleep)
# 校验是否还在登录模块
if flag % 100 == 0:
logger.info('校验是否还在登录')
validate_cookies()
except Exception as e:
print(traceback.format_exc())
time.sleep(10)
# _setDNSCache()
if modelType == '2':
logger.info('V2版本当前模式[普通模式]')
normalMode()
elif modelType == '1':
logger.info('V2版本当前模式[极速模式]')
fastMode()