def amazon_by_isbn(isbn): """ Use the ecs library to search for books by ISBN number. Args: isbn: The 10 digit ISBN number to look up. Returns: A list with a single sublist representing the book found. The sublist contains the book title, its lowest found price and its ASIN number. Raises: ValueError if the ISBN number is invalid. """ ecs.setLicenseKey(license_key) ecs.setSecretKey(secret_key) ecs.setLocale('us') try: books = ecs.ItemLookup(isbn, IdType='ISBN', SearchIndex='Books', ResponseGroup='Medium') return format_output(books) except ecs.InvalidParameterValue: raise ValueError('Invalid ISBN')
def make_entry(isbn): """ Return a bibfile.BibEntry instance. Calls `make_bookdict`; called by `main`. :date: 2008-08-31 :todo: this is reusing too much add2bib code """ ecs = import_pyaws_ecs() import bibfile entry = bibfile.BibEntry() entry.entry_type = 'book' try: bkinfo = ecs.ItemLookup(ItemId=isbn, IdType='ISBN', SearchIndex="Books", ResponseGroup="Medium") except ecs.AWSException: print "ItemLookup failed" raise bkdict = make_bookdict(bkinfo) entry.citekey = bkdict['citekey'] del bkdict['citekey'] #leaving only real field #entry.update(bkdict) #TODO: why does this not work? for k, v in bkdict.items(): entry[k] = v return entry
def get_book_data(ISBN): """Returns an Amazon API book object if ISBN is valid, otherwise returns False. All non-alpha-num characters are removed from ISBN. """ if not ISBN: return False # clean ISBN -- remove all non alpha-num chars ISBN = re.sub(r'[^a-zA-Z0-9]', '', ISBN) # setup connection ecs.setLicenseKey(settings.AWS_LICENSE_KEY) ecs.setSecretAccessKey(settings.AWS_SECRET_ACCESS_KEY) Service = 'AWSECommerceService' AWSAccessKeyId = settings.AWS_ACCESS_KEY_ID ItemId = ISBN # run lookup try: books = ecs.ItemLookup( ItemId, IdType='ISBN', SearchIndex='Books', ResponseGroup='Images,ItemAttributes,BrowseNodes') book = books[0] return book except ecs.InvalidParameterValue: return False
def amazon_get_image(ASIN, size="LargeImage"): images = [] ecs.setLicenseKey(license_key) result = ecs.ItemLookup(ASIN, ResponseGroup="Images") for item in result: try: images.append(item.LargeImage.URL) except AttributeError: images.append("None") return images
def avarage_rating(ASIN): if ASIN: ecs.setLicenseKey(license_key) try: result = ecs.ItemLookup(ASIN, ResponseGroup = "Reviews") return result[0].CustomerReviews.AverageRating except ecs.AWSException, e: return "None" except AttributeError, e: return "None"
def amazonByISBN(self, isbn): ecs.setLicenseKey('11GY40BZY8FWYGMMVKG2') ecs.setSecretKey('4BKnT84c3U8EfpgVbTlj4+siFIQo3+TQURTHXhFx') ecs.setLocale('us') books = ecs.ItemLookup(isbn, IdType='ISBN', SearchIndex='Books', ResponseGroup='Medium') amazon_books = self.buildResultList(books) return amazon_books
def amazon_similarities(ASIN): try: similarities = [] ecs.setLicenseKey(license_key) result = ecs.ItemLookup(ASIN, ResponseGroup = "Similarities") for item in result: for product in item.SimilarProducts.SimilarProduct: similarities.append((product.Title, product.ASIN)) return similarities except AttributeError: return []
def amazon_editorial_review(ASIN): try: editorials = [] ecs.setLicenseKey(license_key) result = ecs.ItemLookup(ASIN, ResponseGroup = "EditorialReview") for review in result: try: for editorial_review in review.EditorialReviews: editorials.append((editorial_review.Rating, html2txt(editorial_review.Summary), html2txt(editorial_review.Content))) except TypeError: editorials.append(html2txt(review.EditorialReviews.EditorialReview.Content)) return editorials except AttributeError: return []
def amazon_customer_review(ASIN): try: reviews = [] ecs.setLicenseKey(license_key) result = ecs.ItemLookup(ASIN, ResponseGroup = "Reviews") try: for review in result[0].CustomerReviews.Review: reviews.append((review.Rating, html2txt(review.Summary), html2txt(review.Content))) except AttributeError: return [] return reviews except AttributeError: return [] except ecs.MinimumParameterRequirement: return []
def lookup_by_isbn(self, number): isbn = "" if len(number) == 13 or len(number) == 18: isbn = upc2isbn(number) else: isbn = number print "NUMBER was " + number + ",ISBN was " + isbn if len(isbn) > 0: #first we check our database titles = Title.select(Title.q.isbn == isbn) print titles #debug self.known_title = False the_titles = list(titles) if len(the_titles) > 0: self.known_title = the_titles[0] ProductName = the_titles[0].booktitle.decode("unicode_escape") authors = [ x.author_name.decode("unicode_escape") for x in the_titles[0].author ] authors_as_string = string.join(authors, ',') categories = [ x.categoryName.decode("unicode_escape") for x in the_titles[0].categorys ] categories_as_string = string.join(categories, ',') if len(the_titles[0].books) > 0: # ListPrice = the_titles[0].books[0].listprice ListPrice = max([b.listprice for b in the_titles[0].books]) else: ListPrice = 0 Manufacturer = the_titles[0].publisher.decode("unicode_escape") else: #we don't have it yet sleep(1) # so amazon doesn't get huffy ecs.setLicenseKey(amazon_license_key) ecs.setSecretAccessKey( 'hCVGbeXKy2lWQiA2VWV8iWgti6s9CiD5C/wxL0Qf') ecs.setOptions({'AssociateTag': 'someoneelse-21'}) pythonBooks = ecs.ItemLookup(isbn, IdType="ISBN", SearchIndex="Books", ResponseGroup="ItemAttributes") if pythonBooks: result = {} authors = [] categories = [] b = pythonBooks[0] for x in ['Author', 'Creator']: if hasattr(b, x): if type(getattr(b, x)) == type([]): authors.extend(getattr(b, x)) else: authors.append(getattr(b, x)) authors_as_string = string.join(authors, ',') categories_as_string = "" ProductName = "" if hasattr(b, 'Title'): ProductName = b.Title Manufacturer = "" if hasattr(b, 'Manufacturer'): Manufacturer = b.Manufacturer ListPrice = "" if hasattr(b, 'ListPrice'): ListPrice = b.ListPrice.FormattedPrice return { "title": ProductName, "authors": authors, "authors_as_string": authors_as_string, "categories_as_string": categories_as_string, "list_price": ListPrice, "publisher": Manufacturer, "isbn": isbn, "known_title": self.known_title }
ecs.setLicenseKey("license") ecs.setSecretAccessKey('secret') Service = 'AWSECommerceService' Operation = 'ItemLookup' AWSAccessKeyId = 'key id' for book in Book.objects.all(): if book.authors.count() > 3: ItemId = book.ISBN book_authors = book.authors.all() print book_authors prompt = raw_input('Need fixing? ') if "y" == prompt[0]: print "Fixing....", amazon_books = ecs.ItemLookup( ItemId, ResponseGroup='Images,ItemAttributes') amazon_book = amazon_books[0] try: a = Author(name=amazon_book.Author) except AttributeError: print "failed: %s" % (book) continue a.save() book.authors.add(a) for book_author in book_authors: if book_author != a: book.authors.remove(book_author) book.save() print "done."