|
9 | 9 | from paapi5_python_sdk.get_items_request import GetItemsRequest |
10 | 10 | from paapi5_python_sdk.search_items_request import SearchItemsRequest |
11 | 11 | from paapi5_python_sdk.get_variations_request import GetVariationsRequest |
| 12 | +from paapi5_python_sdk.get_browse_nodes_request import GetBrowseNodesRequest |
12 | 13 | from paapi5_python_sdk.partner_type import PartnerType |
13 | 14 | from paapi5_python_sdk.rest import ApiException |
14 | 15 |
|
15 | 16 | from amazon.constant import DOMAINS, REGIONS, CONDITION |
16 | 17 | from amazon.constant import PRODUCT_RESOURCES, SEARCH_RESOURCES, VARIATION_RESOURCES |
| 18 | +from amazon.constant import BROWSE_RESOURCES |
17 | 19 | from amazon.exception import AmazonException |
18 | | -from amazon.parse import parse_product |
| 20 | +from amazon.parse import parse_product, AmazonBrowseNode, parse_browsenode |
19 | 21 | from amazon.tools import get_asin, chunks |
20 | 22 |
|
21 | 23 | import time |
@@ -387,3 +389,56 @@ def get_variations(self, asin, item_count=10, item_page=1, items_per_page=10, co |
387 | 389 | return results |
388 | 390 | else: |
389 | 391 | return None |
| 392 | + |
| 393 | + def get_browsenodes(self, browse_nodes, async_req=False): |
| 394 | + """Get browse nodes information from Amazon. |
| 395 | +
|
| 396 | + Args: |
| 397 | + browse_nodes (list): List of strings containing the browse node ids. |
| 398 | + async_req (bool, optional): Specify if a thread should be created to |
| 399 | + run the request. Defaults to False. |
| 400 | +
|
| 401 | + Returns: |
| 402 | + dict: A dictionary containing the browse node information. |
| 403 | + """ |
| 404 | + |
| 405 | + if isinstance(browse_nodes, list) is False: |
| 406 | + raise Exception('Browse nodes parameter should be a list') |
| 407 | + elif not browse_nodes: |
| 408 | + raise Exception('Browse nodes parameter can\'t be empty') |
| 409 | + |
| 410 | + try: |
| 411 | + request = GetBrowseNodesRequest( |
| 412 | + partner_tag=self.tag, |
| 413 | + partner_type=PartnerType.ASSOCIATES, |
| 414 | + marketplace=self.marketplace, |
| 415 | + browse_node_ids=browse_nodes, |
| 416 | + languages_of_preference=None, |
| 417 | + resources=BROWSE_RESOURCES) |
| 418 | + except ValueError as e: |
| 419 | + raise AmazonException("ValueError", e) |
| 420 | + |
| 421 | + try: |
| 422 | + self._throttle() |
| 423 | + if async_req: |
| 424 | + thread = self.api.get_browse_nodes(request, async_req=True) |
| 425 | + response = thread.get() |
| 426 | + else: |
| 427 | + response = self.api.get_browse_nodes(request) |
| 428 | + except ApiException as e: |
| 429 | + raise AmazonException('ApiException', e) |
| 430 | + |
| 431 | + try: |
| 432 | + if response.browse_nodes_result is not None: |
| 433 | + res = [AmazonBrowseNode(item) for item in response.browse_nodes_result.browse_nodes] |
| 434 | + return parse_browsenode(res) |
| 435 | + if response.errors is not None: |
| 436 | + raise AmazonException(response.errors[0].code, response.errors[0].message) |
| 437 | + except TypeError as e: |
| 438 | + raise AmazonException("TypeError", e) |
| 439 | + except ValueError as e: |
| 440 | + raise AmazonException(ValueError, e) |
| 441 | + except AmazonException as e: |
| 442 | + raise AmazonException(e.status, e.reason) |
| 443 | + except Exception as e: |
| 444 | + raise AmazonException("General", e) |
0 commit comments