Skip to content

Commit 97654d3

Browse files
committed
Merge branch 'develop'
2 parents 9cb744b + 2ae997a commit 97654d3

File tree

5 files changed

+54
-25
lines changed

5 files changed

+54
-25
lines changed

amazon_paapi/api.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
A simple Python wrapper for the last version of the Amazon Product Advertising API.
44
"""
55

6+
from typing import List, Union
7+
import time
8+
69

710
from . import models
811
from .sdk.api.default_api import DefaultApi
912
from .errors import InvalidArgumentException
1013
from .helpers import arguments
1114
from .helpers import requests
1215
from .helpers.generators import get_list_chunks
13-
14-
from typing import List, Union
15-
import time
16+
from .helpers.items import sort_items
1617

1718

1819
class AmazonApi:
@@ -42,19 +43,22 @@ def __init__(self, key: str, secret: str, tag: str, country: models.Country, thr
4243
self._host = 'webservices.amazon.' + models.regions.DOMAINS[country]
4344
self._region = models.regions.REGIONS[country]
4445
self._marketplace = 'www.amazon.' + models.regions.DOMAINS[country]
45-
except KeyError:
46-
raise InvalidArgumentException('Country code is not correct')
46+
except KeyError as error:
47+
raise InvalidArgumentException('Country code is not correct') from error
4748

4849
self._api = DefaultApi(key, secret, self._host, self._region)
4950

5051

51-
def get_items(self,
52+
def get_items(
53+
self,
5254
items: Union[str, List[str]],
5355
condition: models.Condition = None,
5456
merchant: models.Merchant = None,
5557
currency_of_preference: str = None,
5658
languages_of_preference: List[str] = None,
57-
**kwargs) -> List[models.Item]:
59+
include_unavailable: bool = False,
60+
**kwargs
61+
) -> List[models.Item]:
5862

5963
"""Get items information from Amazon.
6064
@@ -69,6 +73,8 @@ def get_items(self,
6973
information should be returned. Expected currency code format is ISO 4217.
7074
languages_of_preference (``list[str]``, optional): Languages in order of preference in
7175
which the item information should be returned.
76+
include_unavailable (``bool``, optional): The returned list includes not available
77+
items. Not available items have the ASIN and item_info equals None. Defaults to False.
7278
kwargs (``dict``, optional): Any other arguments to be passed to the Amazon API.
7379
7480
Returns:
@@ -86,21 +92,22 @@ def get_items(self,
8692
'merchant': merchant,
8793
'currency_of_preference': currency_of_preference,
8894
'languages_of_preference': languages_of_preference
89-
})
95+
})
9096

9197
items_ids = arguments.get_items_ids(items)
9298
results = []
9399

94-
for asin_chunk in get_list_chunks(items_ids, chunk_size=10):
100+
for asin_chunk in get_list_chunks(list(set(items_ids)), chunk_size=10):
95101
request = requests.get_items_request(self, asin_chunk, **kwargs)
96102
self._throttle()
97103
items_response = requests.get_items_response(self, request)
98104
results.extend(items_response)
99105

100-
return results
106+
return sort_items(results, items_ids, include_unavailable)
101107

102108

103-
def search_items(self,
109+
def search_items(
110+
self,
104111
item_count: int = None,
105112
item_page: int = None,
106113
actor: str = None,
@@ -122,7 +129,8 @@ def search_items(self,
122129
min_reviews_rating: int = None,
123130
search_index: str = None,
124131
sort_by: models.SortBy = None,
125-
**kwargs) -> models.SearchResult:
132+
**kwargs
133+
) -> models.SearchResult:
126134
"""Searches for items on Amazon based on a search query. At least one of the following
127135
parameters should be specified: ``keywords``, ``actor``, ``artist``, ``author``,
128136
``brand`` or ``title``.
@@ -205,15 +213,17 @@ def search_items(self,
205213
return requests.get_search_items_response(self, request)
206214

207215

208-
def get_variations(self,
216+
def get_variations(
217+
self,
209218
asin: str,
210219
variation_count: int = None,
211220
variation_page: int = None,
212221
condition: models.Condition = None,
213222
currency_of_preference: str = None,
214223
languages_of_preference: List[str] = None,
215224
merchant: models.Merchant = None,
216-
**kwargs) -> models.VariationsResult:
225+
**kwargs
226+
) -> models.VariationsResult:
217227
"""Returns a set of items that are the same product, but differ according to a
218228
consistent theme, for example size and color. A variation is a child ASIN.
219229
@@ -260,10 +270,12 @@ def get_variations(self,
260270
return requests.get_variations_response(self, request)
261271

262272

263-
def get_browse_nodes(self,
273+
def get_browse_nodes(
274+
self,
264275
browse_node_ids: List[str],
265276
languages_of_preference: List[str] = None,
266-
**kwargs) -> List[models.BrowseNode]:
277+
**kwargs
278+
) -> List[models.BrowseNode]:
267279
"""Returns the specified browse node's information like name, children and ancestors.
268280
269281
Args:

amazon_paapi/helpers/arguments.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def get_items_ids(items: Union[str, List[str]]) -> List[str]:
1919

2020
if items_ids:
2121
return items_ids
22-
else:
23-
raise AsinNotFoundException('No ASIN codes have been found.')
22+
23+
raise AsinNotFoundException('No ASIN codes have been found.')
2424

2525

2626
def check_search_args(**kwargs):

amazon_paapi/helpers/items.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Module to manage items"""
2+
3+
from typing import List
4+
from .. import models
5+
6+
7+
def sort_items(items: List[models.Item], items_ids: List[str], include_unavailable: bool) -> List[models.Item]:
8+
sorted_items = []
9+
10+
for asin in items_ids:
11+
matches = list(filter(lambda item, asin=asin: item.asin == asin, items))
12+
if matches:
13+
sorted_items.append(matches[0])
14+
elif include_unavailable:
15+
sorted_items.append(models.Item(asin=asin))
16+
17+
return sorted_items

amazon_paapi/tools/asin.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"""Some useful tools."""
22

3-
from ..errors import AsinNotFoundException
43
import re
4+
from ..errors import AsinNotFoundException
55

66

77
def get_asin(text: str) -> str:
88
"""Returns the ASIN from a given text. Raises AsinNotFoundException on fail."""
99
# Return if text is an ASIN
10-
if re.search(r'^[A-Z0-9]{10}$', text):
11-
return text
10+
if re.search(r'^[a-zA-Z0-9]{10}$', text):
11+
return text.upper()
1212

1313
# Extract ASIN from URL searching for alphanumeric and 10 digits
1414
asin = re.search(r'(dp|gp/product|gp/aw/d|dp/product)/([a-zA-Z0-9]{10})', text)
1515
if asin:
16-
return asin.group(2)
17-
else:
18-
raise AsinNotFoundException('Asin not found: ' + text)
16+
return asin.group(2).upper()
17+
18+
raise AsinNotFoundException('Asin not found: ' + text)

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='4.1.1',
8+
version='4.2.0',
99
author='Sergio Abad',
1010
author_email='[email protected]',
1111
description='Amazon Product Advertising API 5.0 wrapper for Python',

0 commit comments

Comments
 (0)