def create_cart(asin, quantity=1): api = API(settings.AMAZON_AWS_KEY, settings.AMAZON_SECRET_KEY, settings.AMAZON_API_LOCALE, settings.AMAZON_ASSOCIATE_TAG) cart = api.cart_create({asin: quantity}) try: return unicode(cart.Cart.PurchaseURL) except ValueError, InvalidCartItem: raise ValidationError()
def create_cart(asin, quantity=1): api = API( settings.AMAZON_AWS_KEY, settings.AMAZON_SECRET_KEY, settings.AMAZON_API_LOCALE, settings.AMAZON_ASSOCIATE_TAG) cart = api.cart_create({asin: quantity}) try: return unicode(cart.Cart.PurchaseURL) except ValueError, InvalidCartItem: raise ValidationError()
def buy_items(item_list, quantity_list=None, category_list=None): #Instantiate Amazon API object amazon = API(locale='us') #Default buy 1 of an item, search in 'All' general category if quantity_list == None: quantity_list = [1 for item in item_list] if category_list == None: category_list = ['All' for item in item_list] item_and_quantity = dict() item_names = dict() item_prices = dict() #Search for item in Amazon, get AWS product id #Keep track of AWS product id the quantity to buy. Must be formatted as shown below for i in range(len(item_list)): #print item_list[i] result = search_item_and_price(amazon, item_list[i], category_list[i]) if result != None: item_and_quantity[result['aws_id']] = quantity_list[i] #print type(quantity_list[i]) #Create remote cart of items to buy cart = amazon.cart_create(item_and_quantity) #Get cart information. ID and HMAC is used to track and reference the created #cart. Purchase URL is what the user uses to purchase the desired items cart_id = cart.Cart.CartId cart_hmac = cart.Cart.HMAC purchase_url = cart.Cart.PurchaseURL.text #print type(purchase_url) #Get contents of cart cart_get = amazon.cart_get(cart_id, cart_hmac) subtotal = cart_get.Cart.SubTotal.getchildren()[2].text cart_contents = cart_get.Cart.CartItems.getchildren()[1:] #print type(subtotal) #pprint.pprint(item_and_quantity) #Get the prices and names of items in the cart. Since adding items to the cart #does not (seem to) let the programmer decide which offer listing to add, need #to check what is in the cart. Need to muck around the Amazon Product Advertising #API more, but man the documentation leaves much to be desired for cart_item in cart_contents: item_info = cart_item.getchildren() aws_id = item_info[1].text item_prices[aws_id] = item_info[7].getchildren()[-1].text if type(item_info[4].text) == unicode: item_names[aws_id] = item_info[4].text.encode('ascii', 'ignore') else: # Assuming it is a string type item_names[aws_id] = item_info[4].text #print item_names[aws_id] #print type(item_names[aws_id]) #print type(item_prices[aws_id]) #pprint.pprint(item_and_quantity) #pprint.pprint(item_names) #pprint.pprint(item_prices) #pprint.pprint(subtotal) #print type(purchase_url) return item_and_quantity, item_names, item_prices, subtotal, purchase_url