def titles(self, letter=None): alphabet = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ] the_titles = False if letter != None: if letter in alphabet: the_titles = Title.select(""" title.booktitle LIKE "%s%%" AND book.title_id=title.id AND book.status ='STOCK' """ % (escape_string(letter)), orderBy="booktitle", clauseTables=['book'], distinct=True) else: the_titles = Title.select(""" title.booktitle RLIKE "^[0-9]" AND book.title_id=title.id AND book.status ='STOCK' """, orderBy="booktitle", clauseTables=['book'], distinct=True) return dict(authorswidget=AuthorsWidget(), titlelistwidget=TitleListWidget(), the_titles=the_titles, the_letter=letter, alphabet=alphabet) else: return dict(authorswidget=AuthorsWidget(), titlelistwidget=TitleListWidget(), the_titles=False, the_letter=letter, alphabet=alphabet)
def unconfirmed(self, letter=""): all_titles = [] if letter == "": all_titles = Title.select("kind_id=1", orderBy='booktitle') else: all_titles = Title.select( """kind_id=1 and booktitle LIKE '%s%%'""" % (escape_string(letter)), orderBy='booktitle') unconfirmed_titles = [] for t in all_titles: if t.copies_in_status( 'STOCK') > 0: # or t.copies_in_status('UNKNOWN')>0: t.unconfirmed_books = [] for b in [ b for b in t.books if (b.status == 'STOCK' or b.status == 'UNKNOWN') ]: tags = b.get_tags( 'inventory', 'confirmation11') #needs a date eventually! if len(tags) == 0: t.unconfirmed_books.append(b) if len(t.unconfirmed_books) > 0: unconfirmed_titles.append(t) return dict(titles=unconfirmed_titles)
def outofstock(self,letter=""): all_titles=[] if letter=="": all_titles=Title.select("kind_id=1",orderBy='booktitle') else: all_titles=Title.select("""kind_id=1 and booktitle LIKE '%s%%'""" % (escape_string(letter)),orderBy='booktitle') return dict(titles=[t for t in all_titles if t.copies_in_status("STOCK")==0])
def uncategorized(self,letter=""): all_titles=[] if letter=="": all_titles=Title.select("kind_id=1",orderBy='booktitle') else: all_titles=Title.select("""kind_id=1 and booktitle LIKE '%s%%'""" % (escape_string(letter)),orderBy='booktitle') return dict(titles=[t for t in all_titles if len(list(t.sections))==0])
def uncategorized(self, letter=""): all_titles = [] if letter == "": all_titles = Title.select("kind_id=1", orderBy='booktitle') else: all_titles = Title.select( """kind_id=1 and booktitle LIKE '%s%%'""" % (escape_string(letter)), orderBy='booktitle') return dict( titles=[t for t in all_titles if len(list(t.sections)) == 0])
def outofstock(self, letter=""): all_titles = [] if letter == "": all_titles = Title.select("kind_id=1", orderBy='booktitle') else: all_titles = Title.select( """kind_id=1 and booktitle LIKE '%s%%'""" % (escape_string(letter)), orderBy='booktitle') return dict( titles=[t for t in all_titles if t.copies_in_status("STOCK") == 0])
def storefront(self): searchform = widgets.TableForm(fields=SearchFields(), submit_text="Search!") conn = db.connect() cursor = conn.cursor() cursor.execute(""" select booktitle,count(book.id) as blah,title_id from book,title where book.title_id=title.id and book.status='SOLD' and title.kind_id=1 group by title_id order by blah desc limit 30 """) results = cursor.fetchall() cursor.close() best_sellers = [ Title.get(x[2]) for x in results if Title.get(x[2]).copies_in_status("STOCK") > 0 ] new_titles = Title.select(""" book.title_id=title.id AND book.status ='STOCK' """, orderBy="-title.id", clauseTables=['book'], distinct=True) return dict(authorswidget=AuthorsWidget(), titlelistwidget=TitleListWidget(), searchform=searchform, new_titles=new_titles[:10], best_sellers=best_sellers)
def title(self, id): ratingsform = widgets.RemoteForm(fields=RatingFields(), submit_text="Rate it!") # Title.sqlmeta.addJoin(MultipleJoin('Rating',joinMethodName='ratings')) thetitle = Title.get(id) thetitle.ratings = [] same_author = [] for author in thetitle.author: titles = Title.select( """ title.id!=%s AND book.title_id=title.id AND book.status ='STOCK' AND author.id=author_title.author_id AND author_title.title_id=title.id AND author.author_name='%s' """ % (escape_string( "%s" % thetitle.id), escape_string(author.author_name)), orderBy="booktitle", clauseTables=['book', 'author', 'author_title'], distinct=True) for title in titles: same_author.append(title) if thetitle.copies_in_status("STOCK") == 0: raise HTTPError(404) return dict(thetitle=thetitle, authorswidget=AuthorsWidget(), ratingsform=ratingsform, titlelistwidget=TitleListWidget(), same_author=same_author)
def do_easyselect_wizard(frame_object, event, label): wizard = EasySelectWizard(None, -1, label) wizard.label = label wizard.parent = frame_object if Kind.select(Kind.q.kindName == label).count() < 1: print "you probably need to add a kind to correspond to the label %s" % label title_list = Title.select( """ title.kind_id= kind.id AND kind.kind_name='%s' """ % (label), orderBy=Title.q.booktitle, clauseTables=["kind"], distinct=True, ) # wizard.title_list=[t for t in title_list if len([b for b in t.books if b.status=="STOCK"])>0] wizard.title_list = title_list wizard.title_page = ChooseItemPage(wizard) wizard.GetPageAreaSizer().Add(wizard.title_page) wizard.action_page = ChooseActionPage(wizard) EVT_WIZARD_FINISHED(wizard, wizard.GetId(), wizard.finish) wizard.RunWizard(wizard.title_page) wizard.Destroy()
def reinventory(self, isbn="", author="", title=""): searchform = widgets.TableForm(fields=PrivateSearchFields(), submit_text="Search!") if author == "" and title == "" and isbn == "": the_titles = False else: the_titles = Title.select(""" title.isbn LIKE '%%%s%%' AND author.id=author_title.author_id AND author_title.title_id=title.id AND author.author_name LIKE '%%%s%%' AND title.booktitle LIKE '%%%s%%' """ % (escape_string(isbn), escape_string(author), escape_string(title)), orderBy="booktitle", clauseTables=['author', 'author_title'], distinct=True) title_count = 0 try: title_count = the_titles.count() except: pass if title_count > 0: if the_titles.count() == 1: return self.title(the_titles[0].id, searchvalues=dict(author=author, title=title, isbn=isbn)) return dict(the_titles=the_titles, authorswidget=PrivateAuthorsWidget(), titlelistwidget=PrivateTitleListWidget(), searchform=searchform, values=dict(author=author, title=title, isbn=isbn))
def test_search_isbn_that_we_have_unit(self): random_item = random.sample(list(Title.select()), 1)[0] result = self._my_class.search_isbn(**{"isbn": random_item.isbn})[0] self.assertEqual( result["isbn"], random_item.isbn, "method search_isbn doesn't return proper item for isbn10", )
def test_lookup_by_orig_isbn_is_invalid(self): random_item = random.sample(list(Title.select("isbn RLIKE '^[0-9]{13}$'")), 1)[ 0 ] wrong_isbn = random_item.isbn[0:12] + str((int(random_item.isbn[12]) + 1) % 10) print(random_item.isbn, wrong_isbn) with self.assertRaises((isbnlib.NotValidISBNError, isbnlib._exceptions.NotValidISBNError)): inventory.lookup_by_isbn(wrong_isbn)
def titles(self,letter=None): alphabet=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] the_titles=False if letter != None: if letter in alphabet: the_titles=Title.select(""" title.booktitle LIKE "%s%%" AND book.title_id=title.id AND book.status ='STOCK' """ % (escape_string(letter)),orderBy="booktitle",clauseTables=['book'],distinct=True) else: the_titles=Title.select(""" title.booktitle RLIKE "^[0-9]" AND book.title_id=title.id AND book.status ='STOCK' """ ,orderBy="booktitle",clauseTables=['book'],distinct=True) return dict(authorswidget=AuthorsWidget(),titlelistwidget=TitleListWidget(),the_titles=the_titles,the_letter=letter,alphabet=alphabet) else: return dict(authorswidget=AuthorsWidget(),titlelistwidget=TitleListWidget(),the_titles=False,the_letter=letter,alphabet=alphabet)
def author(self,id): the_author=Author.get(id) the_titles=Title.select(""" book.title_id=title.id AND book.status ='STOCK' AND author.id=author_title.author_id AND author_title.title_id=title.id AND author.author_name='%s' """ % (escape_string(the_author.author_name)),orderBy="booktitle",clauseTables=['book','author','author_title'],distinct=True) return dict(the_titles=the_titles,the_author=the_author,authorswidget=AuthorsWidget(),titlelistwidget=TitleListWidget())
def unconfirmed(self,letter=""): all_titles=[] if letter=="": all_titles=Title.select("kind_id=1",orderBy='booktitle') else: all_titles=Title.select("""kind_id=1 and booktitle LIKE '%s%%'""" % (escape_string(letter)),orderBy='booktitle') unconfirmed_titles=[] for t in all_titles: if t.copies_in_status('STOCK')>0:# or t.copies_in_status('UNKNOWN')>0: t.unconfirmed_books=[] for b in [b for b in t.books if (b.status=='STOCK' or b.status=='UNKNOWN')]: tags=b.get_tags('inventory','confirmation11') #needs a date eventually! if len(tags)==0: t.unconfirmed_books.append(b) if len(t.unconfirmed_books)>0: unconfirmed_titles.append(t) return dict(titles=unconfirmed_titles)
def test_print_label_unit(self): random_item = random.sample( list(Title.select('WHERE title.isbn RLIKE "^978"')), 1)[0] result = self._my_class.print_label( isbn=random_item.isbn, booktitle=random_item.booktitle, authorstring=random_item.authors_as_string(), ) self.assertIsNone(result, "print_label doesn't return")
def test_titleedit_functional(self): random_item = random.sample(list(Title.select()), 1)[0] response = self._my_app.get("/titleedit", {"id": random_item.id}) code, error = tidylib.tidy_document(response.body, options={ "show-errors": 1, "show-warnings": 0 }) self.assertFalse(error, "/titleedit did not return valid html page")
def test_lookup_by_orig_isbn_is_valid(self): random_item = random.sample(list(Title.select("isbn RLIKE '^[0-9]{13}$'")), 1)[ 0 ] result = inventory.lookup_by_isbn(random_item.isbn) self.assertEqual( random_item.isbn, result["isbn"], "inventory.lookup_by_isbn returned wrong isbn for random isbn in database", )
def test_search_isbn_that_we_dont_have_functional(self): random_item = random.sample(list(Title.select()), 1)[0] response = self._my_app.get("/admin/search_isbn", { "isbn": random_item.isbn }).json[0] self.assertEqual( response["isbn"], random_item.isbn, "/admin/search_isbn doesn't return proper item for isbn10", )
def test_lookup_by_isbn_is_wsr(self): random_item = random.sample( list(Title.select("isbn RLIKE 'wsr [0-9]{3,5}'")), 1 )[0] result = inventory.lookup_by_isbn(random_item.isbn) self.assertEqual( random_item.isbn, result["isbn"], "inventory.lookup_by_isbn returned wrong isbn for random isbn (wsr) in database", )
def search(self,author="",title=""): searchform = widgets.TableForm(fields=SearchFields(), submit_text="Search!") if author == "" and title=="": the_titles=False else: the_titles=Title.select(""" book.title_id=title.id AND book.status ='STOCK' AND author.id=author_title.author_id AND author_title.title_id=title.id AND author.author_name LIKE '%%%s%%' AND title.booktitle LIKE '%%%s%%' """ % (escape_string(author),escape_string(title)),orderBy="booktitle",clauseTables=['book','author','author_title'],distinct=True) return dict(the_titles=the_titles,authorswidget=AuthorsWidget(),titlelistwidget=TitleListWidget(),searchform=searchform,values=dict(author=author,title=title))
def test_print_label_functional(self): random_item = random.sample( list(Title.select('WHERE title.isbn RLIKE "^978"')), 1)[0] response = self._my_app.post( "/admin/print_label", { "isbn": random_item.isbn, "booktitle": random_item.booktitle, "authorstring": random_item.authors_as_string(), }, ) print(response) self.assertIsNone(response, "print_label doesn't return")
def test_select_item_for_isbn_search_functional(self): random_item = random.sample(list(Title.select()), 1)[0] response = self._my_app.get("/admin/select_item_for_isbn_search", {"isbn": random_item.isbn}) code, error = tidylib.tidy_document(response.body, options={ "show-errors": 1, "show-warnings": 0 }) self.assertFalse( error, "/admin/select_item_for_isbn_search did not return proper response" )
def author(self, id): the_author = Author.get(id) the_titles = Title.select( """ book.title_id=title.id AND book.status ='STOCK' AND author.id=author_title.author_id AND author_title.title_id=title.id AND author.author_name='%s' """ % (escape_string(the_author.author_name)), orderBy="booktitle", clauseTables=['book', 'author', 'author_title'], distinct=True) return dict(the_titles=the_titles, the_author=the_author, authorswidget=AuthorsWidget(), titlelistwidget=TitleListWidget())
def storefront(self): searchform = widgets.TableForm(fields=SearchFields(), submit_text="Search!") conn=db.connect() cursor=conn.cursor() cursor.execute(""" select booktitle,count(book.id) as blah,title_id from book,title where book.title_id=title.id and book.status='SOLD' and title.kind_id=1 group by title_id order by blah desc limit 30 """) results= cursor.fetchall() cursor.close() best_sellers = [Title.get(x[2]) for x in results if Title.get(x[2]).copies_in_status("STOCK")>0] new_titles=Title.select(""" book.title_id=title.id AND book.status ='STOCK' """ ,orderBy="-title.id",clauseTables=['book'],distinct=True) return dict(authorswidget=AuthorsWidget(),titlelistwidget=TitleListWidget(),searchform=searchform,new_titles=new_titles[:10],best_sellers=best_sellers)
def test_lookup_by_isbn10_is_invalid(self): # translation table of checkdigits to wrong ones (digit plus 1) tr_table = dict( list( zip( ["x", "X"] + list(map(str, list(range(9, -1, -1)))), ["0", "0", "x"] + list(map(str, list(range(9, 0, -1)))), ) ) ) random_item = random.sample(list(Title.select("isbn RLIKE '^[0-9]{13}$'")), 1)[ 0 ] wrong_isbn = isbnlib.to_isbn10(random_item.isbn) wrong_isbn = wrong_isbn[0:9] + tr_table[wrong_isbn[9]] with self.assertRaises((isbnlib.NotValidISBNError, isbnlib._exceptions.NotValidISBNError)): result = inventory.lookup_by_isbn(wrong_isbn)
def test_lookup_by_orig_isbn_has_extra_hyphens(self): random_item = random.sample(list(Title.select("isbn RLIKE '^[0-9]{13}$'")), 1)[ 0 ] prepared_isbn = ( random_item.isbn[0:3] + "-" + random_item.isbn[3:8] + "-" + random_item.isbn[8:] ) result = inventory.lookup_by_isbn(prepared_isbn) self.assertEqual( random_item.isbn, result["isbn"], "inventory.lookup_by_isbn returned wrong isbn for random isbn in database", )
def test_select_item_for_isbn_search_unit(self): random_item = random.sample(list(Title.select()), 1)[0] code, error = tidylib.tidy_document( self._my_class.select_item_for_isbn_search( isbn=random_item.isbn, title=random_item.booktitle, author=random_item.authors_as_string(), ), options={ "show-errors": 1, "show-warnings": 0 }, ) self.assertEqual( error, "", "InventoryServer.select_item_for_isbn_search did not return proper response", )
def do_edition_wizard(self, frame_object,event,label): wizard = EditionWizard (None, -1, label) wizard.label=label wizard.parent=frame_object if Kind.select(Kind.q.kindName==label).count()<1: print 'you probably need to add a kind to correspond to the label %s' % label wizard.editions=[] wizard.title_list=Title.select(""" title.kind_id= kind.id AND kind.kind_name='%s' """ % (label),orderBy=Title.q.booktitle,clauseTables=['kind'],distinct=True) wizard.title_page = ChooseItemPage ( wizard ) wizard.GetPageAreaSizer().Add(wizard.title_page) wizard.edition_page=ChooseEditionPage(wizard) wizard.action_page=ChooseActionPage(wizard) EVT_WIZARD_FINISHED(wizard,wizard.GetId(), wizard.finish) wizard.RunWizard ( wizard.title_page ) wizard.Destroy()
def title(self,id): ratingsform = widgets.RemoteForm(fields=RatingFields(), submit_text="Rate it!") # Title.sqlmeta.addJoin(MultipleJoin('Rating',joinMethodName='ratings')) thetitle=Title.get(id) thetitle.ratings=[] same_author=[] for author in thetitle.author: titles=Title.select(""" title.id!=%s AND book.title_id=title.id AND book.status ='STOCK' AND author.id=author_title.author_id AND author_title.title_id=title.id AND author.author_name='%s' """ % (escape_string("%s" % thetitle.id),escape_string(author.author_name)),orderBy="booktitle",clauseTables=['book','author','author_title'],distinct=True) for title in titles: same_author.append(title) if thetitle.copies_in_status("STOCK")==0: raise HTTPError(404) return dict(thetitle=thetitle,authorswidget=AuthorsWidget(),ratingsform=ratingsform,titlelistwidget=TitleListWidget(),same_author=same_author)
def search(self, author="", title=""): searchform = widgets.TableForm(fields=SearchFields(), submit_text="Search!") if author == "" and title == "": the_titles = False else: the_titles = Title.select( """ book.title_id=title.id AND book.status ='STOCK' AND author.id=author_title.author_id AND author_title.title_id=title.id AND author.author_name LIKE '%%%s%%' AND title.booktitle LIKE '%%%s%%' """ % (escape_string(author), escape_string(title)), orderBy="booktitle", clauseTables=['book', 'author', 'author_title'], distinct=True) return dict(the_titles=the_titles, authorswidget=AuthorsWidget(), titlelistwidget=TitleListWidget(), searchform=searchform, values=dict(author=author, title=title))
def reinventory(self,isbn="",author="",title=""): searchform = widgets.TableForm(fields=PrivateSearchFields(), submit_text="Search!") if author == "" and title=="" and isbn=="": the_titles=False else: the_titles=Title.select(""" title.isbn LIKE '%%%s%%' AND author.id=author_title.author_id AND author_title.title_id=title.id AND author.author_name LIKE '%%%s%%' AND title.booktitle LIKE '%%%s%%' """ % (escape_string(isbn),escape_string(author),escape_string(title)),orderBy="booktitle",clauseTables=['author','author_title'],distinct=True) title_count=0 try: title_count=the_titles.count() except: pass if title_count>0: if the_titles.count()==1: return self.title(the_titles[0].id,searchvalues=dict(author=author,title=title,isbn=isbn)) return dict(the_titles=the_titles,authorswidget=PrivateAuthorsWidget(),titlelistwidget=PrivateTitleListWidget(),searchform=searchform,values=dict(author=author,title=title,isbn=isbn))
def do_edition_wizard(self, frame_object, event, label): wizard = EditionWizard(None, -1, label) wizard.label = label wizard.parent = frame_object if Kind.select(Kind.q.kindName == label).count() < 1: print 'you probably need to add a kind to correspond to the label %s' % label wizard.editions = [] wizard.title_list = Title.select(""" title.kind_id= kind.id AND kind.kind_name='%s' """ % (label), orderBy=Title.q.booktitle, clauseTables=['kind'], distinct=True) wizard.title_page = ChooseItemPage(wizard) wizard.GetPageAreaSizer().Add(wizard.title_page) wizard.edition_page = ChooseEditionPage(wizard) wizard.action_page = ChooseActionPage(wizard) EVT_WIZARD_FINISHED(wizard, wizard.GetId(), wizard.finish) wizard.RunWizard(wizard.title_page) wizard.Destroy()
def database_gen(authorOrTitle=""): titles = [] # start out with the join clauses in the where clause list where_clause_list = [] clause_tables = ["book", "author", "author_title"] join_list = [ LEFTJOINOn("title", "book", "book.title_id=title.id"), LEFTJOINOn(None, "author_title", "title.id=author_title.title_id"), LEFTJOINOn(None, "author", "author.id=author_title.author_id"), ] # add filter clauses if they are called for where_clause_list.append( "(author.author_name RLIKE '%s' OR title.booktitle RLIKE '%s')" % (authorOrTitle.strip(), authorOrTitle.strip()) ) # AND all where clauses together where_clause = AND(where_clause_list) titles = [] # do search. titles = Title.select( where_clause, join=join_list, clauseTables=clause_tables, distinct=True ) for t1 in titles: yield { "title": t1.booktitle, "authors": t1.author, "authors_as_string": t1.authors_as_string(), "categories_as_string": t1.categories_as_string(), "list_price": t1.highest_price_book().ourprice, "publisher": t1.publisher, "isbn": t1.isbn, "format": t1.type, "kind": t1.kind.kindName, "known_title": t1, }
def do_easyselect_wizard(frame_object, event, label): wizard = EasySelectWizard(None, -1, label) wizard.label = label wizard.parent = frame_object if Kind.select(Kind.q.kindName == label).count() < 1: print 'you probably need to add a kind to correspond to the label %s' % label title_list = Title.select(""" title.kind_id= kind.id AND kind.kind_name='%s' """ % (label), orderBy=Title.q.booktitle, clauseTables=['kind'], distinct=True) #wizard.title_list=[t for t in title_list if len([b for b in t.books if b.status=="STOCK"])>0] wizard.title_list = title_list wizard.title_page = ChooseItemPage(wizard) wizard.GetPageAreaSizer().Add(wizard.title_page) wizard.action_page = ChooseActionPage(wizard) EVT_WIZARD_FINISHED(wizard, wizard.GetId(), wizard.finish) wizard.RunWizard(wizard.title_page) wizard.Destroy()
from objects.title import Title from objects.author import Author from objects.category import Category setLicenseKey(amazon_license_key) setSecretAccessKey(amazon_secret_key) setAssociateTag(amazon_associate_tag) #make sure we're in production database dbname='infoshopkeeper' try: theapp=webtest.TestApp(application) except: pass titles=Title.select(RLIKE(Title.q.isbn, '^[0-9]{10}[0-9xX]$|^[0-9]{13}$')) titles_that_display, titles_that_dont_display, titles_that_dont_even_fetch=(0,[],[]) for title in titles: try: response=theapp.get('/search', {'id':title.id}) lx_htm=lxml.html.fromstring(response.text) if lx_htm.cssselect('tbody tr td').__len__() > 1: titles_that_display += 1 else: titles_that_dont_display.append(title.id) except Exception as e: print e titles_that_dont_even_fetch.append(title.id) print "Number of titles that don't fetch: ", titles_that_dont_even_fetch.__len__()
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 amazon.setLicense(amazon_license_key) pythonItems = amazon.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.BrowseList.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 }
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 test_retrieve_image_url_have_image(self): pass random_item = random.sample(list(Title.select()), 1)[0]
def test_lookup_by_isbn_is_wsr(self): random_item=random.sample(list(Title.select('isbn RLIKE \'wsr [0-9]{3,5}\'')), 1)[0] result=inventory.lookup_by_isbn(random_item.isbn) self.assertEqual(random_item.isbn, result['isbn'], 'inventory.lookup_by_isbn returned wrong isbn for random isbn (wsr) in database')
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 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}
def test_lookup_by_isbn10_have_it(self): random_item= random.sample(list(Title.select('isbn RLIKE \'[0-9]{9}[0-9xX]\'')), 1)[0] result= inventory.lookup_by_isbn( random_item.isbn ) self.assertEqual(random_item.isbn, result['isbn'], 'inventory.lookup_by_isbn returned wrong isbn for random isbn10 in database')
def test_lookup_by_isbn10_is_invalid(self): #translation table of checkdigits to wrong ones (digit plus 1) tr_table=dict(zip( ['x', 'X'] + map(str, range(9, -1, -1)), ['0', '0', 'x'] + map(str, range(9, 0, -1)) )) random_item=random.sample(list(Title.select('isbn RLIKE \'[0-9]{9}[0-9xX]\'')), 1)[0] wrong_isbn=random_item.isbn[0:9] + tr_table[random_item.isbn[9]] self.assertRaises(InvalidParameterValue, inventory.lookup_by_isbn, wrong_isbn)
def test_search_isbn_that_we_dont_have_functional(self): random_item=random.sample(list(Title.select()), 1)[0] response=self._my_app.get('/admin/search_isbn', {'isbn':random_item.isbn}).json[0] self.assertEqual(response['isbn'], random_item.isbn, "/admin/search_isbn doesn't return proper item for isbn10")
def test_titleedit_functional(self): random_item=random.sample(list(Title.select()), 1)[0] response=self._my_app.get('/titleedit', {'id':random_item.id}) code, error=tidylib.tidy_document(response.body, options={'show-errors':1, 'show-warnings':0}) self.assertFalse(error, '/titleedit did not return valid html page')
def test_search_isbn_that_we_dont_have_unit(self): random_item=random.sample(list(Title.select()), 1)[0] result=self._my_class.search_isbn(**{'isbn':random_item.isbn})[0] self.assertEqual(result['isbn'], random_item.isbn, "method search_isbn doesn't return proper item for isbn10")
def search(self,title="",sortby="booktitle",distributor="",owner="",publisher="",author="",category="",out_of_stock='no',stock_less_than="",stock_more_than="",sold_more_than="",sold_less_than="",tag="",kind=""): cherrypy.session['lastsearch']=False self.common() cherrypy.session['lastsearch']=cherrypy.request.browserUrl self._searchtemplate.empty=True self._searchtemplate.title=title self._searchtemplate.author=author self._searchtemplate.category=category self._searchtemplate.distributor=distributor self._searchtemplate.owner=owner self._searchtemplate.publisher=publisher self._searchtemplate.out_of_stock=out_of_stock self._searchtemplate.stock_less_than=stock_less_than self._searchtemplate.stock_more_than=stock_more_than self._searchtemplate.sold_more_than=sold_more_than self._searchtemplate.sold_less_than=sold_less_than self._searchtemplate.tag=tag self._searchtemplate.kinds=list(Kind.select()) self._searchtemplate.kind=kind the_kind=kind if type(the_kind) == type([1,2,3]): the_kind=the_kind[0] titles=[] fields=[title,author,category,distributor,owner,publisher,stock_less_than,stock_more_than,sold_more_than,sold_less_than,tag,kind] fields_used = [f for f in fields if f != ""] if len(fields_used)>0 or out_of_stock=="yes": self._searchtemplate.empty=False if len(author)>0: titles=Title.select(""" title.kind_id = '%s' AND title.booktitle LIKE '%%%s%%' AND title.publisher LIKE '%%%s%%' AND title.tag LIKE '%%%s%%' AND book.title_id=title.id AND book.distributor LIKE '%%%s%%' AND book.owner LIKE '%%%s%%' AND author_title.title_id=title.id AND author_title.author_id=author.id AND author.author_name LIKE '%%%s%%' """ % (escape_string(the_kind),escape_string(title),escape_string(publisher),escape_string(tag),escape_string(distributor),escape_string(owner),escape_string(author)),orderBy=sortby,clauseTables=['book','author','author_title'],distinct=True) else: if len(category)>0: titles=Title.select(""" title.kind_id = '%s' AND title.booktitle LIKE '%%%s%%' AND title.publisher LIKE '%%%s%%' AND title.tag LIKE '%%%s%%' AND book.title_id=title.id AND book.distributor LIKE '%%%s%%' AND book.owner LIKE '%%%s%%' AND category.title_id=title.id AND category.category_name LIKE '%%%s%%' """ % (escape_string(the_kind),escape_string(title),escape_string(publisher),escape_string(tag),escape_string(distributor),escape_string(owner),escape_string(category)),orderBy=sortby,clauseTables=['book','category'],distinct=True) else: # do a less complicated query titles=Title.select(""" title.kind_id = '%s' AND title.booktitle LIKE '%%%s%%' AND title.publisher LIKE '%%%s%%' AND title.tag LIKE '%%%s%%' AND book.title_id=title.id AND book.distributor LIKE '%%%s%%' AND book.owner LIKE '%%%s%%' """ % (escape_string(the_kind),escape_string(title),escape_string(publisher),escape_string(tag),escape_string(distributor),escape_string(owner)),orderBy=sortby,clauseTables=['book'],distinct=True) if out_of_stock == 'yes': titles = [t for t in titles if t.copies_in_status("STOCK") == 0] if stock_less_than != "": titles = [t for t in titles if t.copies_in_status("STOCK") <= int(stock_less_than)] if stock_more_than != "": titles = [t for t in titles if t.copies_in_status("STOCK") >= int(stock_more_than)] if sold_more_than != "": titles = [t for t in titles if t.copies_in_status("SOLD") >= int(sold_more_than)] if sold_less_than != "": titles = [t for t in titles if t.copies_in_status("SOLD") <= int(sold_less_than)] self._searchtemplate.titles=titles return self._searchtemplate.respond()
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, }
def search(self, title="", sortby="booktitle", distributor="", owner="", publisher="", author="", category="", out_of_stock='no', stock_less_than="", stock_more_than="", sold_more_than="", sold_less_than="", tag="", kind=""): cherrypy.session['lastsearch'] = False self.common() cherrypy.session['lastsearch'] = cherrypy.request.browserUrl self._searchtemplate.empty = True self._searchtemplate.title = title self._searchtemplate.author = author self._searchtemplate.category = category self._searchtemplate.distributor = distributor self._searchtemplate.owner = owner self._searchtemplate.publisher = publisher self._searchtemplate.out_of_stock = out_of_stock self._searchtemplate.stock_less_than = stock_less_than self._searchtemplate.stock_more_than = stock_more_than self._searchtemplate.sold_more_than = sold_more_than self._searchtemplate.sold_less_than = sold_less_than self._searchtemplate.tag = tag self._searchtemplate.kinds = list(Kind.select()) self._searchtemplate.kind = kind the_kind = kind if type(the_kind) == type([1, 2, 3]): the_kind = the_kind[0] titles = [] fields = [ title, author, category, distributor, owner, publisher, stock_less_than, stock_more_than, sold_more_than, sold_less_than, tag, kind ] fields_used = [f for f in fields if f != ""] if len(fields_used) > 0 or out_of_stock == "yes": self._searchtemplate.empty = False if len(author) > 0: titles = Title.select( """ title.kind_id = '%s' AND title.booktitle LIKE '%%%s%%' AND title.publisher LIKE '%%%s%%' AND title.tag LIKE '%%%s%%' AND book.title_id=title.id AND book.distributor LIKE '%%%s%%' AND book.owner LIKE '%%%s%%' AND author_title.title_id=title.id AND author_title.author_id=author.id AND author.author_name LIKE '%%%s%%' """ % (escape_string(the_kind), escape_string(title), escape_string(publisher), escape_string(tag), escape_string(distributor), escape_string(owner), escape_string(author)), orderBy=sortby, clauseTables=['book', 'author', 'author_title'], distinct=True) else: if len(category) > 0: titles = Title.select(""" title.kind_id = '%s' AND title.booktitle LIKE '%%%s%%' AND title.publisher LIKE '%%%s%%' AND title.tag LIKE '%%%s%%' AND book.title_id=title.id AND book.distributor LIKE '%%%s%%' AND book.owner LIKE '%%%s%%' AND category.title_id=title.id AND category.category_name LIKE '%%%s%%' """ % (escape_string(the_kind), escape_string(title), escape_string(publisher), escape_string(tag), escape_string(distributor), escape_string(owner), escape_string(category)), orderBy=sortby, clauseTables=['book', 'category'], distinct=True) else: # do a less complicated query titles = Title.select(""" title.kind_id = '%s' AND title.booktitle LIKE '%%%s%%' AND title.publisher LIKE '%%%s%%' AND title.tag LIKE '%%%s%%' AND book.title_id=title.id AND book.distributor LIKE '%%%s%%' AND book.owner LIKE '%%%s%%' """ % (escape_string(the_kind), escape_string(title), escape_string(publisher), escape_string(tag), escape_string(distributor), escape_string(owner)), orderBy=sortby, clauseTables=['book'], distinct=True) if out_of_stock == 'yes': titles = [ t for t in titles if t.copies_in_status("STOCK") == 0 ] if stock_less_than != "": titles = [ t for t in titles if t.copies_in_status("STOCK") <= int(stock_less_than) ] if stock_more_than != "": titles = [ t for t in titles if t.copies_in_status("STOCK") >= int(stock_more_than) ] if sold_more_than != "": titles = [ t for t in titles if t.copies_in_status("SOLD") >= int(sold_more_than) ] if sold_less_than != "": titles = [ t for t in titles if t.copies_in_status("SOLD") <= int(sold_less_than) ] self._searchtemplate.titles = titles return self._searchtemplate.respond()
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')
def lookup_by_isbn(number, forceUpdate=False): isbn, price = _process_isbn(number) print("Looking up isbn", isbn, "with price", price) # if length of isbn>0 and isn't "n/a" or "none" 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 known_title = False the_titles = list(titles) if (len(the_titles) > 0) and (not forceUpdate): ##print "in titles" 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 = ", ".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 = ", ".join(categories) if price == 0: if len(the_titles[0].books) > 0: ListPrice = max([x.listprice for x in the_titles[0].books]) else: ListPrice = 0 else: ListPrice = price Manufacturer = the_titles[0].publisher.format() Format = the_titles[0].type.format() Kind = the_titles[0].kind.kindName orig_isbn = the_titles[0].origIsbn.format() # if the_titles[0].images: # large_url = the_titles[0].images.largeUrl # med_url = the_titles[0].images.medUrl # small_url = the_titles[0].images.smallUrl # else: # large_url = med_url = small_url = '' large_url = med_url = small_url = "" SpecialOrders = [ tso.id for tso in Title.selectBy( isbn=isbn ).throughTo.specialorder_pivots.filter( TitleSpecialOrder.q.orderStatus == "ON ORDER" ) ] return { "title": ProductName, "authors": authors, "authors_as_string": authors_as_string, "categories_as_string": categories_as_string, "list_price": ListPrice, "publisher": Manufacturer, "isbn": isbn, "orig_isbn": orig_isbn, "large_url": large_url, "med_url": med_url, "small_url": small_url, "format": Format, "kind": Kind, "known_title": known_title, "special_order_pivots": SpecialOrders, } else: # we don't have it yet # if we're using amazon ecs if use_amazon_ecs: 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] amazonBooks = [] idType = "" if len(isbn) == 12: idType = "UPC" elif len(isbn) == 13: # if we are using an internal isbn if isbn.startswith(internal_isbn_prefix): return [] # otherwise search on amazon. elif isbn.startswith("978") or isbn.startswith("979"): idType = "ISBN" else: idType = "EAN" try: print("searching amazon for ", isbn, idType, file=sys.stderr) amazonProds = AmzSear(isbn) print(amazonProds, file=sys.stderr) except (ecs.InvalidParameterValue, HTTPError): pass if amazonProds: print(amazonProds, file=sys.stderr) # inner comprehension tests each prodict for price whose type is in formats # if we find a price which its key is in formats, then we return the coorresponding product format_list = [ "Paperback", "Mass Market Paperback", "Hardcover", "Perfect Paperback", "Pamphlet", "Plastic Comb", "Spiral-bound", "Print on Demand (Paperback)", "DVD", "Calendar", "Board book", "Audio Cassette", "Cards", "Audio CD", "Diary", "DVD-ROM", "Library Binding", "music", "Vinyl", "Health and Beauty", "Hardback", ] prods = [ x for x in amazonProds.values() if [dum for dum in x["prices"].keys() if dum in format_list] ] for prod1 in prods: print(prod1, file=sys.stderr) price_dict = prod1["prices"] listprice = max(price_dict.values()) format = [k for k in format_list if k in price_dict] format = format[0] if not format: continue title = prod1["title"] image_url = prod1["image_url"] authors = [ x.replace("by ", "") for x in prod1["subtext"] if x.startswith("by ") ] auth_list = [ y.strip() for a in [x.split(", ") for x in authors[0].split(" and ")] for y in a ] # we assume any full name less than five characters is an abbreviation like 'Jr.' # so we add it back to the previous authorname abbrev_list = [i for i, x in enumerate(auth_list) if len(x) < 5] for i in abbrev_list: auth_list[i - 1 : i + 1] = [ ", ".join(auth_list[i - 1 : i + 1]) ] return { "title": title, "authors": auth_list, "authors_as_string": ",".join(auth_list), "categories_as_string": "", "list_price": listprice, "publisher": "", "isbn": isbn, "orig_isbn": isbn, "large_url": image_url, "med_url": image_url, "small_url": image_url, "format": format, "kind": "books", "known_title": known_title, "special_orders": [], } else: traceback.print_exc() print("using isbnlib from ecs", file=sys.stderr) isbnlibbooks = [] try: isbnlibbooks = isbnlib.meta(str(isbn)) except: pass if isbnlibbooks: return { "title": isbnlibbooks["Title"], "authors": isbnlibbooks["Authors"], "authors_as_string": ",".join(isbnlibbooks["Authors"]), "categories_as_string": None, "list_price": price, "publisher": isbnlibbooks["Publisher"], "isbn": isbn, "orig_isbn": isbn, "large_url": None, "med_url": None, "small_url": None, "format": None, "kind": "books", "known_title": known_title, "special_orders": [], } else: return {} else: # if we're scraping amazon print("scraping amazon", file=sys.stderr) headers = { "User-Agent": random.sample(user_agents, 1).pop() } amazon_url_template = "http://www.amazon.com/dp/%s/" if len(isbn) == 13: isbn10 = None if isbnlib.is_isbn13(isbn): isbn10 = isbnlib.to_isbn10(isbn) else: return {} if isbn10: with requests.Session() as session: try: print("getting amazon") page_response = session.get( amazon_url_template % isbn10, headers=headers, timeout=0.1 ) print("got response") page_content = BeautifulSoup(page_response.content, "lxml") print("got parsed content") try: booktitle = page_content.select("#productTitle").pop().text except Exception as e: traceback.print_exc() booktitle = '' popover_preload = [ a.text for a in page_content.select( ".author.notFaded .a-popover-preload a.a-link-normal" ) ] author_name = [ a.text for a in page_content.select( ".author.notFaded a.a-link-normal" ) if a.text not in popover_preload ] contributor_role = page_content.select(".contribution span") try: contributor_role = [ re.findall("\w+", cr.text).pop() for cr in contributor_role ] except Exception as e: traceback.print_exc() contributor_role = [] author_role = zip(author_name, contributor_role) try: listprice = ( page_content.select(".a-text-strike").pop().text ) except IndexError as e: print("using bookfinder4u") if "listprice" not in locals(): with requests.Session() as session: bookfinderurl = "http://www.bookfinder4u.com/IsbnSearch.aspx?isbn='%s'&mode=direct" url = bookfinderurl % isbn try: page_response2 = session.get( url, headers=headers, timeout=0.1 ) page_content2 = BeautifulSoup( page_response2.content, "lxml" ) except Exception as e: traceback.print_exc() listprice = 0.0 else: try: matches = re.search( "List\sprice:\s(\w{2,4})\s(\d+(.\d+)?)", page_content2.text, re.I, ) if matches: listprice = matches.groups()[1] else: listprice = 0.00 except Exception as e: traceback.print_exc() listprice = 0.00 try: book_edition = ( page_content.select("#bookEdition").pop().text ) except Exception as e: traceback.print_exc() book_edition = "" try: matches = re.findall( "(?<=imageGalleryData'\s:\s\[)\{.*?\}", page_content.contents[1].text, ) image_url_dict = eval(matches[0]) except Exception as e: traceback.print_exc() image_url_dict = {"mainUrl": "", "thumbUrl": ""} category_items = page_content.select(".zg_hrsr_ladder a") category_items = [a.text for a in category_items] product_details = page_content.select( "#productDetailsTable" ) # ul:first-of-type") try: product_details1 = product_details.pop().text.splitlines() quit_flag = 0 for pd in product_details1: if pd.endswith("pages"): format, numpages = pd.split(":") numpages = numpages.replace(" pages", "").strip() quit_flag += 1 continue if pd.startswith("Publisher: "): matches = re.match( "Publisher: ([^;^(]*)\s?([^(]*)?\W(.*)\W", pd ).groups() publisher = matches[0] publication_date = matches[2] quit_flag += 1 continue if quit_flag == 2: break else: publisher = '' format = '' except Exception as e: traceback.print_exc() publisher = '' format = '' if booktitle: return { "title": booktitle, "authors": author_name, "authors_as_string": ",".join(author_name), "categories_as_string": ",".join(category_items), "list_price": listprice, "publisher": publisher, "isbn": isbn, "orig_isbn": isbn, "large_url": image_url_dict["mainUrl"], "med_url": image_url_dict["mainUrl"], "small_url": image_url_dict["thumbUrl"], "format": format, "kind": "books", "known_title": known_title, "special_orders": [], } except Exception as e: traceback.print_exc() print("using isbnlib from scraper", file=sys.stderr) isbnlibbooks = [] try: isbnlibbooks = isbnlib.meta(str(isbn)) except: pass if isbnlibbooks: return { "title": isbnlibbooks["Title"], "authors": isbnlibbooks["Authors"], "authors_as_string": ",".join( isbnlibbooks["Authors"] ), "categories_as_string": None, "list_price": price, "publisher": isbnlibbooks["Publisher"], "isbn": isbn, "orig_isbn": isbn, "large_url": None, "med_url": None, "small_url": None, "format": None, "kind": "books", "known_title": known_title, "special_orders": [], } else: return {} else: if title: return { "title": title, "authors": author_name, "authors_as_string": ",".join(author_name), "categories_as_string": ",".join(category_items), "list_price": listprice, "publisher": publisher, "isbn": isbn, "orig_isbn": isbn, "large_url": image_url_dict["mainUrl"], "med_url": image_url_dict["mainUrl"], "small_url": image_url_dict["thumbUrl"], "format": format, "kind": "books", "known_title": known_title, "special_orders": [], } 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')
from sqlobject.sqlbuilder import RLIKE from objects.title import Title import lxml from lxml import html, cssselect import webtest from wsgiapp_local import application try: theapp=webtest.TestApp(application) except: pass titles=Title.select(RLIKE(Title.q.isbn, '^[0-9]{9}[0-9xX]$')) titles_that_display, titles_that_dont_display, titles_that_dont_even_fetch=(0,[],[]) for title in titles: try: response=theapp.get('/search', {'id':title.id}) lx_htm=lxml.html.fromstring(response.text) if lx_htm.cssselect('tbody tr td').__len__() > 1: titles_that_display += 1 else: titles_that_dont_display.append(title.id) except Exception as e: print e titles_that_dont_even_fetch.append(title.id) print "Number of titles that don't fetch: ", titles_that_dont_even_fetch.__len__()
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 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 []
from objects.title import Title from objects.author import Author from objects.category import Category setLicenseKey(amazon_license_key) setSecretAccessKey(amazon_secret_key) setAssociateTag(amazon_associate_tag) # make sure we're in production database dbname = "infoshopkeeper" try: theapp = webtest.TestApp(application) except: pass titles = Title.select(RLIKE(Title.q.isbn, "^[0-9]{10}[0-9xX]$|^[0-9]{13}$")) titles_that_display, titles_that_dont_display, titles_that_dont_even_fetch = ( 0, [], []) for title in titles: try: response = theapp.get("/search", {"id": title.id}) lx_htm = lxml.html.fromstring(response.text) if lx_htm.cssselect("tbody tr td").__len__() > 1: titles_that_display += 1 else: titles_that_dont_display.append(title.id) except Exception as e: print(e) titles_that_dont_even_fetch.append(title.id)