-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrabsku_thread.py
More file actions
63 lines (46 loc) · 1.33 KB
/
grabsku_thread.py
File metadata and controls
63 lines (46 loc) · 1.33 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
55
56
57
58
59
60
61
62
63
import requests
import elementtree.ElementTree as ET
import xml.parsers.expat
import csv
import threading
bcinvn = {}
class Sku_List(threading.Thread):
def __init__(self, url, topLevel, name='SKUS'):
threading.Thread.__init__(self)
self.name = name
self.url = url
self.topLevel = topLevel
self.cnt = 0
self.invn = {}
def run(self):
SKUS_TO_GRAB = True
while SKUS_TO_GRAB is True:
self.cnt = self.cnt + 1
payload = {'limit': '200', 'page' : self.cnt}
try:
r = requests.get('STORE URL%s' %self.url, params=payload, auth=('API ID', 'API PASSWORD'))
tree = ET.fromstring(r.content)
except xml.parsers.expat.ExpatError, e:
SKUS_TO_GRAB = False
else:
for product in tree.findall('%s' %self.topLevel):
if product.find('sku').text is not None:
itmsku = int(product.find('sku').text)
qty = int(product.find('inventory_level').text)
self.invn[itmsku] = qty
return self.invn
threads = []
skuThread = Sku_List('products/skus', 'sku', 'skus')
productThread = Sku_List('products', 'product', 'products')
skuThread.start()
threads.append(skuThread)
productThread.start()
threads.append(productThread)
print 'Threads Declared'
for t in threads:
t.join()
print 'Exiting Threads'
bcinvn.update(skuThread.invn)
bcinvn.update(productThread.invn)
print 'SKUS GRABBED'
print len(bcinvn)