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
/
monitor.py
117 lines (98 loc) · 4.28 KB
/
monitor.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
import logging
from datetime import datetime
from json import JSONDecodeError
import time
from threading import Thread
import glb
isInStockApiParams = []
def init():
_count = 100
_currIndex = 0
while True:
# 分割商品列表
_currItems = list(glb.config['items'].keys())[_currIndex:_currIndex + _count]
if len(_currItems) != 0:
isInStockApiParams.append({
'skuIds': ','.join([itemId for itemId in _currItems]),
'area': glb.accountList[0].config['areaId'], # 使用第1个账户的
'type': 'getstocks'})
_currIndex += _count
continue
else:
break
def checkLogin():
while True:
for _account in glb.accountList:
resp = _account.checkLogin()
if resp is None:
continue
if not resp.json()['Identity']['IsAuthenticated']:
logging.error('{} 未登录'.format(_account.id))
time.sleep(60)
def monitor():
logging.info('### 检查是否为抢购商品 ###')
_checkSnappingUp()
logging.info('### 开始监控库存 ###')
for isInStockApiParam in isInStockApiParams:
Thread(target=_monitor, args=(isInStockApiParam,)).start()
Thread(target=checkSnappingUp).start()
def checkSnappingUp():
while True:
_checkSnappingUp()
def _checkSnappingUp():
for itemId in glb.runTimeItems.keys():
resp = glb.request(
'检查是否为抢购商品', None, glb.GET, 'https://yushou.jd.com/youshouinfo.action',
params={'sku': itemId},
headers={'referer': 'https://item.jd.com/{}.html'.format(itemId),
'cookie': None},
logLvl={glb.successLogLvl: logging.DEBUG, # 请求成功的日志等级
glb.timeoutLogLvl: logging.DEBUG, # 请求超时的日志等级
glb.tooManyFailureLogLvl: logging.DEBUG}, # 过多失败的日志等级
timeout=3)
if resp is None or resp.text[:1] != '{':
continue
if resp.text != '{"error":"pss info is null"}':
if not glb.runTimeItems[itemId][glb.isSnappingUp]:
logging.warning('{} 是抢购商品, 不自动购买'.format(itemId))
glb.runTimeItems[itemId][glb.isSnappingUp] = True
else:
if glb.runTimeItems[itemId][glb.isSnappingUp]:
logging.warning('{} 不是抢购商品, 会自动购买'.format(itemId))
glb.runTimeItems[itemId][glb.isSnappingUp] = False
if canBuy(itemId):
Thread(target=buy, args=(itemId,)).start()
def _monitor(isInStockApiParam):
while True:
resp = glb.request('监控库存', None, glb.GET, 'https://c0.3.cn/stocks',
params=isInStockApiParam, headers={'cookie': None}, redirect=False,
logLvl={glb.successLogLvl: logging.DEBUG, # 请求成功的日志等级
glb.timeoutLogLvl: logging.DEBUG, # 请求超时的日志等级
glb.tooManyFailureLogLvl: logging.DEBUG}, # 过多失败的日志等级
timeout=1.5)
if resp is not None:
try:
for itemId, value in resp.json().items():
if value['skuState'] == 0 or value['StockState'] not in (33, 40):
glb.runTimeItems[itemId][glb.isInStock] = False
else:
glb.runTimeItems[itemId][glb.isInStock] = True
if canBuy(itemId):
Thread(target=buy, args=(itemId,)).start()
except JSONDecodeError:
continue
def canBuy(itemId):
item = glb.runTimeItems[itemId]
if item[glb.isInStock] and not item[glb.isSnappingUp]:
logging.warning('{} 有货 且不是抢购商品'.format(itemId))
return True
else:
return False
def buy(itemId):
idsOfAccountsWantToBuy = glb.config['items'][itemId]
if len(idsOfAccountsWantToBuy) == 0:
for account in glb.accountList:
Thread(target=account.buy, args=(itemId,)).start()
else:
for _id in idsOfAccountsWantToBuy:
Thread(target=glb.accountDict[_id].buy, args=(itemId,)).start()