-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproduct.py
72 lines (63 loc) · 2.89 KB
/
product.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
import config
import requests
import json
# retrieve ticker symbol of requested item
def get_ticker_symbol(requested_item):
search_request_payload = {
"params": "query={0}&hitsPerPage=20&facets=*".format(requested_item['title'])
}
ticker_symbol = None
try:
search_result = requests.post(config.STOCKX_SEARCH_ENDPOINT,
headers=config.REQUEST_HEADERS,
params=config.SEARCH_REQUEST_PARAMS,
json=search_request_payload,).json()['hits']
if search_result:
item = search_result[0]
ticker_symbol = item['ticker_symbol']
else:
print(f"{requested_item['title']} could not be found on StockX.")
print("Please check and make sure the item name matches the item found on StockX.\n")
except Exception as e:
print(f"Failed retrieving ticker symbol for {requested_item['title']}:")
print(f"{e}\n")
return ticker_symbol
# retrieve prices of requested item
def get_prices(requested_item):
if requested_item['ticker_symbol']:
session = requests.Session()
keyword = requested_item['title']
URL = f"{config.STOCKX_PRODUCT_ENDPOINT}{keyword}"
product_info = None
try:
search_result = session.get(URL, headers=config.REQUEST_HEADERS)
if search_result.status_code == 200:
search_result = search_result.json()
search_result_products = search_result['Products']
else:
print(f"Failed retrieving information for {requested_item['title']}:")
print("Timed out\n")
return
except Exception as e:
print(f"Failed retrieving information for {requested_item['title']}:")
print(f"{e}\n")
return
for product in search_result_products:
if (product['tickerSymbol'] == requested_item['ticker_symbol']
and product['shoeSize'] == requested_item['size']):
product_info = {
"title": product['title'],
"actual_size": product['shoeSize'],
"lowest_ask_price": product['market']['lowestAsk'],
"highest_bid_price": product['market']['highestBid'],
"last_sale_price": product['market']['lastSale']
}
print(f"Product: {product_info['title']}\nSize: {product_info['actual_size']}")
break
if product_info:
return product_info
else:
print(f"{requested_item['title']} could not be found on StockX.")
print("Please check and make sure the item name, ticker symbol, and size matches the item found on StockX.\n")
else:
print("Missing StockX ticker symbol\n")