1414
1515import time
1616
17- from constant import DOMAINS , REGIONS , PRODUCT_RESOURCES , SEARCH_RESOURCES , CONDITION
17+ from constant import DOMAINS , REGIONS , CONDITION
18+ from constant import PRODUCT_RESOURCES , SEARCH_RESOURCES , VARIATION_RESOURCES
1819from exception import AmazonException
1920from parse import parse_product
2021from tools import get_asin , chunks
@@ -234,7 +235,7 @@ def search_products(self, item_count=10, item_page=1, items_per_page=10, actor=N
234235 availability = availability ,
235236 brand = brand ,
236237 browse_node_id = browse_node ,
237- condition = condition ,
238+ condition = CONDITION [ condition ] ,
238239 currency_of_preference = currency ,
239240 delivery_flags = delivery ,
240241 item_count = items_per_page ,
@@ -250,8 +251,7 @@ def search_products(self, item_count=10, item_page=1, items_per_page=10, actor=N
250251 resources = SEARCH_RESOURCES ,
251252 search_index = search_index ,
252253 sort_by = sort_by ,
253- title = title
254- )
254+ title = title )
255255 except Exception as exception :
256256 raise AmazonException (exception .status , exception .reason )
257257
@@ -284,3 +284,71 @@ def search_products(self, item_count=10, item_page=1, items_per_page=10, actor=N
284284 return results
285285 else :
286286 return None
287+
288+ def get_variations (self , asin , item_count = 10 , item_page = 1 , items_per_page = 10 , condition = None ,
289+ currency = None , languages = None , merchant = None , async_req = False ):
290+
291+ if items_per_page > 10 or items_per_page < 1 :
292+ raise AmazonException ('ValueError' , 'Arg items_per_page should be between 1 and 10' )
293+ if item_count > 100 or item_count < 1 :
294+ raise AmazonException ('ValueError' , 'Arg item_count should be between 1 and 100' )
295+ if item_page > 10 or item_page < 1 :
296+ raise AmazonException ('ValueError' , 'Arg item_page should be between 1 and 10' )
297+
298+ # Remove 10 items limit from Amazon API
299+ last_page_list = [False for x in range (int (item_count / items_per_page ))]
300+ if last_page_list :
301+ last_page_list .pop ()
302+ last_page_list .append (True )
303+ last_page_items = item_count % items_per_page
304+
305+ results = []
306+ for last_page in last_page_list :
307+ if last_page and len (last_page_list ) > 1 :
308+ items_per_page = last_page_items
309+ try :
310+ request = GetVariationsRequest (
311+ partner_tag = self .tag ,
312+ partner_type = PartnerType .ASSOCIATES ,
313+ marketplace = self .marketplace ,
314+ asin = asin ,
315+ condition = CONDITION [condition ],
316+ currency_of_preference = currency ,
317+ languages_of_preference = languages ,
318+ merchant = merchant ,
319+ offer_count = 1 ,
320+ variation_count = items_per_page ,
321+ variation_page = item_page ,
322+ resources = VARIATION_RESOURCES )
323+ except Exception as exception :
324+ raise AmazonException (exception .status , exception .reason )
325+
326+ try :
327+ # Wait before doing the request
328+ wait_time = 1 / self .throttling - (time .time () - self .last_query_time )
329+ if wait_time > 0 :
330+ time .sleep (wait_time )
331+ self .last_query_time = time .time ()
332+
333+ # Send the request and create results
334+ if async_req :
335+ thread = self .api .get_variations (request , async_req = True )
336+ response = thread .get ()
337+ else :
338+ response = self .api .get_variations (request )
339+ if response .variations_result is not None :
340+ for item in response .variations_result .items :
341+ results .append (parse_product (item ))
342+ if response .errors is not None :
343+ raise AmazonException (response .errors [0 ].code , response .errors [0 ].message )
344+
345+ except Exception as exception :
346+ raise AmazonException (exception .reason , exception .body )
347+ item_page += 1
348+ if item_page > 10 :
349+ break
350+
351+ if results :
352+ return results
353+ else :
354+ return None
0 commit comments