-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbcorders.py
More file actions
54 lines (41 loc) · 1.34 KB
/
bcorders.py
File metadata and controls
54 lines (41 loc) · 1.34 KB
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
import requests
import elementtree.ElementTree as ET
order_items = []
filename = 'lastupdate.txt'
target = open(filename, 'r')
last_id = target.read()
target.close()
payload = {'min_id': last_id, 'limit' : '250'}
print "Importing Orders Since Last Update"
r = requests.get('https://<STORE URL>/api/v2/orders', params=payload, auth=(<USER ID>, <API KEY>))
tree = ET.fromstring(r.content)
for order in tree.findall('order'):
payment_status = order.find('payment_status').text
if payment_status == 'captured':
order_id = order.find('id').text
order_items.append( order_id )
max = len(order_items)
cnt = 0
order_inventory = {}
while cnt < max:
r = requests.get('https://<STORE URL>/api/v2/orders/%s/products.xml' % order_items[cnt], auth=(<USER ID>, <API KEY>))
tree = ET.fromstring(r.content)
for product in tree.findall('product'):
if (product.find('sku').text) is None:
cnt = cnt + 1
else:
itmsku = int(product.find('sku').text)
qty = int(product.find('quantity').text)
if itmsku in order_inventory:
order_inventory[itmsku] = order_inventory[itmsku] + qty
else:
order_inventory[itmsku] = qty
cnt = cnt + 1
cnt = cnt - 1
newest_order = order_items[cnt]
print "Importing Orders Since Last Update COMPLETE"
filename = 'lastupdate.txt'
target = open(filename, 'w')
target.truncate()
target.write(newest_order)
target.close