-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathamazon_api.py
77 lines (66 loc) · 2.95 KB
/
amazon_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from paapi5_python_sdk.api.default_api import DefaultApi
from paapi5_python_sdk.models.partner_type import PartnerType
from paapi5_python_sdk.models.search_items_request import SearchItemsRequest
from paapi5_python_sdk.models.search_items_resource import SearchItemsResource
from paapi5_python_sdk.rest import ApiException
from response_parser import parse_response
from consts import *
# function that search amazon products
def search_items(keywords, search_index="All", item_page=1):
default_api = DefaultApi(
access_key=AMAZON_ACCESS_KEY,
secret_key=AMAZON_SECRET_KEY,
host=AMAZON_HOST,
region=AMAZON_REGION,
)
""" Specify the category in which search request is to be made """
""" For more details, refer: https://webservices.amazon.com/paapi5/documentation/use-cases/organization-of-items-on-amazon/search-index.html """
""" Specify item count to be returned in search result """
item_count = 20
""" Choose resources you want from SearchItemsResource enum """
""" For more details, refer: https://webservices.amazon.com/paapi5/documentation/search-items.html#resources-parameter """
search_items_resource = [
SearchItemsResource.ITEMINFO_TITLE,
SearchItemsResource.OFFERS_LISTINGS_PRICE,
SearchItemsResource.IMAGES_PRIMARY_LARGE,
SearchItemsResource.OFFERS_LISTINGS_SAVINGBASIS,
SearchItemsResource.ITEMINFO_FEATURES,
SearchItemsResource.OFFERS_LISTINGS_PROMOTIONS,
SearchItemsResource.OFFERS_LISTINGS_CONDITION,
SearchItemsResource.OFFERS_LISTINGS_ISBUYBOXWINNER,
]
""" Forming request """
try:
search_items_request = SearchItemsRequest(
partner_tag=PARTNER_TAG,
partner_type=PartnerType.ASSOCIATES,
keywords=keywords,
search_index=search_index,
item_count=item_count,
resources=search_items_resource,
item_page=item_page,
)
except ValueError as exception:
print("Error in forming SearchItemsRequest: ", exception)
return
try:
"""Sending request"""
response = default_api.search_items(search_items_request)
print("Request received")
res = parse_response(response)
if response.errors is not None:
print("\nPrinting Errors:\nPrinting First Error Object from list of Errors")
print("Error code", response.errors[0].code)
print("Error message", response.errors[0].message)
return res
except ApiException as exception:
print("Error calling PA-API 5.0!")
print("Status code:", exception.status)
print("Errors :", exception.body)
print("Request ID:", exception.headers["x-amzn-RequestId"])
except TypeError as exception:
print("TypeError :", exception)
except ValueError as exception:
print("ValueError :", exception)
except Exception as exception:
print("Exception :", exception)