Beispiel #1
0
 def testBuildQuery(self):
     '''example taken from the api-documentation'''
     ecs.setSecretAccessKey('1234567890')
     
     args = {
             'Service':'AWSECommerceService',
             'AWSAccessKeyId':'00000000000000000000',
             'Timestamp':'2009-01-01T12:00:00Z',
             'Operation':'CartCreate',
             'Item.1.OfferListingId':'j8ejq9wxDfSYWf2OCp6XQGDsVrWhl08GSQ9m5j+e8MS449BN1XGUC3DfU5Zw4nt/FBt87cspLow1QXzfvZpvzg==',
             'Item.1.Quantity':'3',
             'AssociateTag':'mytag-20',
             'Version':'2009-01-01'
     }
     expepected_url = 'http://ecs.amazonaws.com/onca/xml?AWSAccessKeyId=00000000000000000000&AssociateTag=mytag-20&Item.1.OfferListingId=j8ejq9wxDfSYWf2OCp6XQGDsVrWhl08GSQ9m5j%2Be8MS449BN1XGUC3DfU5Zw4nt%2FFBt87cspLow1QXzfvZpvzg%3D%3D&Item.1.Quantity=3&Operation=CartCreate&Service=AWSECommerceService&Timestamp=2009-01-01T12%3A00%3A00Z&Version=2009-01-01&Signature=cF3UtjbJb1%2BxDh387C%2FEmS1BCtS%2FZ01taykBCGemvUU%3D'
     self.assertEqual(ecs.buildQuery(args),expepected_url)
Beispiel #2
0
    def testBuildQuery(self):
        '''example taken from the api-documentation'''
        ecs.setSecretAccessKey('1234567890')

        args = {
            'Service': 'AWSECommerceService',
            'AWSAccessKeyId': '00000000000000000000',
            'Timestamp': '2009-01-01T12:00:00Z',
            'Operation': 'CartCreate',
            'Item.1.OfferListingId':
            'j8ejq9wxDfSYWf2OCp6XQGDsVrWhl08GSQ9m5j+e8MS449BN1XGUC3DfU5Zw4nt/FBt87cspLow1QXzfvZpvzg==',
            'Item.1.Quantity': '3',
            'AssociateTag': 'mytag-20',
            'Version': '2009-01-01'
        }
        expepected_url = 'http://ecs.amazonaws.com/onca/xml?AWSAccessKeyId=00000000000000000000&AssociateTag=mytag-20&Item.1.OfferListingId=j8ejq9wxDfSYWf2OCp6XQGDsVrWhl08GSQ9m5j%2Be8MS449BN1XGUC3DfU5Zw4nt%2FFBt87cspLow1QXzfvZpvzg%3D%3D&Item.1.Quantity=3&Operation=CartCreate&Service=AWSECommerceService&Timestamp=2009-01-01T12%3A00%3A00Z&Version=2009-01-01&Signature=cF3UtjbJb1%2BxDh387C%2FEmS1BCtS%2FZ01taykBCGemvUU%3D'
        self.assertEqual(ecs.buildQuery(args), expepected_url)
Beispiel #3
0
    def lookup_by_isbn(self,number):
        isbn=""
        number=re.sub("^([\'\"])(.*)\\1$", '\\2', number)
        #print "number is now: ", number
        if len(number)>=9:
            number=re.sub("[-\s]", '', number)
        #print "number is now: ", number
        if len(number)==10 and isbnlib.isValid(number):
            isbn=isbnlib.convert(number)

        else:
            isbn=number
        #print "NUMBER was " +number+ ",ISBN was "+isbn
        if (len(isbn)>0 and not re.match('^n(\s|/){0,1}a|none', isbn, re.I)):
            #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:
                #print "in titles"
                self.known_title= the_titles[0]
                ProductName = the_titles[0].booktitle.format()
                authors=[]
                if len(the_titles[0].author) > 0:
                    authors = [x.authorName.format() for x in the_titles[0].author]
                authors_as_string = string.join(authors,',')
                categories=[]
                if len(the_titles[0].categorys) > 0:
                    #print len(the_titles[0].categorys)
                    #print the_titles[0].categorys
                    categories = [x.categoryName.format() 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
                else:
                    ListPrice = 0
                Manufacturer = the_titles[0].publisher.format()
                Format=the_titles[0].type.format()
                Kind=the_titles[0].kind.kindName
                return {"title":ProductName,
                    "authors":authors,
                    "authors_as_string":authors_as_string,
                    "categories_as_string":categories_as_string,
                    "list_price":ListPrice,
                    "publisher":Manufacturer,
                    "isbn":isbn,
                    "format":Format,
                    "kind":Kind,
                    "known_title": self.known_title}
            else: #we don't have it yet
                #print "in isbn"
                sleep(1) # so amazon doesn't get huffy 
                ecs.setLicenseKey(amazon_license_key)
                ecs.setSecretAccessKey(amazon_secret_key)
                ecs.setAssociateTag(amazon_associate_tag)
                
                #print "about to search", isbn, isbn[0]
                pythonBooks=[]
                try:
                    pythonBooks = ecs.ItemLookup(isbn,IdType="ISBN",SearchIndex="Books",ResponseGroup="ItemAttributes,BrowseNodes")
                except ecs.InvalidParameterValue:
                    pass
                #print pythonBooks
                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 =""
                    
                    # a bit more complicated of a tree walk than it needs be.
                    # set up to still have the option of category strings like "history -- us"
                    # switched to sets to quickly remove redundancies.
                    def parseBrowseNodes(bNodes):
                        def parseBrowseNodesInner(item):
                            bn=set()
                            if hasattr(item, 'Name'):
                                bn.add(item.Name)
                            if hasattr(item, 'Ancestors'):
                                #print "hasansc"   
                                for i in item.Ancestors:
                                    bn.update(parseBrowseNodesInner(i))
                            if hasattr(item, 'Children'):
                                for i in item.Children:
                                    bn.update(parseBrowseNodesInner(i))
                                    #print "bn ", bn
                            if not (hasattr(item, 'Ancestors') or hasattr(item, 'Children')):            
                                if hasattr(item, 'Name'):
                                    return set([item.Name])
                                else:
                                    return set()
                            return bn
                        nodeslist=[parseBrowseNodesInner(i) for i in bNodes ]
                        nodes=set()
                        for n in nodeslist:
                            nodes = nodes.union(n)
                        return nodes

                    categories=parseBrowseNodes(b.BrowseNodes)
                    categories_as_string = string.join(categories,',')                   

                    categories=parseBrowseNodes(b.BrowseNodes)
                    categories_as_string = string.join(categories,',')
                    #print categories, 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.replace("$",'')

                    Format=''
                    if hasattr(b, "Binding"):
                        Format=b.Binding
                    
                    Kind='books'
                    
                    return {"title":ProductName,
                        "authors":authors,
                        "authors_as_string":authors_as_string,
                        "categories_as_string":categories_as_string,
                        "list_price":ListPrice,
                        "publisher":Manufacturer,
                        "isbn":isbn,
                        "format":Format,
                        "kind":Kind,
                        "known_title": self.known_title}
                else:
                    return []
                
       
        else:
            return []
Beispiel #4
0
    def lookup_by_upc(self,upc):

        if len(upc)>0:
            #first we check our database
            titles =  Title.select(Title.q.isbn==upc)
            #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
                authors = [x.authorName for x in the_titles[0].authors]
                authors_as_string = string.join(authors,',')
                categories = [x.categoryName 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
                else:
                    ListPrice = 0
                Manufacturer = the_titles[0].publisher
                
            else: #we don't have it yet
                sleep(1) # so amazon doesn't get huffy 
                ecs.setLicenseKey(amazon_license_key)
                ecs.setSecretAccessKey(amazon_secret_key)
                ecs.setAssociatTag(amazon_associate_tag)
                pythonItems = ecs.searchByUPC(upc)
                if pythonItems:
                    result={}
                    authors=[]
                    author_object="none"
                    categories=[]
                    b=pythonItems[0]
                    try:
                        author_object=b.Artists.Artist
                    except AttributeError:
                        author_object="none"
                    if type(author_object) in types.StringTypes:
                        authors.append(author_object)
                    else: 
                        authors=author_object
                    authors_as_string = string.join(authors,',')

                    for category in b.BrowseNode:
                        categories.append(category.BrowseName)

                    categories_as_string = string.join(categories,',')
                 
                    ProductName=""
                    try:
                        if b.ProductName:
                            ProductName=b.ProductName
                    except AttributeError:
                        x=1
                        
                    Manufacturer=""
                    try:
                        if b.Manufacturer:
                            Manufacturer=b.Manufacturer
                    except AttributeError:
                        x=1

                    ListPrice=""
                    try:
                        if b.ListPrice:
                            ListPrice=b.ListPrice
                    except AttributeError:
                        x=1

                    ReleaseDate=""
                    try:
                        if b.ReleaseDate:
                            ReleaseDate=b.ReleaseDate
                    except AttributeError:
                        x=1
                    
            return {"title":ProductName,
                    "authors":authors,
                    "authors_as_string":authors_as_string,
                    "categories_as_string":categories_as_string,
                    "list_price":ListPrice,
                    "publisher":Manufacturer,
                    "isbn":upc,
                    "known_title": self.known_title}
Beispiel #5
0
 def testSetSecretKey(self):
     ecs.setSecretAccessKey('1234')
     self.assertEqual(ecs.getSecretAccessKey(), '1234')
Beispiel #6
0
    def lookup_by_isbn(number):
        isbn = ""
        number = re.sub("^(['\"])(.*)\\1$", "\\2", number)
        # print "number is now: ", number
        if len(number) >= 9:
            number = re.sub("[-\s]", "", number)
        # print "number is now: ", number
        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
            known_title = False
            the_titles = list(titles)
            if len(the_titles) > 0:
                # print "in titles"
                known_title = the_titles[0]
                ProductName = the_titles[0].booktitle.format()
                authors = [x.authorName.format() for x in the_titles[0].author] or []
                authors_as_string = string.join(authors, ",")
                if len(the_titles[0].categorys) > 0:
                    # print len(the_titles[0].categorys)
                    # print the_titles[0].categorys
                    categories = [x.categoryName.format() 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
                else:
                    ListPrice = 0
                Manufacturer = the_titles[0].publisher.format()
                Format = the_titles[0].type.format()
                Kind = the_titles[0].kind.kindName

            else:  # we don't have it yet
                # print "in isbn"
                sleep(1)  # so amazon doesn't get huffy
                ecs.setLicenseKey(amazon_license_key)
                ecs.setSecretAccessKey(amazon_secret_key)
                ecs.setAssociateTag(amazon_associate_tag)

                # print "about to search", isbn, isbn[0]
                pythonBooks = ecs.ItemLookup(
                    isbn, IdType="ISBN", SearchIndex="Books", ResponseGroup="ItemAttributes,BrowseNodes"
                )
                # print pythonBooks
                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 = ""

                    # a bit more complicated of a tree walk than it needs be.
                    # set up to still have the option of category strings like "history -- us"
                    # switched to sets to quickly remove redundancies.
                    def parseBrowseNodes(bNodes):
                        def parseBrowseNodesInner(item):
                            bn = set()
                            if hasattr(item, "Name"):
                                bn.add(item.Name)
                            if hasattr(item, "Ancestors"):
                                # print "hasansc"
                                for i in item.Ancestors:
                                    bn.update(parseBrowseNodesInner(i))
                            if hasattr(item, "Children"):
                                for i in item.Children:
                                    bn.update(parseBrowseNodesInner(i))
                                    # print "bn ", bn
                            if not (hasattr(item, "Ancestors") or hasattr(item, "Children")):
                                if hasattr(item, "Name"):
                                    return set([item.Name])
                                else:
                                    return set()
                            return bn

                        nodeslist = [parseBrowseNodesInner(i) for i in bNodes]
                        nodes = set()
                        for n in nodeslist:
                            nodes = nodes.union(n)
                        return nodes

                    categories = parseBrowseNodes(b.BrowseNodes)
                    categories_as_string = string.join(categories, ",")

                    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.replace("$", "")

                    Format = ""
                    if hasattr(b, "Binding"):
                        Format = b.Binding

                    Kind = "books"

            return {
                "title": ProductName,
                "authors": authors,
                "authors_as_string": authors_as_string,
                "categories_as_string": categories_as_string,
                "list_price": ListPrice,
                "publisher": Manufacturer,
                "isbn": isbn,
                "format": Format,
                "kind": Kind,
                "known_title": known_title,
            }
Beispiel #7
0
 def testSetSecretKey(self):
     ecs.setSecretAccessKey('1234')
     self.assertEqual(ecs.getSecretAccessKey(), '1234')