Skip to content

Commit fbe1318

Browse files
committed
Added throttle as method
1 parent 048e4b3 commit fbe1318

1 file changed

Lines changed: 11 additions & 22 deletions

File tree

amazon/paapi.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(self, key: str, secret: str, tag: str, country: str, throttling=0.8
4444
self.throttling = False
4545
else:
4646
self.throttling = float(throttling)
47-
if not self.throttling > 0:
47+
if self.throttling <= 0:
4848
raise ValueError
4949
except ValueError:
5050
raise AmazonException('ValueError', 'Throttling should be False or greater than 0')
@@ -59,6 +59,13 @@ def __init__(self, key: str, secret: str, tag: str, country: str, throttling=0.8
5959
self.api = DefaultApi(access_key=self.key, secret_key=self.secret, host=self.host,
6060
region=self.region)
6161

62+
def _throttle(self):
63+
if self.throttling:
64+
wait_time = 1 / self.throttling - (time.time() - self.last_query_time)
65+
if wait_time > 0:
66+
time.sleep(wait_time)
67+
self.last_query_time = time.time()
68+
6269
def get_products(self, product_ids: [str, list], condition='Any', merchant='All',
6370
async_req=False):
6471
"""Find product information for multiple products on Amazon.
@@ -105,14 +112,8 @@ def get_products(self, product_ids: [str, list], condition='Any', merchant='All'
105112

106113
for x in range(3):
107114
try:
108-
# Wait before doing the request
109-
if self.throttling:
110-
wait_time = 1 / self.throttling - (time.time() - self.last_query_time)
111-
if wait_time > 0:
112-
time.sleep(wait_time)
113-
self.last_query_time = time.time()
114-
115115
# Send the request and create results
116+
self._throttle()
116117
if async_req:
117118
thread = self.api.get_items(request, async_req=True)
118119
response = thread.get()
@@ -265,14 +266,8 @@ def search_products(self, item_count=10, item_page=1, items_per_page=10, keyword
265266

266267
for x in range(3):
267268
try:
268-
# Wait before doing the request
269-
if self.throttling:
270-
wait_time = 1 / self.throttling - (time.time() - self.last_query_time)
271-
if wait_time > 0:
272-
time.sleep(wait_time)
273-
self.last_query_time = time.time()
274-
275269
# Send the request and create results
270+
self._throttle()
276271
if async_req:
277272
thread = self.api.search_items(request, async_req=True)
278273
response = thread.get()
@@ -357,14 +352,8 @@ def get_variations(self, asin, item_count=10, item_page=1, items_per_page=10, co
357352

358353
for x in range(3):
359354
try:
360-
# Wait before doing the request
361-
if self.throttling:
362-
wait_time = 1 / self.throttling - (time.time() - self.last_query_time)
363-
if wait_time > 0:
364-
time.sleep(wait_time)
365-
self.last_query_time = time.time()
366-
367355
# Send the request and create results
356+
self._throttle()
368357
if async_req:
369358
thread = self.api.get_variations(request, async_req=True)
370359
response = thread.get()

0 commit comments

Comments
 (0)