def insert_data(self, data): """ Will handle inserting data into the database """ try: # Check if book is in database, if so update else create try: book = db_session.query(Book).filter(Book.book_id == data.get('book_id')).one() except NoResultFound: book = Book() book.title = data.get('title') book.subtitle = data.get('subtitle') book.author = data.get('author') book.year = data.get('year') book.pages = data.get('pages') book.language = data.get('language') book.publisher = data.get('publisher') book.isbn = data.get('isbn') book.format = data.get('format') book.description = data.get('description') book.file_source = data.get('file_source') book.file_cover_source = data.get('file_cover_source') book.file_location = data.get('file_location') book.file_cover_location = data.get('file_cover_location') book.book_id = data.get('book_id') book.time_collected = data.get('time_collected') db_session.add(book) db_session.commit() # self.track_stat('rows_added_to_db', rows_affected) except Exception: db_session.rollback() logger.exception("Error adding to db {data}".format(data=data))
def parse_book_info(self, isbn): r = requests.get('http://api.douban.com/v2/book/isbn/%s' % isbn) jsonObj = json.loads(r.text) book = Book() if jsonObj['title']: book.title = jsonObj['title'] authors = jsonObj['author'] book.author = ",".join(authors) book.price = jsonObj['price'] book.summary = jsonObj['summary'] book.catalog = jsonObj['catalog'] book.publisher = jsonObj['publisher'] book.isbn13 = jsonObj['isbn13'] book.douban_id = jsonObj['id'] book.image = jsonObj['image'] book.douban_url = jsonObj['alt'] book.pages = jsonObj['pages'] book.pubdate = jsonObj['pubdate'] book.rating = jsonObj['rating']['average'] # 处理标签 book.tags = jsonObj['tags'] book.borrow_id = 0 book.borrow_name = '' book.borrow_counts = 0 book.status = 0 # 初始状态为空闲中 return book
def get(self): db = getUtility(IRelationalDatabase) cr = db.cursor() barcode = self.book.barcode if barcode: cr.execute( """SELECT id, barcode, author, title FROM books WHERE barcode = ?""", (barcode, )) else: cr.execute("""SELECT id, barcode, author, title FROM books""") rst = cr.fetchall() cr.close() books = [] for record in rst: id = record['id'] barcode = record['barcode'] author = record['author'] title = record['title'] book = Book() book.id = id book.barcode = barcode book.author = author book.title = title books.append(book) return books
def handleData(self, response): s = response.text books_dict = dict() books = [] queue_out = Queue() for field in book_fields: if self.re_rule.get(field): books_dict[field] = getRe(s, self.re_rule[field]) elif self.xpath_rule.get(field): books_dict[field] = self.getXpath(s, self.xpath_rule[field]) self.req().createImageThread(self.web, books_dict['image'], queue_out) print('搜索条目数:', len(books_dict['url'])) for i in range(len(books_dict['url'])): book = Book() book.url = books_dict['url'][i].replace(' ', '').replace('\n', '') book.image = books_dict['image'][i].replace(' ', '').replace('\n', '') book.title = books_dict['title'][i].replace(' ', '').replace('\n', '') book.author = books_dict['author'][i].replace(' ', '').replace( '\n', '') book.genre = books_dict['genre'][i].replace(' ', '').replace('\n', '') book.serial = books_dict['serial'][i].replace(' ', '').replace( '\n', '') book.word_n = books_dict['word_n'][i].replace(' ', '').replace( '字', '').replace('\n', '') book.info = books_dict['info'][i] books.append(book) self.booksImage(books, queue_out) # Ui_MainWindow.tabWidget.get_ResultWidget('空').label.setText('啊哈哈') return books
def get(self): db = getUtility(IRelationalDatabase) cr = db.cursor() barcode = self.book.barcode if barcode: cr.execute("""SELECT id, barcode, author, title FROM books WHERE barcode = ?""", (barcode,)) else: cr.execute("""SELECT id, barcode, author, title FROM books""") rst = cr.fetchall() cr.close() books = [] for record in rst: id = record['id'] barcode = record['barcode'] author = record['author'] title = record['title'] book = Book() book.id = id book.barcode = barcode book.author = author book.title = title books.append(book) return books
def update_books(books = get_books()): opener = urllib2.build_opener() opener.addheaders = [('User-agent', 'Mozilla/5.0')] for book in books: try: b = Book.objects.filter(title=book['title']).count() print '>>>', b if not b: b = Book() b.title = book['title'] author = book['author'] last_name = author.split(' ')[-1] first_name = ' '.join(author.split(' ')[:-1]) try: author = Author.objects.get(first_name=first_name, last_name=last_name) except: author = Author(first_name=first_name, last_name=last_name) author.save() b.author = author b.external_url = 'http://en.wikipedia.org'+book['link'] try: content = opener.open('http://en.wikipedia.org'+book['link']).read() s = Soup(content) info = s.find('table', {'class':'infobox'}) img = info.find('img') if img: b.image = 'http:'+img.get('src') except: print "IMAGE FAILED FOR", book b.save() except Exception, e: print e print "WOAH TOTAL FAILURE", book
def post(self): entity_key_urlsafe = self.request.get("entity_key") book = None book_seller_key = self.person.key # Make sure POST request is given these names book_image_url = self.request.get("image-url") book_price = float(self.request.get("price")) book_isbn = self.request.get("isbn") book_author = self.request.get("author") book_title = self.request.get("title") book_dept = self.request.get("dept-abbrev") book_condition_id = int(self.request.get("condition")) if entity_key_urlsafe: book_key = ndb.Key(urlsafe=entity_key_urlsafe) book = book_key.get() # keep same seller key # don't need cart_key book.price = book_price if book_isbn: book.isbn = book_isbn if book_author: book.author = book_author if book_title: book.title = book_title if book_dept: book.dept = book_dept.lower() if book_condition_id: book.condition_id = book_condition_id book.image_url = book_image_url else: book = Book(parent=ROOT_BOOK_KEY, seller_key = book_seller_key, price=book_price, image_url=book_image_url) if book_isbn: book.isbn = book_isbn if book_author: book.author = book_author if book_title: book.title = book_title if book_dept: book.dept = book_dept.lower() if book_condition_id: book.condition_id = book_condition_id # TODO: Replace above with this when all fields are given # book = Book(parent=ROOT_BOOK_KEY, seller_key = book_seller_key, price=book_price, # image_url=book_image_url, # isbn = book_isbn, author = book_author, title = book_title, # dept = book_dept, comments = str(book_comments).strip()) logging.info("Adding Book: " + str(book)) book.put() self.redirect(self.request.referer)
def on_add_clicked(self, *args): barcode = self.ui.barcode.get_text() author = self.ui.author.get_text() title = self.ui.title.get_text() book = Book() book.barcode = barcode book.author = author book.title = title self.add(book) self.ui.list_store.append((book, barcode, author, title))
def order_add_book_custom(request): """Add book to the current order with a custom title & author. Used for the AJAX book adds of books with custom titles and/or authors.""" # If this is a non-unique book, fill in what attributes we can and continue if request.POST.get('Title', False): book = Book() book.title = request.POST.get('Title', '') book.author = request.POST.get('Author', '') order_add_book(request, book) else: # The title is empty, which is the one field we require. We fail # silently for now, but could do something here. print 'Tried to add a custom book with no title to the current order, failing silently' return order_render_as_response(request)
def newBook(bookshelf_id): if request.method == "GET": if "username" in login_session: hostBookshelf = session.query(Bookshelf).get(bookshelf_id) if hostBookshelf.user_id != login_session["user_id"]: flash("Sorry! Only creators of a particular bookshelf can add \ edit or delete books.") return redirect( url_for( "bookshelfBooks", bookshelf_id=bookshelf_id)) else: return render_template( "newBook.html", bookshelf_id=hostBookshelf.id) else: return redirect(url_for("loginPage")) elif request.method == "POST": title = request.form.get("title") image = request.form.get("imageURL") author = request.form.get("author") category = request.form.get("category") description = request.form.get("description") user_id = login_session["user_id"] if title: newBook = Book( title=title, bookshelf_id=bookshelf_id, user_id=user_id) if image: newBook.image_url = image if author: newBook.author = author if category: newBook.category = category if description: newBook.description = description session.add(newBook) flash("New book {} successfully added.".format(newBook.title)) session.commit() else: flash("Please provide the title of the book") return redirect(url_for("newBook", bookshelf_id=bookshelf_id)) return redirect(url_for("bookshelfBooks", bookshelf_id=bookshelf_id))
def initialize_lists(self): cm = Gtk.ListStore(object, str, str, str) self.ui.catalog = cm self.ui.catalog_view.set_model(cm) for _ in range(3): b = Book() b.title = "Brotherhood of the ring" b.author = "J.R.R.Tolkien" b.barcode = "123-1232" self.append_book(b) mm = Gtk.ListStore(object, int, str) self.ui.members = mm self.ui.member_view.set_model(mm) for _ in range(3): m = Member() m.name = "Jim Carry" m.number = 123 self.append_member(m)
def initialize_lists(self): cm=Gtk.ListStore(object, str, str, str) self.ui.catalog=cm self.ui.catalog_view.set_model(cm) for _ in range(3): b=Book() b.title="Brotherhood of the ring" b.author="J.R.R.Tolkien" b.barcode="123-1232" self.append_book(b) mm=Gtk.ListStore(object, int, str) self.ui.members=mm self.ui.member_view.set_model(mm) for _ in range(3): m=Member() m.name="Jim Carry" m.number=123 self.append_member(m)
def add(request): if request.method == "POST": form = BookAddForm(request.POST) if form.is_valid(): book = Book() book.title = form.cleaned_data['title'] book.author = form.cleaned_data['author'] book.press = form.cleaned_data['press'] book.year = form.cleaned_data['year'] book.save() messages.success(request, "已保存") form = BookAddForm() else: messages.error(request, "保存失败,请检查所填写的内容!") else: form = BookAddForm() context = RequestContext(request, { "form":form, }) return render_to_response("add.html", context)
def post(self): user = users.get_current_user() if user: # gather ip the pieces of data bookshelf_name = user.nickname() book = Book(parent=bookshelf_key(bookshelf_name)) book.username = user.nickname() book.title = self.request.get('title') book.author = self.request.get('author') if self.request.get('in_series') == constants.CHECKED: book.in_series = True else: book.in_series = False book.series_name = self.request.get('series_name') try: book.book_in_series = int(self.request.get('book_in_series')) except: book.book_in_series = None book.isbn = self.request.get('isbn') try: book.pub_year = int(self.request.get('pub_year')) except: book.pub_year = None if self.request.get('was_it_read') == constants.CHECKED: book.was_read = True else: book.was_read = False try: cdt = datetime.strptime(self.request.get('date_read'), '%m/%d/%Y') cd = date(cdt.year, cdt.month, cdt.day) book.completed_date = cd except Exception, e: logging.info("exception getting read date : " + str(e)) book.completed_date = None book.put() self.redirect('/books')
def fetchCreateByISBN(isbn, dataSource, needInvoke): book = Book.byISBN(isbn) if needInvoke: dataSource = dataSource(isbn) if not book: book = Book() book.title = dataSource.title book.author = dataSource.author book.isbn = dataSource.isbn book.image = dataSource.image book.imageSmall = dataSource.imageSmall book.imageLarge = dataSource.imageLarge book.amazonUrl = dataSource.detailUrl book.numberOfPages = dataSource.numberOfPages book.put() book.tags = dataSource.genre return book
def get_books_ml(): opener = urllib2.build_opener() opener.addheaders = [('User-agent', 'Mozilla/5.0')] content = opener.open('http://www.modernlibrary.com/top-100/100-best-novels/').read() s = Soup(content) books = [] _list = [] for bl in s.findAll('div', {'class':'list-100'}): _list += bl.findAll('li') for b in _list: title = b.find('strong').getText().title() author = b.getText()[b.getText().find('by')+3:] books.append({ 'title':title, 'author':author, }) for book in books: try: b = Book.objects.filter(title=book['title']).count() print '>>>', b if not b: b = Book() b.title = book['title'] author = book['author'] last_name = author.split(' ')[-1] first_name = ' '.join(author.split(' ')[:-1]) try: author = Author.objects.get(first_name=first_name, last_name=last_name) except: author = Author(first_name=first_name, last_name=last_name) author.save() b.author = author b.save() except Exception, e: print e print "WOAH TOTAL FAILURE", book
def insert_data(self, data): """ Will handle inserting data into the database """ try: # Check if book is in database, if so update else create try: book = db_session.query(Book).filter( Book.book_id == data.get('book_id')).one() except NoResultFound: book = Book() book.title = data.get('title') book.subtitle = data.get('subtitle') book.author = data.get('author') book.year = data.get('year') book.pages = data.get('pages') book.language = data.get('language') book.publisher = data.get('publisher') book.isbn = data.get('isbn') book.format = data.get('format') book.description = data.get('description') book.file_source = data.get('file_source') book.file_cover_source = data.get('file_cover_source') book.file_location = data.get('file_location') book.file_cover_location = data.get('file_cover_location') book.book_id = data.get('book_id') book.time_collected = data.get('time_collected') db_session.add(book) db_session.commit() # self.track_stat('rows_added_to_db', rows_affected) except Exception: db_session.rollback() logger.exception("Error adding to db {data}".format(data=data))
def run_api(): """function to hit new york times api and return all books and authors""" for category in lists: try: r = urlopen("http://api.nytimes.com/svc/books/v3/lists/" + category + ".json?api-key=" + times_keys["books"]) rInfo = r.info() rRaw = r.read().decode(rInfo.get_content_charset('utf8')) result = json.loads(rRaw) except Exception as e: print(category + " has given us an error") break # Make a dictionary of the list categories book_lists = {} # Make a list of book objects list_of_book_objects = [] try: books = result["results"]["books"] except Exception as e: print("*" * 80) print(result) print(category) print("*" * 80) books = [] for book in books: # Empty list del list_of_book_objects[:] # Book title book_title = book["title"] # Best Seller List Name book_best_seller_list = result["results"]["list_name"] # Best Seller List date book_best_seller_date = result["results"]["bestsellers_date"] # ISBN book_isbn = book["primary_isbn13"] # Publisher book_publisher = book["publisher"] # Summary book_summary = book["description"] # Image book_book_image = book["book_image"] # Amazon link book_amazon_link = book["amazon_product_url"] # Author object a = book["author"] single_author = a.split(' and ')[0] if len(single_author.split(' ')) >= 2: lastName = single_author.split(' ')[-1] else: lastName = "None" firstName = single_author.split(' ')[0] with app.app_context(): author = Author.query.filter_by( first_name=firstName).filter_by(last_name=lastName).all() if len(author) > 0: book_author = author[0] else: book_author = Author( first_name=firstName, last_name=lastName, book_count=0, bio="not found", link=None) with app.app_context(): DB.session.add(book_author) DB.session.commit() with app.app_context(): q_book = Book.query.filter_by(isbn=book_isbn).all() if len(q_book) > 0: b = q_book[0] b.best_seller_list = b.best_seller_list + \ ", " + book_best_seller_list else: b = Book(isbn=book_isbn, title=book_title, summary=book_summary, best_seller_date=book_best_seller_date, best_seller_list=book_best_seller_list, book_image=book_book_image, amazon_link=book_amazon_link, publisher=book_publisher) b.author = book_author with app.app_context(): try: DB.session.add(b) DB.session.commit() except Exception: print("Handling weird book summary") DB.session.rollback() DB.session.delete(book_author) DB.session.commit() list_of_book_objects.append(b) # Add book objects in a specific best seller list to the general # dictionary of book lists book_lists[category] = list_of_book_objects
def sell(): ''' This function insert a book which the user sold to the database. Also, Checks if the user has enough creadits to pay to the system for this sale. ''' form = sellForm() if (request.method == 'POST') and form.validate_on_submit(): #DETERMINE IF USER HAVE ENOUGH CREDIT TO SELL THE BOOK #GET USER ID username = request.form['username'] user = User.query.filter_by(username = str(username)).first() userid = user.id #GET USER CREDITS user_credits = user.get_credit() #GET USER COMPLAINTS num_of_complaints = db.session.query(User_Complaints).filter_by(complained_id = userid).count() #CALCULATE CREDIT AFTER SALE temp_credit = user.credits - ((int(request.form['saleDuration'])) * 5) - ((int(request.form['saleDuration'])) * num_of_complaints) #UPDATE USER CREDITS if (temp_credit < 0): return render_template('no_credit.html') user.credits = temp_credit file = form.data.get('bookImage') tempBool = 0 b = Book() filename = ''.join(random.choice(string.ascii_letters+string.digits) for x in range(20)) if (str(request.files['bookImage']) == "<FileStorage: u'' ('application/octet-stream')>"): print "NO IMAGE" b.image_name = "noImage.jpg" else: #file = form.data.get('bookImage') file = request.files['bookImage'] file.filename = filename+".jpg" if file and allowed_file(file.filename): #UPLOAD FILE filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) b.image_name = filename #PUSH DATA TO DB b.title = request.form['title'] b.author = request.form['author'] b.isbn = request.form['isbn'] b.price = float(request.form['price']) b.saleDuration = int(request.form['saleDuration']) b.publisher = request.form['publisher'] b.numOfPages = int(request.form['numOfPages']) b.lang = request.form['lang'] b.condition = request.form['condition'] b.genre = request.form['genre'] b.bookType = request.form['bookType'] b.edition = int(request.form['edition']) b.information = request.form['information'] #tempBool=0 tempBool = False if (form.data.get('buyable') == True): #tempBool = 1; tempBool = True else: #tempBool = 0; tempBool = False b.buyable = tempBool if (tempBool == True): #if (tempBool == 1): b.buyout_price = float(request.form['buynowPrice']) # changing current_bid to price #b.current_bid=float(0) b.current_bid = b.price b.biddable= 1 b.starting_bid=float(request.form['price']) b.owner_id=int(userid) db.session.add(b) db.session.commit() return render_template('success.html') return render_template('sell.html', form=form)
from models import Book some_books = [('Nietzsche', 'Asi hablo Zaratustra'), ('Pwaqo', 'Conjunto Vacio')] for abook in some_books: book = Book() book.author, book.title = abook book.save()
def post(self): book = Book(); book.title = self.request.get('title') book.author = self.request.get('author') book.put() self.redirect("/list")