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 []
def test_lookup_by_isbn13_has_extra_hyphens(self): random_item=random.sample(list(Title.select('isbn RLIKE \'[0-9]{9}[0-9xX]\'')), 1)[0] converted_isbn=isbn.convert(random_item.isbn) converted_isbn=converted_isbn[0:3]+'-'+converted_isbn[3:8]+'-'+converted_isbn[8:] result=inventory.lookup_by_isbn(converted_isbn) self.assertEqual(random_item.isbn, result['isbn'], 'inventory.lookup_by_isbn returned wrong isbn for random isbn in database')
def test_lookup_by_isbn13_is_invalid(self): random_item=random.sample(list(Title.select('isbn RLIKE \'[0-9]{9}[0-9xX]\'')), 1)[0] converted_isbn=isbn.convert(random_item.isbn) wrong_isbn=converted_isbn[0:12] + str((int(converted_isbn[12]) + 1) % 10) result=inventory.lookup_by_isbn(wrong_isbn) self.assertEqual(random_item.isbn, result['isbn'], 'inventory.lookup_by_isbn returned wrong isbn for invalid isbn13')
def test_lookup_by_isbn13_is_valid(self): random_item=random.sample(list(Title.select('isbn RLIKE \'[0-9]{9}[0-9xX]\'')), 1)[0] converted_isbn=isbn.convert(random_item.isbn) result=inventory.lookup_by_isbn(converted_isbn) self.assertEqual(random_item.isbn, result['isbn'], 'inventory.lookup_by_isbn returned wrong isbn for random isbn in database')