Skip to content

Commit 830cf5d

Browse files
committed
Updated documentation and cleaned code
1 parent b09433b commit 830cf5d

3 files changed

Lines changed: 33 additions & 21 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Get the ASIN from a URL:
5353

5454
Changelog
5555
-------------
56+
Version 2.0.2
57+
- Added some type hints.
58+
- Solved bug with images exception.
59+
5660
Version 2.0.1
5761
- Improved exception handling.
5862

amazon/paapi.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from paapi5_python_sdk.get_items_resource import GetItemsResource
77
from paapi5_python_sdk.partner_type import PartnerType
88
import time
9-
import logging
109
import re
1110

1211

@@ -48,7 +47,7 @@ class Class:
4847
"""Base class for creating the product instance."""
4948

5049

51-
def get_asin(url):
50+
def get_asin(url: str):
5251
"""Find the ASIN from a given URL.
5352
5453
Args:
@@ -57,14 +56,23 @@ def get_asin(url):
5756
Returns:
5857
string: Product ASIN. None if ASIN not found.
5958
"""
60-
if re.search("^[A-Z0-9]{10}$", url):
59+
# Return if url parameter already is the ASIN
60+
if re.search(r'^[A-Z0-9]{10}$', url):
6161
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)
62+
# Extract ASIN from URL searching for alphanumeric and 10 digits
63+
have_asin = re.search(r'(dp|gp/product)/([a-zA-Z0-9]{10})', url)
6464
return have_asin.group(2) if have_asin else None
6565

6666

6767
def parse_product(item):
68+
"""Parse item data and creates product instance.
69+
70+
Args:
71+
item (instance): The instance with the data from Amazon API.
72+
73+
Returns:
74+
instance: Product instance with parsed data.
75+
"""
6876
product = Class()
6977
product.raw_info = item
7078

@@ -269,18 +277,18 @@ def parse_product(item):
269277
except Exception:
270278
pass
271279
try:
272-
for x in product.images.variants.small:
273-
product.images.cropped.small.append(x.replace('_SL', '_AC'))
280+
for x in product.images.variants.small:
281+
product.images.cropped.small.append(x.replace('_SL', '_AC'))
274282
except Exception:
275283
pass
276284
try:
277-
for x in product.images.variants.medium:
278-
product.images.cropped.medium.append(x.replace('_SL', '_AC'))
285+
for x in product.images.variants.medium:
286+
product.images.cropped.medium.append(x.replace('_SL', '_AC'))
279287
except Exception:
280288
pass
281289
try:
282-
for x in product.images.variants.large:
283-
product.images.cropped.large.append(x.replace('.jpg', '._AC_.jpg'))
290+
for x in product.images.variants.large:
291+
product.images.cropped.large.append(x.replace('.jpg', '._AC_.jpg'))
284292
except Exception:
285293
pass
286294

@@ -522,10 +530,10 @@ class AmazonAPI:
522530
key (string): Your API key.
523531
secret (string): Your API secret.
524532
tag (string): The tag you want to use for the URL.
525-
country (string): Country code.
533+
country (string): Country code (AU, BR, CA, FR, DE, IN, IT, JP, MX, ES, TR, AE, UK, US).
526534
throttling (float, optional): Reduce this value to wait longer between API calls.
527535
"""
528-
def __init__(self, key, secret, tag, country, throttling=0.9):
536+
def __init__(self, key: str, secret: str, tag: str, country: str, throttling=0.9):
529537
self.key = key
530538
self.secret = secret
531539
self.tag = tag
@@ -540,9 +548,9 @@ def get_products(self, product_ids: [str, list], condition=Condition.ANY):
540548
"""Find product information for a specific product on Amazon.
541549
542550
Args:
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.
545-
condition (class, optional): Specify the product condition. Defaults to NEW.
551+
product_ids (string): One or more item ids like ASIN or product URL (max 10).
552+
Could be a string separated by comma or as a list.
553+
condition (class, optional): Specify the product condition. Defaults to ANY.
546554
547555
Returns:
548556
class instance: An instance of the class Product containing all the available
@@ -554,11 +562,11 @@ class instance: An instance of the class Product containing all the available
554562
secret_key=self.secret,
555563
host=self.host,
556564
region=self.region)
557-
558-
# clean up input data into a list stripping any extra white space
565+
566+
# Clean up input data into a list stripping any extra white space
559567
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
568+
569+
# Extract ASIN if supplied input is product URL and remove any duplicate ASIN
562570
asin_list = list(set([get_asin(x) for x in asin_or_url_list[:10]]))
563571

564572
product_resources = [

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='2.0.1',
8+
version='2.0.2',
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)