Skip to content

Commit

Permalink
Fix get_single_item_stock API change
Browse files Browse the repository at this point in the history
  • Loading branch information
tychxn committed Jan 24, 2020
1 parent fdca5bf commit 7d63044
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions jd_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(self):
self.sess = requests.session()

self.item_cat = dict()
self.item_vender_ids = dict()

self.risk_control = ''
self.eid = global_config.get('config', 'eid') or DEFAULT_EID
Expand Down Expand Up @@ -409,12 +410,17 @@ def get_single_item_stock(self, sku_id, area):
:return: 库存状态元祖:(33, '现货') (34, '无货') (36, '采购中') (40, '可配货')
"""
cat = self.item_cat.get(sku_id)
vender_id = self.item_vender_ids.get(sku_id)
if not cat:
page = self._get_item_detail_page(sku_id)
match = re.search(r'cat: \[(.*?)\]', page.text)
cat = match.group(1)
self.item_cat[sku_id] = cat

match = re.search(r'venderId:(\d*?),', page.text)
vender_id = match.group(1)
self.item_vender_ids[sku_id] = vender_id

url = 'https://c0.3.cn/stock'
payload = {
'skuId': sku_id,
Expand All @@ -425,7 +431,7 @@ def get_single_item_stock(self, sku_id, area):
'callback': 'jQuery{}'.format(random.randint(1000000, 9999999)),
'extraParam': '{"originid":"1"}', # get error stock state without this param
'cat': cat, # get 403 Forbidden without this param (obtained from the detail page)
# 'venderId': '' # won't return seller information without this param (can be ignored)
'venderId': vender_id # return seller information with this param (can't be ignored)
}
headers = {
'User-Agent': USER_AGENT,
Expand All @@ -434,8 +440,8 @@ def get_single_item_stock(self, sku_id, area):
resp = requests.get(url=url, params=payload, headers=headers)

resp_json = parse_json(resp.text)
stock_state = resp_json['StockState'] # 33 -- 现货 34 -- 无货 40 -- 可配货
stock_state_name = resp_json['StockStateName']
stock_state = resp_json['stock']['StockState'] # 33 -- 现货 34 -- 无货 40 -- 可配货
stock_state_name = resp_json['stock']['StockStateName']
return stock_state, stock_state_name # (33, '现货') (34, '无货') (36, '采购中') (40, '可配货')

def get_multi_item_stock(self, sku_ids, area):
Expand Down Expand Up @@ -820,8 +826,9 @@ def submit_order_by_stock(self, sku_ids, area, interval=3):
if self.submit_order():
break
else:
print(get_current_time(), '【%s】无货,准备下一次查询' % sku_ids)
time.sleep(interval)
print(get_current_time(), '【%s】无货,准备下一次查询 (%ss)' % (sku_ids, interval))

time.sleep(interval)

def get_order_info(self, unpaid=True):
"""查询订单信息
Expand Down

0 comments on commit 7d63044

Please sign in to comment.