Skip to content

Commit caef250

Browse files
authored
Added type hints and get_asin improvement
- Pluralize get_product and product_id as it accepts multiple ids and returns multiple products. - Added type hints to product_ids - Improved get_asin function - Added product_ids limit to max 10 per request, amazon accets maximum of 10 asins per call for GetItems request.
1 parent 20957eb commit caef250

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

amazon/paapi.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ def get_asin(url):
5656
Returns:
5757
string: Product ASIN. None if ASIN not found.
5858
"""
59-
split_url = url.split('?')[0].replace('?', '/').replace('&', '/').split('/')
60-
for c in list(reversed(split_url)):
61-
if len(c) == 10 and c.isalnum():
62-
return c
59+
if re.search("[A-Z0-9]{10}", url):
60+
return url
61+
# since asin is alphanumeric and 10 digit
62+
have_asin = re.search(r"(dp|gp/product)/([a-zA-Z0-9]{10})", url)
63+
return have_asin.group(2) if have_asin else None
6364

6465

6566
def parse_product(item):
@@ -534,12 +535,12 @@ def __init__(self, key, secret, tag, country, throttling=0.9):
534535
self.marketplace = 'www.amazon.' + DOMAINS[country]
535536
self.last_query_time = time.time()
536537

537-
def get_product(self, product_id, condition=Condition.ANY):
538+
def get_products(self, product_ids: [str, list], condition=Condition.ANY):
538539
"""Find product information for a specific product on Amazon.
539540
540541
Args:
541-
product_id (string): Product ASIN or URL. You can send multiple products separated
542-
by commas.
542+
product_ids (string): One or more ItemIds like ASIN that uniquely identify an item or product URL. (Max 10)
543+
Seperated by comma or as a list.
543544
condition (class, optional): Specify the product condition. Defaults to NEW.
544545
545546
Returns:
@@ -552,11 +553,12 @@ class instance: An instance of the class Product containing all the available
552553
secret_key=self.secret,
553554
host=self.host,
554555
region=self.region)
555-
556-
product_id = product_id.split(',')
557-
asin_list = []
558-
for x in product_id:
559-
asin_list.append(get_asin(x.strip()))
556+
557+
# clean up input data into a list stripping any extra white space
558+
asin_or_url_list = [x.strip() for x in product_ids.split(",")] if isinstance(product_ids, str) else product_ids
559+
560+
# extract asin if supplied input is product url and remove any duplicate asin from cleaned list
561+
asin_list = list(set([get_asin(x) for x in asin_or_url_list[:10]]))
560562

561563
product_resources = [
562564
GetItemsResource.BROWSENODEINFO_BROWSENODES,

0 commit comments

Comments
 (0)