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 get_ASIN(etextid): sql_query = """SELECT DISTINCT title, creator, contributor FROM book WHERE id = '""" + etextid + "';" db = connect_to_database("amazon", "root", "gitkotwg0") #repace with password cursor = db.cursor() cursor.execute(sql_query) results = cursor.fetchall() db.close() for title, creator, contributor in results: if contributor != 'NULL': ecs.setLicenseKey('0ZW74MMABE2VX9H26182') try: books = ecs.ItemSearch(Keywords=correct_contrib(contributor) + ' ' + title, SearchIndex='Books', Sort='relevancerank') return books[0].ASIN except KeyError: print "KEYERROR" else: try: ecs.setLicenseKey('0ZW74MMABE2VX9H26182') books = ecs.ItemSearch(title, SearchIndex='Books', Sort='relevancerank') return books[0].ASIN except KeyError: return "KeyError"
def get_ASIN(etextid): sql_query = """SELECT DISTINCT title, creator, contributor FROM book WHERE id = CONVERT( _utf8 '""" + etextid + "'USING latin1) COLLATE latin1_swedish_ci;" db = connect_to_database("amazon", "root", "gitkotwg0") #repalce with password cursor = db.cursor() cursor.execute(sql_query) results = cursor.fetchall() db.close() for title, creator, contributor in results: if contributor != 'NULL': ecs.setLicenseKey('0ZW74MMABE2VX9H26182') try: books = ecs.ItemSearch(Keywords=correct_contrib(contributor) + ' ' + title, SearchIndex='Books', Sort='relevancerank') return books[0].ASIN except KeyError: return "None" else: try: ecs.setLicenseKey('0ZW74MMABE2VX9H26182') books = ecs.ItemSearch(Keywords=title, SearchIndex='Books', Sort='relevancerank') return books[0].ASIN except KeyError: return "None" except ecs.AWSException: return "None" except TypeError: return "None"
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 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 searchAmazon(self,keywords): ecs.setLicenseKey('11GY40BZY8FWYGMMVKG2') ecs.setSecretKey('4BKnT84c3U8EfpgVbTlj4+siFIQo3+TQURTHXhFx') ecs.setLocale('us') books = ecs.ItemSearch(keywords, SearchIndex='Books', ResponseGroup='Medium',AssociateTag='appinventoror-22') amazon_books=self.buildResultList(books) return amazon_books
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 _setup_amazon_keys(): config = ConfigParser() files = config.read([".amazonrc", "~/.amazonrc"]) if not files: print >> sys.stderr, "ERROR: Unable to find .amazonrc with access keys." access_key = config.get("default", "access_key") secret = config.get("default", "secret") ecs.setLicenseKey(access_key) ecs.setSecretAccessKey(secret)
def setup_amazon_keys(self): config = ConfigParser() files = config.read([".amazonrc", "~/.amazonrc"]) if not files: raise Exception("ERROR: Unable to find .amazonrc with access keys.") access_key = config.get("default", "access_key") secret = config.get("default", "secret") ecs.setLicenseKey(access_key) ecs.setSecretAccessKey(secret)
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 searchAmazon(self, keywords): ecs.setLicenseKey('11GY40BZY8FWYGMMVKG2') ecs.setSecretKey('4BKnT84c3U8EfpgVbTlj4+siFIQo3+TQURTHXhFx') ecs.setLocale('us') books = ecs.ItemSearch(keywords, SearchIndex='Books', ResponseGroup='Medium', AssociateTag='appinventoror-22') amazon_books = self.buildResultList(books) return amazon_books
def set_license_key(): ecs = import_pyaws_ecs() # need an AWS key to proceed (see above) import ConfigParser as configparser #anticipate name change cfg = configparser.ConfigParser() cfg.read('bibstuff.cfg') aws_key = cfg.get('isbn2bib', 'aws_key') try: ecs.setLicenseKey(aws_key) except ecs.AWSException: raise RuntimeError("Failed to set key. Do you have a bibstuff.cfg " " file in your current directory?")
def set_license_key(): ecs = import_pyaws_ecs() # need an AWS key to proceed (see above) import ConfigParser as configparser #anticipate name change cfg = configparser.ConfigParser() cfg.read('bibstuff.cfg') aws_key = cfg.get('isbn2bib','aws_key') try: ecs.setLicenseKey(aws_key) except ecs.AWSException: raise RuntimeError("Failed to set key. Do you have a bibstuff.cfg " " file in your current directory?")
def setup_amazon_keys(self): config = ConfigParser() files = config.read([".amazonrc", "~/.amazonrc"]) if not files: raise Exception( "ERROR: Unable to find .amazonrc with access keys.") access_key = config.get("default", "access_key") secret = config.get("default", "secret") ecs.setLicenseKey(access_key) ecs.setSecretAccessKey(secret)
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 amazon_by_keyword(keyword): """ Use the ecs library to search for books by keyword. Args: keyword: A string of keyword(s) to search for. Returns: A list of three item lists. Each sublist represents a result and includes the book title, its lowest found price and its ASIN number. """ ecs.setLicenseKey(license_key) ecs.setSecretKey(secret_key) ecs.setLocale('us') books = ecs.ItemSearch(keyword, SearchIndex='Books', ResponseGroup='Medium') return format_output(books)
def get_books(query, license=license_key, base_dir=resource_dir, useful_till=40): ecs.setLicenseKey(license) books = ecs.ItemSearch(query, SearchIndex="Books", ResponseGroup="Images") books_with_large_images = [] # print len(books) for i in range(useful_till): if hasattr(books[i], "LargeImage"): # print books[i].LargeImage.Width,books[i].MediumImage.Height books_with_large_images.append(books[i]) # print len(books_with_large_images) for i, each_book in enumerate(books_with_large_images): os.chdir(base_dir) if not os.path.exists(query): os.mkdir(query) urllib.urlretrieve(each_book.LargeImage.URL, base_dir + os.path.sep + query + os.path.sep + str(i) + ".jpg") return len(books_with_large_images)
from pyaws import ecs from textbook.models import Book, Subject print "Musn't be run before migration 0012 of textbook app." ecs.setLicenseKey("so key"); ecs.setSecretAccessKey('much secret') Service = 'AWSECommerceService' Operation = 'ItemLookup' AWSAccessKeyId = 'key id' import pdb pdb.set_trace() def add_ancestors(book, node, subject): """Recursive function that adds subjects. book = Book object being dealt with. node = New node that was just added as Subject subject = The Subject just created from new node. Assume that if a subject is added, it has right ancestory. """ if hasattr(node, "Ancestors"): ancestor = node.Ancestors[0] # 0 is referenced because nodes have only one ancestor try: ancestor_subject, created = Subject.objects.get_or_create(name=ancestor.Name, bnid=ancestor.BrowseNodeId) subject.move_to(ancestor_subject) # move subject to ancestor tree as it's new except:
from pyaws import ecs from mooi.models import Book from mooi.models import Author 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()
from pyPdf import PdfFileWriter, PdfFileReader import pyPdf import os import re import sys from pyaws import ecs ecs.setLicenseKey('1P5WQWG01FJRRNESE4G2') #pdfDir='/home/rexa/Desktop/books/Python/' pdfDir='/home/rexa/Desktop/books/hardcases/' res=os.listdir(pdfDir) res.sort() resPDF=[resFile for resFile in res if re.search(r'\.pdf$',resFile)] basePath=pdfDir fileDictList=list() for file in resPDF: pdfFilePath=pdfDir+file rePDF=re.compile(r'\.pdf') reCHM=re.compile(r'\.chm') reISBN=re.compile(r'ISBN') reISBNLine=re.compile(r'^.*?ISBN.*?$') if rePDF.search(file):
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 }
def main(): from optparse import OptionParser cmnd = os.path.basename(sys.argv[0]) parser = OptionParser(version="%prog "+__version__) parser.add_option("-k", "--awskey", dest="awskey", help="specify your AWS-key") parser.add_option("-a", "--author", dest="author", help="search by Author") parser.add_option("-t", "--title", dest="title", help="search by Title") parser.add_option("-i", "--isbn", dest="isbn", help="search by ISBN") parser.add_option("-c", "--count", dest="count", help="show this number of items") parser.add_option("-l", "--language", default="us", metavar="LOCALE", help="search language: fr, ca, de, jp, us, " "or uk [default: %default]") (options, args) = parser.parse_args() if options.awskey: ecs.setLicenseKey(options.awskey) else: print "You have to specify an AWS key!" print "Get one at: http://aws.amazon.com" print "run", cmnd, "--help for additional help" sys.exit(1) try: ecs.setLocale(options.language) except ecs.BadLocale: ecs.setLocale("us") if options.count: count = int(options.count) else: count = 20 # options are mutually exclusive. isbn >> titel >> author if options.isbn: books = ecs.ItemLookup(ItemId=options.isbn, IdType='ISBN', SearchIndex='Books', ResponseGroup="Medium") elif options.title: books = ecs.ItemSearch('', SearchIndex='Books', Title=options.title, ResponseGroup="Medium") elif options.author: books = ecs.ItemSearch('', SearchIndex='Books', Author=options.author, ResponseGroup="Medium") else: print "You have to specify a search query!" print "run", cmnd, "--help for additional help" sys.exit(1) if len(books) == 0: print "Sorry, nothing found" sys.exit(0) count = min(count, len(books)) #print dir(books[0]) #sys.exit(0) for i in range(count): print "%d) %s" % (i+1, books[i].Title) if type(books[i].Author) == list: print "\tby ", " and ".join(books[i].Author) else: print "\tby %s" % (books[i].Author) itemnumber = int(raw_input("Which item do you want? ")) itemnumber -= 1 if type(books[itemnumber].Author) == list: author = " and ".join(books[itemnumber].Author) author = author.strip() else: author = books[itemnumber].Author.strip() author_lastname = author.split()[-1] # last author wins title = books[itemnumber].Title.strip() publisher = books[itemnumber].Publisher.strip() year = books[itemnumber].PublicationDate.strip().split('-')[0] isbn = books[itemnumber].ISBN.strip() # output entry: print "BibTex-entry:" print "@book{%s%s," % (author_lastname.lower()[:3], year[2:]) print ' author = "%s",' % (author) print ' title = "%s",' % (title) print ' publisher = "%s",' % (publisher) print ' year = "%s",' % (year) print ' isbn = "%s"' % (isbn) print "}"
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}
from pyaws import ecs from mooi.models import Book from mooi.models import Author 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)
#prepare a logger import logging logging.basicConfig(format='\n%(levelname)s:\n%(message)s\n') isbn2bib_logger = logging.getLogger('bibstuff_logger') #we need pyaws to get data from Amazon from pyaws import ecs #need an AWS key to proceed (see above) import ConfigParser as configparser #anticipate name change cfg = configparser.ConfigParser() cfg.read('bibstuff.cfg') aws_key = cfg.get('isbn2bib','aws_key') try: ecs.setLicenseKey(aws_key) except AWSException: print """Failed to set key. Do you have a bibstuff.cfg file in your current directory? """ #unfortunately, addresses are not available in bookinfo # hope it's in my list ... publisher_addresses = dict() fh = open('data/publisher_addresses.txt','r') for line in fh: if line.startswith('#') or not line.strip(): continue info = tuple(item.strip() for item in line.split('|') ) try: