This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
account.py
124 lines (114 loc) · 5.93 KB
/
account.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
import logging
import re
import time
import requests
import glb
import monitor
class Account:
def __init__(self, id, config):
self.id = id
self.config = config['config']
self.sess = requests.Session()
self.sess.cookies.update(config['cookies'])
self.sess.headers.update(glb.reqHeaders)
self.isBuying = False
def checkLogin(self):
return glb.request(
'检测登录', self.sess, glb.GET, 'https://passport.jd.com/loginservice.aspx?method=Login',
headers={'Referer': 'https://www.jd.com/'},
logLvl={glb.successLogLvl: logging.DEBUG,
glb.timeoutLogLvl: logging.DEBUG}, timeout=5)
def buy(self, itemId):
while True:
if not monitor.canBuy(itemId):
return
if not self.isBuying:
break
self.isBuying = True
success = [False]
logging.warning('开始购买 ({})'.format(', '.join((self.id, itemId))))
try:
# 添加到购物车
if glb.request(
'添加到购物车 ({})'.format(', '.join((self.id, itemId))), self.sess, glb.GET,
'https://cart.jd.com/gate.action', params={'pid': itemId, 'pcount': 1, 'ptype': 1},
redirect=False, logLvl={glb.defaultLogLvl: logging.ERROR,
glb.redirectLogLvl: logging.DEBUG}, timeout=3) is None:
return
logging.warning('添加到购物车 ({}) 成功'.format(', '.join((self.id, itemId))))
def getOrderInfoCheck(_resp, args):
if re.search('showCheckCode" value="(.+)"', _resp.text).group(1) == 'true':
logging.warning('结算 ({}) 需要通过图形验证码'.format(', '.join((args[0].id, args[1]))))
time.sleep(1)
return True
return False
# 结算
resp = glb.request(
'结算 ({})'.format(', '.join((self.id, itemId))), self.sess, glb.GET,
'https://trade.jd.com/shopping/order/getOrderInfo.action',
checkFuc=getOrderInfoCheck, args=(self, itemId),
redirect=False, logLvl={glb.defaultLogLvl: logging.ERROR}, timeout=3)
if resp is None:
return
riskControl = re.search('riskControl" value="(.+?)"', resp.text).group(1)
def submitOrderCheck(_resp, args):
respObj = _resp.json()
if respObj['resultCode'] in (60123, 600157):
logging.error('提交订单 ({}) 失败 (message: {})'.format(
', '.join((args[0].id, args[1])), respObj['message']))
return False
elif respObj['resultCode'] == 600158:
logging.error('提交订单 ({}) 失败 (无货)'.format(', '.join((args[0].id, args[1]))))
glb.runTimeItems[args[1]][glb.isInStock] = False
return False
elif respObj['resultCode'] == 60017:
logging.error('提交订单 ({}) 失败 (请求过于频繁), 睡眠5s'.format(', '.join((args[0].id, args[1]))))
time.sleep(5)
return True
elif respObj['success']:
logging.error('\n\n\n提交订单 ({}) 成功!!!!!!!!!!!!!!!!!!!\n\n\n'.format(
', '.join((args[0].id, args[1]))))
args[2][0] = True
return False
else:
logging.error('提交订单 ({}) 失败 ({})'.format(
', '.join((args[0].id, args[1])), respObj))
return False
# 提交订单
if glb.request(
'提交订单 ({})'.format(', '.join((self.id, itemId))), self.sess, glb.POST,
'https://trade.jd.com/shopping/order/submitOrder.action',
headers={'Origin': 'https://trade.jd.com',
'Referer': 'https://trade.jd.com/shopping/order/getOrderInfo.action'},
data={
'overseaPurchaseCookies': '',
'vendorRemarks': '[]',
'submitOrderParam.sopNotPutInvoice': 'false',
'submitOrderParam.trackID': 'TestTrackId',
'submitOrderParam.ignorePriceChange': '0',
'submitOrderParam.btSupport': '0',
'submitOrderParam.eid': self.config['eid'], # https://gia.jd.com/fcf.html
'submitOrderParam.fp': self.config['fp'],
'riskControl': riskControl,
'submitOrderParam.jxj': 1,
'submitOrderParam.trackId': self.config['trackId'],
# 'submitOrderParam.isBestCoupon': 1, #
# 'submitOrderParam.needCheck': 1, #
},
checkFuc=submitOrderCheck, args=(self, itemId, success),
logLvl={glb.defaultLogLvl: logging.ERROR}, timeout=3) is None:
return
finally:
# 失败后删除商品
if not success[0]:
glb.request(
'从购物车删除 ({})'.format(', '.join((self.id, itemId))), self.sess, glb.POST,
'https://cart.jd.com/removeSkuFromCart.action',
data={'venderId': '8888', 'pid': itemId, 'ptype': '1', 'packId': '0',
'targetId': '0', 't': '0', 'outSkus': '', 'random': '0.3794799431176733',
'locationId': self.config['areaId']},
headers={'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://cart.jd.com',
'Referer': 'https://cart.jd.com/cart.action'},
timeout=5)
self.isBuying = False