def __create_book_object(self, book_dict): """ Create a book object from the dictionary. """ title = book_dict["best_book"]["title"] author = book_dict["best_book"]["author"]["name"] rating = float(book_dict["average_rating"]) count = int(book_dict["ratings_count"]["#text"]) goodreads_id = int(book_dict["best_book"]["id"]["#text"]) return Book(title, author, rating, count, goodreads_id)
def parse_input(input_content: str) -> INPUT_TYPE: lines = input_content.split('\n') n_books, n_libraries, n_days = get_int_list(lines.pop(0)) book_scoring = get_int_list(lines.pop(0)) books = {i: Book(i, score) for i, score in enumerate(book_scoring)} libraries = [] for library_id in range(n_libraries): total_books, signup_time, scan_capacity = get_int_list(lines.pop(0)) libraries.append( Library(library_id, {books[book_id] for book_id in get_int_list(lines.pop(0))}, signup_time, scan_capacity)) return n_days, libraries
logger.info(f'Got {len(items)} books from Amazon wishlist.') for item in items: byline = item.find('span', id=re.compile('^item-byline')).text author = re.search(r'^by\s([\w\s.]+)[,(]', byline) d = { 'title': item.find('a', id=re.compile('^itemName_')).text, 'author': str.replace(author.group(1), '.', '').strip(), } dict_list.append(d) logger.info(f'Getting book metadata...') r = ReadingList() with tqdm(total=len(dict_list)) as pbar: for item in dict_list: pbar.update(1) book = Book(**item) found = book.find_goodreads_id() if not found: continue book.update_from_goodreads_api() r.add_book(book) logger.info(f'Calculating Ben scores...') r.calculate_ben_scores() r.sort() logger.info(f'Writing to CSV...') r.csv_export('output/readinglist.csv') logger.info(f'Done!')
def csv_export(self, outfile): fieldnames = vars(Book('', '')).keys() with open(outfile, 'w') as f: writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() writer.writerows([b.__dict__ for b in self.books])
def addToInventory(self, title="", status="STOCK", authors=[], publisher="", price="", isbn="", categories=[], distributor="", owner="", notes="", quantity=1, known_title=False, kind_name="", extra_prices={}): if not (known_title): #add a title the_kinds = list(Kind.select(Kind.q.kindName == kind_name)) kind_id = None if the_kinds: kind_id = the_kinds[0].id known_title = Title(isbn=isbn, booktitle=title.encode("ascii", "backslashreplace"), publisher=publisher.encode( "ascii", "backslashreplace"), tag=" ", kindID=kind_id) for rawAuthor in authors: author = rawAuthor.encode("ascii", "backslashreplace") theAuthors = Author.selectBy(author_name=author) theAuthorsList = list(theAuthors) if len(theAuthorsList) == 1: known_title.addAuthor(theAuthorsList[0]) elif len(theAuthorsList) == 0: a = Author(author_name=author) known_title.addAuthor(a) else: # We should SQLDataCoherenceLost here print "mmm... looks like you have multiple author of the sama name in your database..." for category in categories: Category(categoryName=category.encode("ascii", "backslashreplace"), title=known_title) for i in range(int(quantity)): print distributor.encode('ascii', "backslashreplace") wholesale = 0 try: wholesale = extra_prices['wholesale'] except: pass b = Book(title=known_title, status=status.encode("ascii", "backslashreplace"), distributor=distributor.encode('ascii', "backslashreplace"), listprice=price, owner=owner.encode("ascii", "backslashreplace"), notes=notes.encode("ascii", "backslashreplace"), consignmentStatus="", wholesale=wholesale) b.extracolumns() for mp in extra_prices.keys(): setattr(b, string.replace(mp, " ", ""), extra_prices[mp])
def addToInventory( title="", status="STOCK", authors=None, publisher="", listprice="", ourprice="", isbn="", orig_isbn="", categories=[], distributor="", location="", location_id="", large_url="", med_url="", small_url="", owner="", notes="", quantity=1, known_title=False, types="", kind_name="", kind=default_kind, extra_prices={}, tag="", labels_per_copy=1, printlabel=False, special_orders=0, ): print("GOT to addToInventory", file=sys.stderr) if not authors: authors = [] if known_title: print("known_title ", known_title, file=sys.stderr) if not known_title.booktitle: known_title.booktitle = title if not known_title.publisher: known_title.publisher = publisher if not known_title.type: known_title.type = types elif not (known_title): print("unknown title", file=sys.stderr) # add a title the_kinds = list(Kind.select(Kind.q.kindName == kind)) kind_id = None if the_kinds: kind_id = the_kinds[0].id print("kind id is", kind_id, file=sys.stderr) # print>>sys.stderr, title title = title publisher = publisher # print>>sys.stderr, title, publisher known_title = Title( isbn=isbn, origIsbn=orig_isbn, booktitle=title, publisher=publisher, tag=" ", type=types, kindID=kind_id, ) print(known_title, file=sys.stderr) im = Images( titleID=known_title.id, largeUrl=large_url, medUrl=med_url, smallUrl=small_url, ) print(im, file=sys.stderr) for rawAuthor in authors: author = rawAuthor theAuthors = Author.selectBy(authorName=author) theAuthorsList = list(theAuthors) if len(theAuthorsList) == 1: known_title.addAuthor(theAuthorsList[0]) elif len(theAuthorsList) == 0: a = Author(authorName=author) known_title.addAuthor(a) else: # We should SQLDataCoherenceLost here print( "mmm... looks like you have multiple author of the sama name in your database...", file=sys.stderr, ) for category in categories: Category(categoryName=category, title=known_title) # the_locations=list(Location.select(Location.q.locationName==location)) # location_id=1 # if the_locations: # location_id = the_locations[0].id if not ourprice: ourprice = listprice print("about to enter book loop", file=sys.stderr) print("location is", location, file=sys.stderr) print("location_id is", location_id, file=sys.stderr) for i in range(int(quantity)): print("book loop", file=sys.stderr) b = Book( title=known_title, status=status, distributor=distributor, listprice=listprice, ourprice=ourprice, location=int(location_id), owner=owner, notes=notes, consignmentStatus="", )
continue books = Book.selectBy(titleID=t1.id, ourprice=float(ourprice), status='STOCK') if list(books): ourprice = books[0].ourprice listprice = books[0].listprice book = books[0] break if not book: listprice = Book.selectBy(titleID=t1.id).max(Book.q.listprice) book = Book(title=t1, status='STOCK', location=1, owner='woodenshoe', listprice=float(listprice), ourprice=float(ourprice), consignmentStatus='', distributor='', notes='') barcodeLabel.print_barcode_label(isbn=isbn, booktitle=t1.booktitle, ourprice=book.ourprice, listprice=listprice, num_copies=1) else: continue else: titles = Title.select(sqlbuilder.RLIKE(Title.q.booktitle, isbn)) if list(titles): for n, t1 in enumerate(titles):