class AmazonScraper(object): def __init__(self, access_key, secret_key, associate_tag, *args, **kwargs): self.api = AmazonAPI(access_key, secret_key, associate_tag, *args, **kwargs) def reviews(self, ItemId=None, URL=None): return Reviews(self, ItemId, URL) def review(self, Id=None, URL=None): return Review(self, Id, URL) def lookup(self, URL=None, **kwargs): if URL: kwargs['ItemId'] = extract_asin(URL) result = self.api.lookup(**kwargs) if isinstance(result, (list, tuple)): result = [Product(p) for p in result] else: result = Product(result) return result def similarity_lookup(self, **kwargs): for p in self.api.similarity_lookup(**kwargs): yield Product(p) def browse_node_lookup(self, **kwargs): return self.api.browse_node_lookup(**kwargs) def search(self, **kwargs): for p in self.api.search(**kwargs): yield Product(p) def search_n(self, n, **kwargs): for p in self.api.search_n(n, **kwargs): yield Product(p)
def handle(self, *args, **options): amazon = AmazonAPI(settings.AMAZON_ACCESS_KEY, settings.AMAZON_SECRET_KEY, settings.AMAZON_ASSOC_TAG) two_days_ago = date.today() - timedelta(days=2) # isbns = [book.isbn for book in Book.objects.filter(mod_date__gte=two_days_ago)] isbns = [book.isbn for book in Book.objects.all()] grouped_isbns = map(None, *[iter(isbns)]*10) print "=== Start daily book update." for index, isbns in enumerate(grouped_isbns): isbns = filter(None, isbns) isbns = ",".join(isbns) print " == index : %s / items : %s" % (str(index), isbns) amazon_books = amazon.lookup(ItemId=isbns) for amazon_book in amazon_books: try: dbbook = Book.objects.get(isbn__exact=amazon_book.isbn) dbbook.update(amazon_book) except Book.DoesNotExist: Book.objects.create_book(amazon_book) time.sleep(4) print "=== Successful updated all books"
def update_price(self): # check if this is an Amazon product if self.distributor.name == 'Amazon': amazon = AmazonAPI(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, settings.AWS_ASSOCIATE_TAG) try: product = amazon.lookup(ItemId=self.part.asin) price = product.price_and_currency return price[0] except: pass else: import urllib2 from lxml import etree import microdata import urllib items = microdata.get_items(urllib.urlopen(self.url)) for i in items: if i.offers: return "%s (md)".replace("$", "") % i.offers.price.strip().replace("$", "") html = urllib2.urlopen(self.url).read() tree = etree.HTML(html) price = tree.xpath("%s/text()[1]" % self.xpath) try: return "%s (xp)" % price[0].replace("$", "") except: return "N/A"
class Item(object): def __init__(self, product): self.product = product self.amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG) def prod_search(self): products_found = self.amazon.search_n(1, Keywords= self.product, SearchIndex= "All") return products_found def prod_item(self): products_found = self.prod_search() try: return products_found[0].title except IndexError: raise Exception('No product found') def prod_asin(self): products_found = self.prod_search() try: return products_found[0].asin except IndexError: raise Exception('No Asin found') def prod_price(self): product_asin = self.prod_asin() the_product = self.amazon.lookup(ItemId='' + product_asin + '') found_product_price = the_product.price_and_currency print the_product.image return found_product_price
def asin_search(self): try: amazon = AmazonAPI(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, settings.AWS_ASSOCIATE_TAG) products = amazon.search_n(10, Keywords="%s %s" % (self.company.name, self.number), SearchIndex="All") except: products = None return products
def asin_image(self): amazon = AmazonAPI(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, settings.AWS_ASSOCIATE_TAG) try: product = amazon.lookup(ItemId=self.asin) return product.large_image_url except: return None
class GiftlyAmazonAPI: amazon_product_details = ['api', 'asin', 'author', 'authors', 'aws_associate_tag', 'binding', 'brand', 'browse_nodes', 'color', 'creators', 'ean', 'edition', 'editorial_review', 'editorial_reviews', 'eisbn', 'features', 'get_attribute', 'get_attribute_details', 'get_attributes', 'get_parent', 'images', 'isbn', 'label', 'languages', 'large_image_url', 'list_price', 'manufacturer', 'medium_image_url', 'model', 'mpn', 'offer_url', 'pages', 'parent', 'parent_asin', 'parsed_response', 'part_number', 'price_and_currency', 'publication_date', 'publisher', 'region', 'release_date', 'reviews', 'sales_rank', 'sku', 'small_image_url', 'tiny_image_url', 'title', 'to_string', 'upc'] amazon_search_index = ['All','Apparel','Appliances','ArtsAndCrafts','Automotive', 'Baby', 'Beauty','Blended','Books','Classical','Collectibles','DVD', 'DigitalMusic','Electronics', 'GiftCards','GourmetFood','Grocery', 'HealthPersonalCare','HomeGarden','Industrial','Jewelry', 'KindleStore', 'Kitchen','LawnAndGarden','Marketplace','MP3Downloads','Magazines','Miscellaneous', 'Music','MusicTracks','MusicalInstruments','MobileApps','OfficeProducts','OutdoorLiving', 'PCHardware', 'PetSupplies','Photo','Shoes','Software','SportingGoods', 'Tools','Toys','UnboxVideo','VHS','Video', 'VideoGames','Watches','Wireless','WirelessAccessories'] def __init__(self, secret_key, access_key, assoc_tag): self.amazon = AmazonAPI(access_key, secret_key, assoc_tag) #Keywords is a comma-separated string #Returns a dictionary of products mapped as ASIN:TITLE #Can Android parse for keys? We'll find out... def get_similar_items(self, keywords, numitems=None, category=None): keywords = keywords if keywords else None numitems = numitems if numitems else 10 category = category if category else 'All' print "%d items found with keywords %s in the %s category" % (numitems, keywords, category) products = self.amazon.search_n(numitems, Keywords=keywords, SearchIndex=category) product_dict = {} for product in products: product_dict[product.asin] = product.title return product_dict def get_item_by_asin(self, asin): product = self.amazon.lookup(ItemId=asin) product = AmazonProduct(product.asin, product.title) return product.get_product() #asin_list is a list of individual asin strings #they are joined together as one large string def get_items_by_asin(self, asin_list): product_list = [] products = self.amazon.lookup(ItemId=(asin_list)) for product in products: product_list.append(AmazonProduct(product.asin, product.title).get_product()) print product_list return product_list
def search_item(): query = request.args.get('q') category = request.args.get('c') ## replace confidencial to secret file and put it in gitignore from amazon.api import AmazonAPI amazon = AmazonAPI(os.environ['AWS_ACCESS_KEY'], os.environ['AWS_SECRET_KEY'], 'papylus-22', region="JP") if not category: category = 'All' try: products = amazon.search_n(5, Keywords=query, SearchIndex=category) except: # in case 503 Error occurs without any reason import time for i in range(3): print '503 Error' time.sleep(1) products = amazon.search_n(5, Keywords=query, SearchIndex=category) if products: break items = [] for i in products: pub_date = i.publication_date.strftime(u'%Y-%m-%d') if i.publication_date != None else '' items.append({'name': i.title, 'url': i.offer_url, 'img': i.medium_image_url, 'publisher': i.publisher, 'pub_date': pub_date}) return json.dumps(items)
def get(self): search = self.request.get('search') amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG) #initiates a new Amazon API amazon_results = amazon.search_n(15, Keywords=search, SearchIndex='All') # returns in JSON name, salePrice, and URL of user's search from BestBuy best_buy_url = 'http://api.remix.bestbuy.com/v1/products(search='+ search.replace(' ', '&search=')+')?format=json&show=sku,name,salePrice,url,image,upc&pageSize=15&page=5&apiKey=24ta6vtsr78a22fmv8ngfjet' best_buy_results = json.load(urllib2.urlopen(best_buy_url)).get('products') walmart_url = "http://api.walmartlabs.com/v1/search?query=%s&format=json&apiKey=cz9kfm3vuhssnk6hn33zg86k&responseGroup=base" % search.replace(' ','+') walmart_results = json.load(urllib2.urlopen(walmart_url)).get('items') results = [] for product in amazon_results: results += [(product.title, product.price_and_currency[0], product.offer_url, product.medium_image_url, 'Amazon','/wishlist?type=amazon&id=%s'%product.asin,"/compare?upc=%s" %str(product.upc), '/email?producturl=%s'%product.offer_url)] #How to retrive asin for amazon products and decrease image size for product in best_buy_results: results += [(product.get('name'), product.get('salePrice'), product.get('url'), product.get('image'), 'Best Buy','/wishlist?type=bestbuy&id=%s'%product.get('sku'),"/compare?upc=%s" %str(product.get('upc')), '/email?producturl=%s'%product.get('url'))] for product in walmart_results: results += [(product.get('name'), product.get('salePrice'), product.get('productUrl'), product.get('thumbnailImage'), 'Walmart','/wishlist?type=walmart&id=%s'%product.get('itemId'),"/compare?upc=%s" %str(product.get('upc')),'/email?producturl=%s'%product.get('productUrl'))] results = sorted(results,key=lambda x: x[1]) for item in results: i = results.index(item) if str(item[1])[len(str(item[1]))-2] == '.': item = list(item) item[1] = str(item[1]) + '0' item = tuple(item) results[i] = item template_variables={"user_search":search, 'results':results, 'user': users.get_current_user(), 'logout' : users.create_logout_url('/'), 'login' : users.create_login_url('/')} template=jinja_environment.get_template('/templates/results.html') self.response.write(template.render(template_variables))
def amazonResults(amazonID): '''Puts ASIN through Amazon API and returns item title, price, and image''' amazon = AmazonAPI(amazonAccessKey, amazonSecretKey, amazonAssocTag) product = amazon.lookup(ItemId=amazonID) title = product.title image = product.large_image_url price = product.price_and_currency return title, price, image
def amazon_price(self): amazon = AmazonAPI(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, settings.AWS_ASSOCIATE_TAG) try: product = amazon.lookup(ItemId=self.asin) price = product.price_and_currency return "$%.2f %s" % (price[0], price[1]) except: return None
def app_results(query): amazon = AmazonAPI(AWS_KEY, SECRET_KEY, ASSOC_TAG) products = amazon.search(Keywords=query, SearchIndex='All') if referrer == 'dashboard': return render_template("amazon_bottlenose2.html", products = products) else: form = AddProductForm() return render_template("amazon_bottlenose_add.html", products = products, form=form)
def add_book(request, isbn): amazon = AmazonAPI(settings.AMAZON_ACCESS_KEY, settings.AMAZON_SECRET_KEY, settings.AMAZON_ASSOC_TAG) amazon_book = amazon.lookup(ItemId=isbn) Book.objects.create_book(amazon_book) if request.is_ajax(): return HttpResponse(status=200) else: return HttpResponseRedirect(request.META.get("HTTP_REFERER", ""))
def get(self): amazon = AmazonAPI() watches = models.get_db_watches() if watches: for WatchListing in watches: product = amazon.lookup(ItemId=WatchListing.ItemId) WatchListing.Price = product.price_and_currency[0] WatchListing.put() time.sleep(1) #amazon only allows a single API per second... return
def generate_csv(): # initialize amazon api with access key, secret key, and associate tag amazon = AmazonAPI('AKIAJPT5M67Z5DB6R3XA', 'P0ekhRiDVDC2xeJa4fZz1P5qHY/B2Qig71G6wZB3', 'thedeepdark-20') print("Querying amazon API") # returns available book subjects subjects = amazon.browse_node_lookup(BrowseNodeId=1000) f = open('data.csv', 'wb') writer = csv.writer(f) print("\tReturned with " + str(len(subjects[0].children)) + " subjects") # creates books and author lists for subject in subjects: for genre in subject.children: # skip calendar entries if genre.name.text == 'Calendars': continue # returns first 1000 entries in each subject # Amazons api limits the number of return pages to 10 # with 10 items on each for a maximum of 100 items books = amazon.search_n(100, Condition='All', BrowseNode=genre.id, SearchIndex='Books', MaxQPS=0.9) print("Queried " + genre.name + ", returned " + str(len(books)) + " books") failed = 0 for book in books: b_isbn = book.isbn b_title = book.title b_pub_date = str(book.publication_date) b_genre = genre.name b_publisher = book.publisher b_list_price = book.list_price[0] b_price = book.price_and_currency[0] if len(book.authors) == 0: break book_item = [b_isbn, b_title, book.authors[0], b_pub_date, b_publisher, b_genre, b_list_price, b_price] for x in range(len(book_item)): if isinstance(book_item[x], str): book_item[x] = unicode(book_item[x], 'utf-8') try: writer.writerow(book_item) except UnicodeEncodeError: failed += 1 print("\tDone processing books, failed to convert unicode characters " + str(failed) + " times") time.sleep(5)
def keywordSearch(keywords): '''Searches for top 50 results on Amazon of keywords entered by user''' amazon = AmazonAPI(amazonAccessKey, amazonSecretKey, amazonAssocTag) products = amazon.search_n(50, Keywords=keywords, SearchIndex='All') title = ['i', 'i'] image = [''] listPrice = [''] for counter, product in enumerate(products): title.insert(counter, product.title) image.insert(counter, product.large_image_url) listPrice.insert(counter, product.price_and_currency) return title, image, listPrice
def getTopProductIDs(keywords="camera", searchIndex='Electronics'): AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG = \ getAmazomConfidentialKeys() amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG) products = amazon.search(Keywords=keywords, SearchIndex=searchIndex) productIDs = [] for i, product in enumerate(products): productIDs.append(product.asin) return productIDs
def func(product): #insert amazon web services credentials AMAZON_ACCESS_KEY = 'AKIAJGEKFL3UEU6QMCPQ' AMAZON_SECRET_KEY = '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' #associate TAG must be updated every 180 days, make new amazon associates account to get new tag AMAZON_ASSOC_TAG = 'ignacio0ba-20' amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG) products = amazon.search_n(1,Keywords= product, SearchIndex= "All") print products
def prod_price(self, product_asin): #insert amazon web services credentials AMAZON_ACCESS_KEY = 'AKIAJGEKFL3UEU6QMCPQ' AMAZON_SECRET_KEY = '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' #associate TAG must be updated every 180 days, make new amazon associates account to get new tag AMAZON_ASSOC_TAG = 'ignacio0ba-20' amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG) the_product = amazon.lookup(ItemId='' + product_asin + '') found_product_price = the_product.price_and_currency
def get_amazon_order(item): amazon = AmazonAPI(AWS['AMAZON_KEY'], AWS['SECRET_KEY'], AWS['ASSOCIATE_TAG']) if len(item) > 0: try: product = amazon.search_n(1, Keywords=item, SearchIndex='All') try: return product[0] except IndexError: return None except AmazonException: return None else: return None
def prod_search(self): #insert amazon web services credentials #associate TAG must be updated every 180 days, make new amazon associates account to get new tag amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG) products_found = amazon.search_n(1,Keywords= self.product, SearchIndex= "All") try: return products_found[0].title except IndexError: return 'No such item available'
def fetch_from_amazon(game): title = ''.join(('NES ', game.title)) AMAZON_ACCESS = os.environ['AMAZON_ACCESS'] AMAZON_SECRET = os.environ['AMAZON_SECRET'] AMAZON_AWS = os.environ['AMAZON_AWS'] amazon = AmazonAPI(AMAZON_ACCESS, AMAZON_SECRET,AMAZON_AWS) try: products = amazon.search_n(1, Keywords=title, Condition='Used', SearchIndex = 'VideoGames') return amazon.lookup(ItemId=products[0].asin, Condition='Used') # if above fails the template handles the error except: pass
def createCart(products): pdict = {} for item in products: if item in pdict: pdict[item] = pdict[item] + 1 else: pdict[item] = 1 amazon = AmazonAPI(ACCESS_KEY, SECRET_KEY, ASSOC_ID) for index, key in enumerate(pdict.items()): product = amazon.lookup(ItemId=key[0]) item = {'offer_id': product.offer_id, 'quantity': key[1]} if index == 0: cart = amazon.cart_create(item) else: cart = amazon.cart_add(item, cart.cart_id, cart.hmac) return cart
class Amazon: def __init__(self): self.amazon = AmazonAPI('AKIAJTMXQEFQVV7SEF5Q', 'CwNQ/cKQNXckNMhfb7oQ0KTmBoaJNPqSFqzKtubf', 'fbhackcom0d-21', region='UK') def search_for(self, text, top=3): try: products = self.amazon.search(Keywords=text, SearchIndex='All') top_products = [] product_titles = {} for product in products: if product.title is not None and product.price_and_currency is not None: title = product.title # if len(title) > 50: # title = title[:50] + "..." product_titles[product.offer_url] = title top_products.append({ 'offer_url': product.offer_url, 'name': product.title, 'image': product.medium_image_url, 'price': product.price_and_currency[0], 'currency': product.price_and_currency[1], }) top -= 1 if top == 0: break return top_products except: return []
def get_amazon_book_data(cls, isbn): amazon = AmazonAPI(os.environ['AWS_ACCESS_KEY_ID'], os.environ['AWS_SECRET_ACCESS_KEY'], os.environ['AWS_ASSOCIATE_TAG'], region='UK') product_or_products = amazon.lookup(ItemId=isbn, IdType='ISBN', SearchIndex="Books") if type(product_or_products) is list: product = product_or_products[0] else: product = product_or_products return {'isbn': isbn, 'title': product.title, 'page_count': product.pages, 'authors': product.authors }
def get_amazon_product_meta(url): # the input URL is always of amazon amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, region="IN") item_id = get_amazon_item_id(url) if not item_id: return None try: product = amazon.lookup(ItemId=item_id) except amazon.api.AsinNotFound: # log this ASIN return None # product.price_and_currency returns in the form (price, currency) product_price = product.price_and_currency[0]
def test_search_amazon_uk(self): """Test Poduct Search on Amazon UK. Tests that a product search on Amazon UK is working and that the currency of any of the returned products is GBP. The test fails if no results were returned. """ amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, region="UK") assert_equals(amazon.api.Region, "UK", "Region has not been set to UK") products = amazon.search(Keywords="Kindle", SearchIndex="All") currencies = [product.price_and_currency[1] for product in products] assert_true(len(currencies), "No products found") is_gbp = "GBP" in currencies assert_true(is_gbp, "Currency is not GBP, cannot be Amazon UK, though")
def single_access(self, token, option=None): Wrapper.single_access(self,token) result = {} try: asin = option['item'] amazon = AmazonAPI(token['ACCESS_KEY'], token['SECRET_KEY'], token['ASSOC_TAG'], region=token['LOCALE']) data = amazon.lookup(ItemId=asin) result['raw'] = data result['title'] = data.title result['category'] = data.get_attribute('ProductGroup') except: # If any exceptions happen, return None sys.stderr.write("Unexpected error accessing API:\n\t%s\n\t%s" % (sys.exc_info()[0], sys.exc_info()[1])) result = None return result
def update_price(): time_threshold = timezone.now() - timedelta(minutes=settings.UPDATE_PRICE_THRESHOLD) affiliate = Affiliate.objects.get(name='Amazon') products = Product.objects.filter(price_updated__lt=time_threshold, affiliate=affiliate).order_by('price_updated')[:10] if len(products) > 0: asins = [p.asin for p in products] asins_string = ','.join(asins) amazon = AmazonAPI(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, settings.AWS_ASSOCIATE_TAG) az = amazon.lookup(ItemId=asins_string) if type(az) is list: for p in az: process_item(p) else: process_item(az)
def prod_asin(self): #insert amazon web services credentials AMAZON_ACCESS_KEY = 'AKIAJGEKFL3UEU6QMCPQ' AMAZON_SECRET_KEY = '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' #associate TAG must be updated every 180 days, make new amazon associates account to get new tag AMAZON_ASSOC_TAG = 'ignacio0ba-20' amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG) products_found = amazon.search_n(1,Keywords= self.product, SearchIndex= "All") try: return products_found[0].asin except IndexError: return 'No product available'
def test_region(self): amazon = AmazonAPI(_AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY, _AMAZON_ASSOC_TAG) assert_equals(amazon.region, 'US') # old 'region' parameter amazon = AmazonAPI(_AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY, _AMAZON_ASSOC_TAG, region='UK') assert_equals(amazon.region, 'UK') # kwargs method amazon = AmazonAPI(_AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY, _AMAZON_ASSOC_TAG, Region='UK') assert_equals(amazon.region, 'UK')
def get_products(self, obj): ''' Gets the products attached to the order. The expand method is provided here because we won't always want to query Amazon for specific information about the product. If expand is true we will hit Amazon and get back specific information about the product such as price and images. :param obj: :return: ''' request, expand, _, _, _ = gather_request_data(self.context) serialized_products = [ ProductSerializer(product).data for product in obj.get_products() ] if expand == 'true': # pragma: no cover # Not covering this as we have no good way to mock a request to # the amazon api as they use request signatures. - Devon Bleibtrey vendor_ids = [ product['vendor_id'] for product in serialized_products ] amazon = AmazonAPI(settings.AMAZON_PROMOTION_API_KEY, settings.AMAZON_PROMOTION_API_SECRET_KEY, settings.AMAZON_ASSOCIATE_TAG) for sub_list in chunk_list(vendor_ids, 10): sub_ids = ",".join(sub_list) products = amazon.lookup(ItemId=sub_ids) if not hasattr(products, '__iter__'): products = [products] for product in products: match = next((l for l in serialized_products if l['vendor_id'] == product.asin), None) if match is not None: price, currency = product.price_and_currency match['information'] = { "title": product.title, "image": product.large_image_url, "price": price, "currency": currency, "asin": product.asin, "url": product.offer_url } return serialized_products
def setUp(self): """Set Up. Initialize the Amazon API wrapper. The following values: * AMAZON_ACCESS_KEY * AMAZON_SECRET_KEY * AMAZON_ASSOC_TAG Are imported from a custom file named: 'test_settings.py' """ self.amazon = AmazonAPI( _AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY, _AMAZON_ASSOC_TAG, CacheReader=cache_reader, CacheWriter=cache_writer, MaxQPS=0.5 )
def test_search_amazon_uk(self): """Test Poduct Search on Amazon UK. Tests that a product search on Amazon UK is working and that the currency of any of the returned products is GBP. The test fails if no results were returned. """ amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, region="UK") assert_equals(amazon.api.Region, "UK", "Region has not been set to UK") products = amazon.search(Keywords='Kindle', SearchIndex='All') currencies = [product.price_and_currency[1] for product in products] assert_true(len(currencies), "No products found") is_gbp = 'GBP' in currencies assert_true(is_gbp, "Currency is not GBP, cannot be Amazon UK, though")
def search(request): if request.method == 'POST': searchvalue = request.POST.get('searchbox', '') saved_or_not = request.POST.get('saved_or_not', '') amazon = AmazonAPI(ACCESS_KEY, SECRET_KEY, ASSOC_ID) products = amazon.search_n(20, Keywords=searchvalue, SearchIndex='All') context = {'search': searchvalue, 'products': products} if saved_or_not == 'saved': for product in products: if product.title and product.offer_url and product.large_image_url and product.formatted_price and product.price_and_currency: gift = Gift(name=product.title, link=product.offer_url, image_link=product.large_image_url, price_desc=product.formatted_price, price=product.price_and_currency[0]) gift.save() else: context = {} return render(request, 'azsearch/search.html', context)
def amazon_api(asin, url, marketplace='US'): try: amazon = AmazonAPI(settings.AWS_KEY, settings.AWS_SECRET, settings.AWS_API, region=marketplace) product = amazon.lookup(ItemId=asin) model_product = ProductRecord( title=product.title, product_url='<missing product url>', listing_url=url, price=str(product.price_and_currency[1]) + str(product.price_and_currency[0]), primary_img=product.large_image_url, product_indexing=None, crawl_time=datetime.now(), asin=asin.upper()) return model_product except: return None
def amazon_api(country): if country in ['us', 'fr', 'it', 'jp']: number = int(time.time() * 10) % 3 amazon_id = (amazon_id_1, amazon_id_2, amazon_id_3)[number] return AmazonAPI(amazon_id['asso_id'], amazon_id['asso_key'], amazon_id[country], region=country.upper()) elif country in ['ca', 'es']: number = int(time.time() * 10) % 2 amazon_id = (amazon_id_2, amazon_id_3)[number] return AmazonAPI(amazon_id['asso_id'], amazon_id['asso_key'], amazon_id[country], region=country.upper()) else: return AmazonAPI(amazon_id_1['asso_id'], amazon_id_1['asso_key'], amazon_id_1[country], region=country.upper())
def __init__(self): """ Inits a knowledgeSeeker who will seek out book information. :param userlist_name: Name of userlist to get books from. :param api_info: key info for the api we are using. This is (key, secret) for goodreads and (amazon_access_key, amazon_secret_key, amazon_assoc_tag) for amazon. :param api_type: 'goodreads' or 'amazon'. The first gets book objects from goodreads and the second gets book objects from amazon. """ # Init goodreads bookshelf # Make it read only so than can run goodreads book seeker in parallel self.source_bookshelf = shelve.open( "../data/book_db/goodreads_bookshelf.db", flag='r') # Init the bookshelf we will store amazon data in self.amazon_bookshelf = shelve.open( "../data/book_db/amazon_bookshelf.db") # Init the amazon client. self.amazon = AmazonAPI(*aws)
def amazon_textbook_fields(isbn): if amazon is None: amazon = AmazonAPI(get_secret('AMAZON_ACCESS_KEY'), get_secret('AMAZON_SECRET_KEY'), get_secret('AMAZON_ASSOC_TAG')) try: product = amazon.lookup(ItemId=isbn, IdType='ISBN', SearchIndex='Books') except AsinNotFound: return if isinstance(product, list): product = product[0] return { 'detail_url': product.detail_page_url, 'image_url': product.medium_image_url, 'author': product.author, 'title': product.title, }
class AWSClient(object): """ """ def __init__(self, region=None, access_key=None, secret_access_key=None, tag=None): self.region = 'us-east-1' self.root_access_key = 'AKIAJB4BJYPJKV5YACXQ' self.root_secret_access_key = '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' self.search_access_key = 'AKIAIS2HFIM7UBM2H5CA' self.search_secret_access_key = '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' self.associate_tag = "msucapstone0a-20" self.create_comprehend_client() self.create_search_client() def create_comprehend_client(self): """ """ self.comprehend_client = boto3.client( 'comprehend', region_name=self.region, aws_access_key_id=self.root_access_key, aws_secret_access_key=self.root_secret_access_key) def create_search_client(self): self.search_client = AmazonAPI(self.search_access_key, self.search_secret_access_key, self.associate_tag) def run_transcribe_job(self): pass def comprehend_entities(self, text_input): response = self.comprehend_client.detect_entities(Text=text_input, LanguageCode='en') return response def comprehend_key_phrases(self, text_input): response = self.comprehend_client.detect_key_phrases(Text=text_input, LanguageCode='en') return response def comprehend_sentiment(self, text_input): pass def search_n(self, keywords, index, n): return self.search_client.search_n(n, Keywords=keywords, SearchIndex=index)
def single_access(self, token, option=None): Wrapper.single_access(self, token) result = {} try: asin = option['item'] amazon = AmazonAPI(token['ACCESS_KEY'], token['SECRET_KEY'], token['ASSOC_TAG'], region=token['LOCALE']) data = amazon.lookup(ItemId=asin) result['raw'] = data result['title'] = data.title result['category'] = data.get_attribute('ProductGroup') except: # If any exceptions happen, return None sys.stderr.write("Unexpected error accessing API:\n\t%s\n\t%s" % (sys.exc_info()[0], sys.exc_info()[1])) result = None return result
def fetch_random(term=None, num_results=5): """ Build a random search term of 2-3 words and perform an Amazon product search. :return: A tuple containing an instance of :class:`amazon.api.AmazonSearch` and the search term used. """ if not term: search_term = '' words = Word.objects.order_by('?')[:randint(2,3)] for word in words: search_term = "%s %s" % (word, search_term.strip()) else: search_term = term print search_term amazon = AmazonAPI(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, settings.AWS_ASSOCIATE_TAG) products = amazon.search_n(num_results, Keywords="%s" % search_term, SearchIndex='All') return products, search_term
def amazon_api_request_price(): item_id = request.json.get('item_id') brand = request.json.get('brand') model = request.json.get('model') item = Item.query.filter_by(id=item_id).first() amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG) if item: try: products = amazon.search_n(1, Brand=brand, Title=model, SearchIndex='SportingGoods', ResponseGroup='VariationSummary') product_info = { 'offer_url': products[0].offer_url, 'variation_summary': { 'LowestPrice': { 'FormattedPrice': products[0].parsed_response.VariationSummary. LowestPrice["FormattedPrice"].text, 'Amount': products[0].parsed_response.VariationSummary. LowestPrice["Amount"].text }, 'HighestPrice': { 'FormattedPrice': products[0].parsed_response.VariationSummary. HighestPrice["FormattedPrice"].text } } } except: error = {'error': 'amazon api exception'} return json.dumps(error) return json.dumps(product_info)
def create_api_connection(self, config_dict: dict, secrets_dict: dict): try: self.AMAZON_ACCESS_KEY = secrets_dict["AMAZON_ACCESS_KEY"] self.AMAZON_SECRET_KEY = secrets_dict["AMAZON_SECRET_KEY"] self.AMAZON_ASSOC_TAG = secrets_dict["AMAZON_ASSOC_TAG"] except Exception as e: raise Exception( f"{self.HEADER} FATAL: Could not read necessary access-, secret-key and association " f"tag from config. Error-Msg: {str(e)}") try: self.RETRY_LIMIT = int(config_dict["RETRY_LIMIT"]) self.COOLDOWN_TIME_FOR_RETRY = int( config_dict["COOLDOWN_TIME_FOR_RETRY"]) except Exception as e: LogHandler.log_message( f"{self.HEADER} WARNING: Could not read API-Handler-Values from config. Using default " f"values instead. Error-Msg: {str(e)}") self.RETRY_ERROR_MESSAGE = ( f"{self.HEADER} ERROR: Could not reach API after {self.RETRY_LIMIT} retries!" ) try: self.AMAZON_API = AmazonAPI( self.AMAZON_ACCESS_KEY, self.AMAZON_SECRET_KEY, self.AMAZON_ASSOC_TAG, region="DE", MaxQPS=0.5, ) except TypeError as e: LogHandler.log_message( f"{self.HEADER} ERROR: Could not initialize Amazon-API-Connection! Have you " f"provided correct values in your secrets.json-File? Msg.: {str(e)}" )
def __init__(self, credentials_filename): creds = [] with open(credentials_filename, 'rb') as fi: reader = csv.reader(fi) for row in reader: creds.append(row) self.tasks = Queue.Queue(len(creds)) for cred in creds: amazon = AmazonAPI(cred[0], cred[1], self.affiliate_id, MaxQPS=0.95) AmazonWorker(self.tasks, amazon)
def get_or_create_product(asin): """ Checks the database for an existing ASIN. If not found, try to fetch it using the Amazon Product API. :return: An instance of `products.models.Product` """ try: product = Product.objects.get(asin=asin) except: amazon = AmazonAPI(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, settings.AWS_ASSOCIATE_TAG) az = amazon.lookup(ItemId=asin) product = Product(asin=asin, upc=az.upc, ean=az.ean, description=az.title, image_url=az.large_image_url, amazon_url=az.offer_url) product.save() generate_thumb.delay(product, '600x400') generate_thumb.delay(product, '125x125') product.manufacturer = az.get_attribute('Manufacturer') product.brand = az.get_attribute('Brand') product.model_number = az.get_attribute('Model') product.mpn = az.mpn product.part_number = az.part_number product.sku = az.sku product.isbn = az.isbn product.length = az.get_attribute('ItemDimensions.Length') product.width = az.get_attribute('ItemDimensions.Width') product.height = az.get_attribute('ItemDimensions.Height') product.weight = az.get_attribute('ItemDimensions.Weight') product.save() return product
def update_product(product): url = product.product_url try: AMAZON_ACCESS_KEY = 'AKIAIZPHCO56EZACZHBA' AMAZON_SECRET_KEY = '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' AMAZON_ASSOC_TAG = '1015f-21' region = url.split("amazon.")[1].split("/")[0] if "/dp/" in url: ASIN = url.split("/dp/")[1].strip("/").split("/")[0] elif "/gp/" in url: ASIN = url.split("/gp/product/")[1].strip("/").split("/")[0] print(ASIN) amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, region=region.upper()) products = amazon.lookup(ItemId=ASIN) price, currency = products.price_and_currency title = products.title graph = Graph(product_id=product, updated_at=datetime.now(), current_price=price) graph.save() product = Product.objects.get(asin=ASIN) user_products = User_products.objects.filter(product_id=product) for user_product in user_products: if user_product.price_drop_below: print(title, int(price),user_product.email_to) if int(price) <= int(user_product.price_drop_below): print(user_product.user_id_id) print("Price drop value = " + str(user_product.price_drop_below)) print("SENT MAILLLLL TOOOOOO " + str(user_product.email_to)) send_mail('Price dropped!', 'Hi, the price of ' + str(title) + "dropped from " + str(user_product.price_when_added) + " to " + str(price) + "\n\nFollow this link to buy now \n" + str(url), EMAIL_HOST_USER, [user_product.email_to], fail_silently=False) except Exception as e: print("ERROR " + str(e)) return
def main(selfStr): config = configparser.ConfigParser() #config.read('config.ini') #AMAZON_KEY = config.get('amazon', 'AMAZON_KEY') AMAZON_KEY = 'YOUR KEY HERE' #AMAZON_SECRET = config.get('amazon', 'AMAZON_SECRET') AMAZON_SECRET = 'YOUR SECRET KEY HERE' #AMAZON_ASSOCIATE = config.get('amazon', 'AMAZON_ASSOCIATE') AMAZON_ASSOCIATE = 'reffit-20' amazon = AmazonAPI(AMAZON_KEY, AMAZON_SECRET, AMAZON_ASSOCIATE) if ('/dp/' in selfStr) or ('/gp/' in selfStr): #try: print 'Finding item...' product = amazon.lookup(ItemId=get_asin(selfStr)) print 'Found item!' title = product.title price = min(product.price_and_currency[0], product.list_price[0]) if (price <= 0): price = max(product.price_and_currency[0], product.list_price[0]) #print product.images[0].LargeImage.URL ''' for im in product.images: print im.LargeImage.URL a = raw_input() ''' image = str(product.images[-1].LargeImage.URL) print title print price print image print 'Starting calculations' link, p, diff = calc_result(title, price, image) print link + ' $:' + str(p) + ' ' + str(diff) return link, p, diff
def getTop5Books(queryTerm): # AWSACCESSKEYID, AWSSECRETKEY, ASSOCIATETAG are declared as environment varialbes while creating the Lambda function AWSAccessKeyId = os.environ.get("AWSACCESSKEYID") AWSSecretKey = os.environ.get("AWSSECRETKEY") associateTag = os.environ.get("ASSOCIATETAG") dataOfAllBooks = [] api = AmazonAPI(AWSAccessKeyId , AWSSecretKey , associateTag) try: books = api.search(Keywords = queryTerm, SearchIndex = 'Books', salesrank = 'Bestselling') incrementor = 0 num_of_books = 1 for _, book in enumerate(books): if num_of_books <= 5: book_price = book.price_and_currency[0] # skip the book if it is free (they could be promotional stuff) if book_price and book.pages and int(book.pages) >= 50: node = book.browse_nodes[0] category = node.ancestor.name if (category == 'Books'): continue else: if not (is_valid_category(category)): category = node.ancestor.ancestor.name if (is_valid_category(category)): dataOfAllBooks = get_details(dataOfAllBooks, book) incrementor += 1 num_of_books += 1 else: dataOfAllBooks = get_details(dataOfAllBooks, book) incrementor += 1 num_of_books += 1 else: break return dataOfAllBooks except: message = {'contentType': 'PlainText', 'content': """Sorry, no such technical book on Amazon. Please try something else. Ex: suggest me a java book"""} return message
class AmazonScraper(object): def __init__(self, access_key, secret_key, associate_tag, *args, **kwargs): self.api = AmazonAPI(access_key, secret_key, associate_tag, *args, **kwargs) def reviews(self, ItemId=None, URL=None): return Reviews(self, ItemId, URL) def review(self, Id=None, URL=None): return Review(self, Id, URL) def reviewer(self, url): return Reviewer(url) def lookup(self, URL=None, **kwargs): if URL: kwargs['ItemId'] = extract_asin(URL) result = self.api.lookup(**kwargs) if isinstance(result, (list, tuple)): result = [Product(p) for p in result] else: result = Product(result) return result def similarity_lookup(self, **kwargs): for p in self.api.similarity_lookup(**kwargs): yield Product(p) def browse_node_lookup(self, **kwargs): return self.api.browse_node_lookup(**kwargs) def search(self, **kwargs): for p in self.api.search(**kwargs): yield Product(p) def search_n(self, n, **kwargs): for p in self.api.search_n(n, **kwargs): yield Product(p)
def amazon_textbook_fields(isbn): if amazon is None: amazon = AmazonAPI( get_secret("AMAZON_ACCESS_KEY"), get_secret("AMAZON_SECRET_KEY"), get_secret("AMAZON_ASSOC_TAG"), ) try: product = amazon.lookup(ItemId=isbn, IdType="ISBN", SearchIndex="Books") except AsinNotFound: return if isinstance(product, list): product = product[0] return { "detail_url": product.detail_page_url, "image_url": product.medium_image_url, "author": product.author, "title": product.title, }
def run(): b = books() amazon = AmazonAPI(settings.AMZ, settings.AMZSCRT, settings.AMZID) query = amazon.search(Keywords='free kindle books', SearchIndex='KindleStore') for i in query: #new = books.objects.get(id=i.asin) b = books() #if book exists update the price and the update timestamp try: q = books.objects.get(id=i.asin) #if len(new) > 0: if i.price_and_currency[0] != None: q.price = i.price_and_currency[0] q.save() except: b.id = (i.asin) b.title = (i.title) b.description = (i.editorial_review) b.image = (i.large_image_url) b.author = (i.author) b.number_pages = (i.pages) b.publisher = (i.publisher) b.price = 'Free' b.url = (i.offer_url) b.reviews = (i.reviews[1]) b.slug = create_slug(b) b.save()
class ProductSearcher(object): def __init__(self, product_name): self.product_name = product_name keys = pandas.read_csv('accessKeys.csv').iloc[0] self.amazon = AmazonAPI(keys['access_key'], keys['access_secret'], 837361066829, region=REGION) def search_product_details(self, product_name=None): product_name = product_name if product_name is not None \ else self.product_name products = self.amazon.search(Keywords=product_name, SearchIndex='All') for product in products: print('product_title={}'.format(product.title)) print('price={}'.format(product.price_and_currency))
def az(): amazon = AmazonAPI("API_KEY", "API_SECRET_KEY", "API_TAG") amazon2 = AmazonAPI("API_KEY", "API_SECRET_KEY", "API_TAG") amazon3 = AmazonAPI("API_KEY", "API_SECRET_KEY", "API_TAG") amazon4 = AmazonAPI("API_KEY", "API_SECRET_KEY", "API_TAG") amazon5 = AmazonAPI("API_KEY", "API_SECRET_KEY", "API_TAG") amazon6 = AmazonAPI("API_KEY", "API_SECRET_KEY", "API_TAG") # To avoid 503 delays aznBin = [amazon, amazon2, amazon3, amazon4, amazon5, amazon6] # items items = [ "B07197V7C7", 'B071CMPRZZ', 'B071L1VGQW', 'B071D8YQJD', 'B07197V7C7', 'B06Y3V1K81', 'B06Y46X4L9', 'B06Y44TWF3', 'B06Y3ZQPY6', 'B06Y45GQ1L', 'B071RW7SCT', 'B06XZZ93FQ', 'B06XZQMMHJ', 'B06XZRWT8D', 'B06Y19NMP3', 'B071VDCCJM', 'B0714B1GNZ', 'B0711QH8ZS', 'B06ZZCV9SY', 'B071QX74F9', 'B071CPJZSX', 'B071XZ867C', 'B07115GPN7', 'B06ZXRLX3H', 'B072B6W44N', 'B071CD6K6Z', 'B06Y66K3XD', 'B06ZYB4C18' ] count = 0 for item in items: # randomize delay 1 sleep(uniform(0.2, 0.3)) # diff API keys if count == len(aznBin): count = 0 amazonNow = aznBin[count] else: amazonNow = aznBin[count] count += 1 delay = True while delay: try: product = amazonNow.lookup(ItemId=item) delay = False except: # when delay 1 not enough print("503... delay 2 sec") sleep(uniform(2, 3)) try: retail_price = float(product.list_price[0]) # print(product.formatted_price) list_price = float(product.price_and_currency[0]) except: list_price = 100.00 retail_price = 0.00 if list_price <= retail_price: print("Amazon - " + product.title + " - In Stock") link = "https://www.amazon.com/dp/" + item sendTweet(link, "Amazon", getName(str(product.title))) else: print("Amazon - " + product.title + " - OOS")
class AmazonApi(SpidersToolBox, AmazonAPI): def __init__(self): super(AmazonApi, self).__init__() self.responsegroup = "Images,ItemAttributes,Offers" self.amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, region="CN", ErrorHandler=self.error_handler) self.sql = """ select stcode from crawl.amazon_base_information201907 where barcode=%s; """ # and stock is not false and crawl_state != 3 def error_handler(self, error): ex = error['exception'] if ex.code == 503: time.sleep(1) return True def func1(self, stcode): url = "https://www.amazon.cn/dp/{}".format(stcode) response = requests.get(url, headers={"User-Agent": self.user_agent}) # html_data = etree.HTML(response.content.decode()) with open("sss.html", "w", encoding="utf-8") as f: f.write(response.text) def func2(self, stcode): try: products = self.amazon.lookup(IdType="ASIN", ItemId=stcode, ResponseGroup=self.responsegroup) a, b = products.price_and_currency except Exception as e: self.logger.debug(e) else: print(a) print(b) def main(self, barcode): sql = self.sql self.conn.execute(sql, (barcode, )) res = self.conn.fetchone() self.func1(res[0])
class AmazonController: def __init__(self): keys = Amazon.objects.all() key = keys[0] self.amazon = AmazonAPI(key.access_key.encode('ascii'), key.secret_key.encode('ascii'), key.assoc_tag.encode('ascii')) def lookup(self, item_id, id_type='ASIN', search_index=''): try: response = self.amazon.lookup(ItemId=item_id, IdType=id_type, SearchIndex=search_index) return response except: return None
def amazon_api(country): if country == 'us': amazon = AmazonAPI('AKIAJ2TPWCFJMKXPSJVQ', 'ixmfea5B2xKFukyuR/aiBzkI6f+umvISvYlzzBBy', 'newer027-20') elif country == 'ca': amazon = AmazonAPI('AKIAI3TBTKER2RZTHNNQ', '9ahA2sNNnzcmDMuxnzLQUrDFKDzLuwuUREEFA3LX', 'newer02703-20', region='CA') elif country == 'fr': amazon = AmazonAPI('AKIAJ2TPWCFJMKXPSJVQ', 'ixmfea5B2xKFukyuR/aiBzkI6f+umvISvYlzzBBy', 'chengjiante0c-21', region='FR') elif country == 'de': amazon = AmazonAPI('AKIAI3TBTKER2RZTHNNQ', '9ahA2sNNnzcmDMuxnzLQUrDFKDzLuwuUREEFA3LX', 'chengjiante06-21', region='DE') elif country == 'it': amazon = AmazonAPI('AKIAI3TBTKER2RZTHNNQ', '9ahA2sNNnzcmDMuxnzLQUrDFKDzLuwuUREEFA3LX', 'chengjiant076-21', region='IT') elif country == 'jp': amazon = AmazonAPI('AKIAJ2TPWCFJMKXPSJVQ', 'ixmfea5B2xKFukyuR/aiBzkI6f+umvISvYlzzBBy', 'newer027-22', region="JP") elif country == 'es': amazon = AmazonAPI('AKIAI3TBTKER2RZTHNNQ', '9ahA2sNNnzcmDMuxnzLQUrDFKDzLuwuUREEFA3LX', 'chengjiant04a-21', region='ES') else: amazon = AmazonAPI('AKIAI3TBTKER2RZTHNNQ', '9ahA2sNNnzcmDMuxnzLQUrDFKDzLuwuUREEFA3LX', 'chengjiante00-21', region='UK') return amazon
def setup(config): """Initializes this module from openlibrary config. """ global config_content_server, config_loanstatus_url, \ config_ia_access_secret, config_bookreader_host, \ config_ia_ol_shared_key, config_ia_ol_xauth_s3, \ config_internal_tests_api_key, config_ia_loan_api_url, \ config_http_request_timeout, config_amz_api, amazon_api, \ config_ia_availability_api_v1_url, config_ia_availability_api_v2_url, \ config_ia_ol_metadata_write_s3, config_ia_xauth_api_url, \ config_http_request_timeout, config_ia_s3_auth_url if config.get("content_server"): try: config_content_server = ContentServer(config.get("content_server")) except Exception as e: logger.exception('Failed to assign config_content_server') else: logger.error('content_server unassigned') config_loanstatus_url = config.get('loanstatus_url') config_bookreader_host = config.get('bookreader_host', 'archive.org') config_ia_loan_api_url = config.get('ia_loan_api_url') config_ia_availability_api_v1_url = config.get( 'ia_availability_api_v1_url') config_ia_availability_api_v2_url = config.get( 'ia_availability_api_v2_url') config_ia_xauth_api_url = config.get('ia_xauth_api_url') config_ia_access_secret = config.get('ia_access_secret') config_ia_ol_shared_key = config.get('ia_ol_shared_key') config_ia_ol_auth_key = config.get('ia_ol_auth_key') config_ia_ol_xauth_s3 = config.get('ia_ol_xauth_s3') config_ia_s3_auth_url = config.get('ia_s3_auth_url') config_ia_ol_metadata_write_s3 = config.get('ia_ol_metadata_write_s3') config_internal_tests_api_key = config.get('internal_tests_api_key') config_http_request_timeout = config.get('http_request_timeout') config_amz_api = config.get('amazon_api') try: amazon_api = AmazonAPI(config_amz_api.key, config_amz_api.secret, config_amz_api.id) except AttributeError: amazon_api = None
def setup(config): """Initializes this module from openlibrary config. """ global config_loanstatus_url, \ config_ia_access_secret, config_bookreader_host, \ config_ia_ol_shared_key, config_ia_ol_xauth_s3, \ config_internal_tests_api_key, config_ia_loan_api_url, \ config_http_request_timeout, config_amz_api, amazon_api, \ config_ia_availability_api_v1_url, config_ia_availability_api_v2_url, \ config_ia_ol_metadata_write_s3, config_ia_xauth_api_url, \ config_http_request_timeout, config_ia_s3_auth_url, \ config_ia_users_loan_history, config_ia_loan_api_developer_key, \ config_ia_civicrm_api, config_ia_domain config_loanstatus_url = config.get('loanstatus_url') config_bookreader_host = config.get('bookreader_host', 'archive.org') config_ia_domain = config.get('ia_base_url', 'https://archive.org') config_ia_loan_api_url = config.get('ia_loan_api_url') config_ia_availability_api_v1_url = config.get( 'ia_availability_api_v1_url') config_ia_availability_api_v2_url = config.get( 'ia_availability_api_v2_url') config_ia_xauth_api_url = config.get('ia_xauth_api_url') config_ia_access_secret = config.get('ia_access_secret') config_ia_ol_shared_key = config.get('ia_ol_shared_key') config_ia_ol_auth_key = config.get('ia_ol_auth_key') config_ia_ol_xauth_s3 = config.get('ia_ol_xauth_s3') config_ia_ol_metadata_write_s3 = config.get('ia_ol_metadata_write_s3') config_ia_s3_auth_url = config.get('ia_s3_auth_url') config_ia_users_loan_history = config.get('ia_users_loan_history') config_ia_loan_api_developer_key = config.get('ia_loan_api_developer_key') config_ia_civicrm_api = config.get('ia_civicrm_api') config_internal_tests_api_key = config.get('internal_tests_api_key') config_http_request_timeout = config.get('http_request_timeout') config_amz_api = config.get('amazon_api') try: amazon_api = AmazonAPI(config_amz_api.key, config_amz_api.secret, config_amz_api.id, MaxQPS=0.9) except AttributeError: amazon_api = None
def add_to_cart(request, cartItems): amazon = AmazonAPI('Access Key', 'Secret Key', 'gs2671-20') flag = True cartItems = eval(cartItems) for item in cartItems: if (flag): product = amazon.lookup(ItemId=item) amazon_cart = amazon.cart_create([{ 'offer_id': product.offer_id, 'quantity': 1 }]) flag = False else: product = amazon.lookup(ItemId=item) amazon.cart_add({ 'offer_id': product.offer_id, 'quantity': 1 }, amazon_cart.cart_id, amazon_cart.hmac) return HttpResponseRedirect(amazon_cart.purchase_url)