Skip to content

Commit eb0c22b

Browse files
committed
Merge branch 'patch-1'
2 parents 20957eb + 0dc182d commit eb0c22b

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

amazon/paapi.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from paapi5_python_sdk.partner_type import PartnerType
88
import time
99
import logging
10+
import re
1011

1112

1213
REGIONS = {
@@ -56,10 +57,11 @@ def get_asin(url):
5657
Returns:
5758
string: Product ASIN. None if ASIN not found.
5859
"""
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
60+
if re.search("^[A-Z0-9]{10}$", url):
61+
return url
62+
# since asin is alphanumeric and 10 digit
63+
have_asin = re.search(r"(dp|gp/product)/([a-zA-Z0-9]{10})", url)
64+
return have_asin.group(2) if have_asin else None
6365

6466

6567
def parse_product(item):
@@ -534,12 +536,12 @@ def __init__(self, key, secret, tag, country, throttling=0.9):
534536
self.marketplace = 'www.amazon.' + DOMAINS[country]
535537
self.last_query_time = time.time()
536538

537-
def get_product(self, product_id, condition=Condition.ANY):
539+
def get_products(self, product_ids: [str, list], condition=Condition.ANY):
538540
"""Find product information for a specific product on Amazon.
539541
540542
Args:
541-
product_id (string): Product ASIN or URL. You can send multiple products separated
542-
by commas.
543+
product_ids (string): One or more ItemIds like ASIN that uniquely identify an item or product URL. (Max 10)
544+
Seperated by comma or as a list.
543545
condition (class, optional): Specify the product condition. Defaults to NEW.
544546
545547
Returns:
@@ -552,11 +554,12 @@ class instance: An instance of the class Product containing all the available
552554
secret_key=self.secret,
553555
host=self.host,
554556
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()))
557+
558+
# clean up input data into a list stripping any extra white space
559+
asin_or_url_list = [x.strip() for x in product_ids.split(",")] if isinstance(product_ids, str) else product_ids
560+
561+
# extract asin if supplied input is product url and remove any duplicate asin from cleaned list
562+
asin_list = list(set([get_asin(x) for x in asin_or_url_list[:10]]))
560563

561564
product_resources = [
562565
GetItemsResource.BROWSENODEINFO_BROWSENODES,

0 commit comments

Comments
 (0)