def message_url(update, context): amazon_valid_urls = ['www.amzn.to', 'amzn.to', 'www.amazon.', 'amazon.'] url = update.message.text domain = check_domain(update.message.text) if domain.startswith(tuple(amazon_valid_urls)): if 'amzn.to/' in domain: url = requests.get(url).url product = Product(get_asin(url)) message = amazon_message(product, update) context.bot.send_message(update.message.chat_id, message[0], reply_markup=message[1], parse_mode='HTML') context.bot.delete_message(update.message.chat_id, update.message.message_id)
def get_products(self, product_ids: [str, list], condition='Any', merchant='All', async_req=False): """Find product information for multiple products on Amazon. Args: product_ids (str|list): One or more item IDs like ASIN or product URL. Use a string separated by comma or as a list. condition (str, optional): Specify the product condition. Allowed values: Any, Collectible, New, Refurbished, Used. Defaults to Any. merchant (str, optional): Filters search results to return items having at least one offer sold by target merchant. Allowed values: All, Amazon. Defaults to All. async_req (bool, optional): Specify if a thread should be created to run the request. Defaults to False. Returns: list of instances: A list containing 1 instance for each product or None if no results. """ # Clean up input data and remove 10 items limit from Amazon API if isinstance(product_ids, str): product_ids = [x.strip() for x in product_ids.split(',')] elif not isinstance(product_ids, list): raise AmazonException( 'TypeError', 'Arg product_ids should be a list or string') asin_full_list = list(set([get_asin(x) for x in product_ids])) asin_full_list = list(chunks(asin_full_list, 10)) results = [] for asin_list in asin_full_list: try: request = GetItemsRequest(partner_tag=self.tag, partner_type=PartnerType.ASSOCIATES, marketplace=self.marketplace, merchant=merchant, condition=CONDITION[condition], item_ids=asin_list, resources=PRODUCT_RESOURCES) except KeyError: raise AmazonException('KeyError', 'Invalid condition value') except Exception as e: raise AmazonException('GetItemsError', e) for x in range(3): try: # Send the request and create results self._throttle() if async_req: thread = self.api.get_items(request, async_req=True) response = thread.get() else: response = self.api.get_items(request) break except ApiException as e: if x == 2: raise AmazonException('ApiException', e) try: if response.items_result is not None: if len(response.items_result.items) > 0: for item in response.items_result.items: results.append(parse_product(item)) except Exception as e: raise AmazonException('ResponseError', e) if results: return results else: return None
def get_variations(self, asin, item_count=10, item_page=1, items_per_page=10, condition='Any', merchant='All', async_req=False): """Returns a set of items that are the same product, but differ according to a consistent theme, for example size and color. Args: asin (str): One item ID like ASIN or product URL. item_count (int, optional): The total number of products to get. Should be between 1 and 100. Defaults to 10. item_page (int, optional): The page where the results start from. Should be between 1 and 10. Defaults to 1. items_per_page (int, optional): Products on each page. Should be between 1 and 10. Defaults to 10. condition (str, optional): The condition parameter filters offers by condition type. Allowed values: Any, Collectible, New, Refurbished, Used. Defaults to Any. merchant (str, optional): Filters search results to return items having at least one offer sold by target merchant. Allowed values: All, Amazon. Defaults to All. async_req (bool, optional): Specify if a thread should be created to run the request. Defaults to False. Returns: list of instances: A list containing 1 instance for each product or None if no results. """ if items_per_page > 10 or items_per_page < 1: raise AmazonException( 'ValueError', 'Arg items_per_page should be between 1 and 10') if item_count > 100 or item_count < 1: raise AmazonException( 'ValueError', 'Arg item_count should be between 1 and 100') if item_page < 1: raise AmazonException('ValueError', 'Arg item_page should be 1 or higher') results = [] while len(results) < item_count: try: request = GetVariationsRequest( partner_tag=self.tag, partner_type=PartnerType.ASSOCIATES, marketplace=self.marketplace, asin=get_asin(asin), condition=CONDITION[condition], merchant=merchant, offer_count=1, variation_count=items_per_page, variation_page=item_page, resources=VARIATION_RESOURCES) except KeyError: raise AmazonException('KeyError', 'Invalid condition value') except Exception as e: raise AmazonException('GetVariationsError', e) for x in range(3): try: # Send the request and create results self._throttle() if async_req: thread = self.api.get_variations(request, async_req=True) response = thread.get() else: response = self.api.get_variations(request) break except ApiException as e: if x == 2: raise AmazonException('ApiException', e) try: if response.variations_result is not None: if response.variations_result.items is not None: for item in response.variations_result.items: results.append(parse_product(item)) if len(results) >= item_count: break if len(response.variations_result.items ) < items_per_page: break else: break if response.errors is not None: raise AmazonException(response.errors[0].code, response.errors[0].message) except Exception as e: raise AmazonException('ResponseError', e) item_page += 1 if results: return results else: return None