-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskutofile.py
More file actions
35 lines (27 loc) · 734 Bytes
/
skutofile.py
File metadata and controls
35 lines (27 loc) · 734 Bytes
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
import requests
import elementtree.ElementTree as ET
import xml.parsers.expat
import csv
cnt = 1
payload = {'limit': '200', 'page' : cnt}
inventory = {}
while cnt != -1:
try:
r = requests.get('https://STORE URL/api/v2/products/skus', params=payload, auth=('USER ID', 'API TOKEN'))
tree = ET.fromstring(r.content)
except xml.parsers.expat.ExpatError, e:
cnt = -1
else:
for sku in tree.findall('sku'):
itmsku = sku.find('sku').text
qty = sku.find('inventory_level').text
inventory[itmsku] = qty
cnt = cnt + 1
payload = {'limit': '200', 'page' : cnt}
finally:
print len(inventory)
with open('skus.csv', 'w+') as f:
w = csv.writer(f)
for row in inventory.iteritems():
w.writerow(row)
f.close()