Example #1
0
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
Example #2
0
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)
Example #3
0
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)
Example #4
0
    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)
Example #5
0
    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)
Example #6
0
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:
Example #7
0
    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
            }
Example #8
0
    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}
Example #9
0
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)
Example #10
0
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()