Skip to content

Commit 7338e13

Browse files
committed
Improved throttling and now possible to disable it
1 parent 33e2227 commit 7338e13

4 files changed

Lines changed: 29 additions & 20 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ Usage guide
6565

6666
**Throttling:**
6767

68-
Throttling value must be `0 < value <= 1`. This value throttles requests to a maximum of one request per `1 / value` seconds. Note that this value is a per-worker throttling, so applications with multiple workers may make more requests per second. Throttling value is [set by default](https://github.com/sergioteula/python-amazon-paapi/blob/master/amazon/paapi.py#L36) to `0.8` or one request per 1.25 seconds.
68+
Throttling value must be `greater than 0` or `False` to disable it. This value throttles requests to a maximum of one request every `1 / value` seconds. Note that this value is a per-worker throttling, so applications with multiple workers may make more requests per second. Throttling value is [set by default](https://github.com/sergioteula/python-amazon-paapi/blob/master/amazon/paapi.py#L36) to `0.8` or one request every 1.25 seconds.
6969

7070
amazon = AmazonAPI(KEY, SECRET, TAG, COUNTRY, throttling=0.5) # Max one request every two seconds
71+
amazon = AmazonAPI(KEY, SECRET, TAG, COUNTRY, throttling=False) # Unlimited requests per second
7172

7273
Changelog
7374
-------------

amazon/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""Amazon Product Advertising API wrapper for Python"""
22

3-
__version__ = '3.0.2'
3+
__version__ = '3.1.0'
44
__author__ = 'Sergio Abad'

amazon/paapi.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,24 @@ class AmazonAPI:
3030
tag (str): The tag you want to use for the URL.
3131
country (str): Country code. Use one of the following:
3232
AU, BR, CA, FR, DE, IN, IT, JP, MX, ES, TR, AE, UK, US.
33-
throttling (float, optional): Reduce this value to wait longer
34-
between API calls.
33+
throttling (float, optional): It should be greater than 0 or False to disable throttling.
34+
This value determines wait time between API calls.
3535
"""
3636
def __init__(self, key: str, secret: str, tag: str, country: str, throttling=0.8):
3737
self.key = key
3838
self.secret = secret
3939
self.tag = tag
40-
if 1 >= throttling > 0:
41-
self.throttling = throttling
42-
elif throttling <= 0:
43-
raise AmazonException('ValueError', 'Throttling should be greater than 0')
44-
elif throttling > 1:
45-
raise AmazonException('ValueError', 'Throttling should be 1 or less')
40+
try:
41+
if throttling is True:
42+
raise ValueError
43+
elif throttling is False:
44+
self.throttling = False
45+
else:
46+
self.throttling = float(throttling)
47+
if not self.throttling > 0:
48+
raise ValueError
49+
except ValueError:
50+
raise AmazonException('ValueError', 'Throttling should be False or greater than 0')
4651
self.country = country
4752
try:
4853
self.host = 'webservices.amazon.' + DOMAINS[country]
@@ -101,9 +106,10 @@ def get_products(self, product_ids: [str, list], condition='Any', merchant='All'
101106
for x in range(3):
102107
try:
103108
# Wait before doing the request
104-
wait_time = 1 / self.throttling - (time.time() - self.last_query_time)
105-
if wait_time > 0:
106-
time.sleep(wait_time)
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)
107113
self.last_query_time = time.time()
108114

109115
# Send the request and create results
@@ -260,9 +266,10 @@ def search_products(self, item_count=10, item_page=1, items_per_page=10, keyword
260266
for x in range(3):
261267
try:
262268
# Wait before doing the request
263-
wait_time = 1 / self.throttling - (time.time() - self.last_query_time)
264-
if wait_time > 0:
265-
time.sleep(wait_time)
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)
266273
self.last_query_time = time.time()
267274

268275
# Send the request and create results
@@ -351,9 +358,10 @@ def get_variations(self, asin, item_count=10, item_page=1, items_per_page=10, co
351358
for x in range(3):
352359
try:
353360
# Wait before doing the request
354-
wait_time = 1 / self.throttling - (time.time() - self.last_query_time)
355-
if wait_time > 0:
356-
time.sleep(wait_time)
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)
357365
self.last_query_time = time.time()
358366

359367
# Send the request and create results

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='python-amazon-paapi',
8-
version='3.0.2',
8+
version='3.1.0',
99
author='Sergio Abad',
1010
author_email='sergio.abad@bytelix.com',
1111
description='Amazon Product Advertising API 5.0 wrapper for Python',

0 commit comments

Comments
 (0)