-
Notifications
You must be signed in to change notification settings - Fork 3
/
stockx-inventory-updater.py
164 lines (145 loc) · 5.9 KB
/
stockx-inventory-updater.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
import requests
import os
from openpyxl import load_workbook
from forex_python.converter import CurrencyRates
from datetime import datetime
import json
import time
import collections
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
sales_db = collections.defaultdict(dict)
productname_db = collections.defaultdict()
def load_config():
with open(os.path.join(__location__, "config.json")) as json_file:
data = json.load(json_file)
return data
def resource_path(relative_path):
try:
base_path = sys._MEIPASS # pylint: disable=no-member
except Exception:
base_path = os.path.dirname(__file__)
return os.path.join(base_path, relative_path)
def main():
config = load_config()
if config["currency"] == "USD":
rate = 1
else:
c = CurrencyRates()
rate = c.get_rate('USD', config["currency"])
wb = load_workbook(resource_path("./stock_book.xlsx"))
ws = wb['Sheet1']
session = requests.Session()
session.headers.update({
'content-type': 'application/x-www-form-urlencoded',
'user-agent': config["user-agent"],
'accept': '*/*',
'accept-ending': 'gzip, deflate, br',
'connection': 'keep-alive'
})
# session.get("https://stockx.com/api/browse?&_search=EF2367&dataType=product")
# print(session.cookies)
row_num = config["start-row"]
for row in ws.iter_rows(min_row=config["start-row"]):
if row[0].value == None:
break
print(f'Fetching row {row_num}: {row[0].value}')
sku = row[0].value
size = str(row[2].value)
if sales_db[sku].get(size) is not None: #if fetched before
row[1].value = productname_db[sku]
row[9].value = "Stockx"
row[9].hyperlink = f'https://stockx.com/{productname_db[sku]}'
sales = sales_db[sku][size]
print('Skipping, sales exist in database')
row[4].value = round((sales["last"] * rate), 2)
row[5].value = round((sales["average"] * rate), 2)
row[6].value = round((sales["highest_bid"] * rate), 2)
row[7].value = round((sales["lowest_ask"] * rate), 2)
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %I:%M:%S %p")
row[10].value = dt_string
row_num += 1
else:
urlkey = search_product(sku, session)
if urlkey is None:
break
row[9].value = "Stockx"
row[9].hyperlink = f'https://stockx.com/{urlkey}'
result = product_info(urlkey, size, session)
if result is None:
break
row[1].value = result["title"]
productname_db[sku] = row[1].value
if result["uuid"] is None:
print("Size not found, check if W or Y is needed.")
break
sales = get_sales(result["uuid"], sku, size, session)
if sales is None:
break
sales["highest_bid"] = result["highest_bid"]
sales["lowest_ask"] = result["lowest_ask"]
sales_db[sku][size] = sales
row[4].value = round((sales["last"] * rate), 2)
row[5].value = round((sales["average"] * rate), 2)
row[6].value = round((sales["highest_bid"] * rate), 2)
row[7].value = round((sales["lowest_ask"] * rate), 2)
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %I:%M:%S %p")
row[10].value = dt_string
row_num += 1
wb.save(resource_path("./stock_book_output.xlsx"))
def search_product(sku, session):
url = f'https://stockx.com/api/browse?&_search={sku}&dataType=product'
req = session.get(url)
while req.status_code != 200:
print(f'Error {req.status_code} at search_product')
input("Please solve captcha at https://www.stockx.com on an incognito window and press enter")
req = session.get(url)
else:
data = req.json()
urlkey = data["Products"][0]["urlKey"]
return urlkey
def product_info(urlkey, size, session):
url = f'https://stockx.com/api/products/{urlkey}?includes=market¤cy=USD'
req = session.get(url)
while req.status_code != 200:
print(f'Error {req.status_code} at product_info')
input("Please solve captcha at https://www.stockx.com on an incognito window and press enter")
req = session.get(url)
else:
data = req.json()
result = {}
result["title"] = data["Product"]["title"]
result["uuid"] = None
for key in data["Product"]["children"]:
if (data["Product"]["children"][key]["shoeSize"]) == size:
uuid = data["Product"]["children"][key]["uuid"]
highest_bid = data["Product"]["children"][key]["market"]["highestBid"]
lowest_ask = data["Product"]["children"][key]["market"]["lowestAsk"]
result["uuid"] = uuid
result["highest_bid"] = highest_bid
result["lowest_ask"] = lowest_ask
break
return result
def get_sales(uuid, sku, size, session):
url = f'https://stockx.com/api/products/{uuid}/activity?state=480¤cy=USD&limit=3&page=1&sort=createdAt&order=DESC'
req = session.get(url)
while req.status_code != 200:
print(f'Error {req.status_code} at get_sales')
input("Please solve captcha at https://www.stockx.com on an incognito window and press enter")
req = session.get(url)
else:
data = req.json()
amount = 0
limit = 0
sales = {}
for i in data["ProductActivity"]:
if limit == 0:
sales["last"] = i["localAmount"]
amount += i["localAmount"]
limit += 1
average = amount / limit
sales["average"] = round(average, 2)
return sales
if __name__ == '__main__':
main()