Ejemplo n.º 1
0
    def create_and_store_purchase_option(self, temp_book, isbn, price):
        errors = []
        # Check if book has correct ISBN
        # Check DB for duplicate isbn entry.
        # If so just make purchase option

        book = Book.get(isbn=isbn)
        if not book:
            book = Book()
            book.author = temp_book.author
            if temp_book.subtitle is None:
                book.title = temp_book.title
            else:
                book.title = temp_book.title+': ' + temp_book.subtitle
            book.isbn = isbn
            book.save()

        # add purchaseChoice to DB
        p = PurchaseChoice()
        p.book_id = book.id
        p.price = price
        p.type = 'Hardcover'
        p.isRental = False
        p.link = ''  # TODO change to valid link
        p.local_seller_id = current_user.id
        p.isLocalSeller = True
        p.save()
        return errors
Ejemplo n.º 2
0
def book_info():
    id = request.args.get('id')
    book_data = Book.get(id=id)
    if book_data is None:
        return ""
    json_output = json.dumps(book_data.__dict__, sort_keys=True, indent=4)
    return json_output
Ejemplo n.º 3
0
 def get_list_by_isbn(self, isbn):
     book_query = Book.get(isbn=isbn)
     # return blank list if this book is not yet in the database
     if book_query is None:
         option_list = []
     elif isinstance(book_query, list):
         # This indicates there are multiple books
         # with the same ISBN number, which shouldn't happen
         option_list = []
         for book in book_query:
             option_list = self.fill_list_by_bookid(option_list, book.id)
     else:
         option_list = self.fill_list_by_bookid([], book_query.id)
     return option_list
Ejemplo n.º 4
0
def store_book_info_for_user_and_isbn(user_id, isbn, isbns_to_retry):
    # add book object
    b = Book()  # create temp book object
    query_book = Book.get(
        isbn=isbn
    )  # check if Book object is already in DB
    if not query_book:
        temp_arr = get_books_for_book_title_using_google_books(
            'isbn:' + isbn
        )
        if len(temp_arr) == 0:
            AZ = AmazonScraper()
            amazon_dict = AZ.get_amazon_purchase_choices_for_keyword(isbn)
            if len(amazon_dict) == 0:
                isbn_info = {}
                isbn_info['isbn'] = isbn
                isbn_info['user_id'] = user_id
                isbn_info['retries'] = 0
                isbns_to_retry.append(isbn_info)
                return False
            temp_book = Book()
            temp_book.isbn = amazon_dict[0].get('isbn')
            temp_book.title = amazon_dict[0].get('title')
            temp_book.author = amazon_dict[0].get('author')
            temp_book.thumbnail_link = amazon_dict[0].get('thumbnail_link')
            temp_book.subtitle = ''
            temp_arr.append(temp_book)

        temp_var = temp_arr[0]
        b.isbn = temp_var.isbn
        b.author = temp_var.author
        if(temp_var.subtitle is None):
            b.title = temp_var.title  # check if subtitle is null
        else:
            b.title = (
                temp_var.title +
                ': ' + temp_var.subtitle  # check if subtitle is null
            )
        b.thumbnail_link = temp_var.thumbnail_link

        # Sometimes, the isbn found by the scraper differs from the one
        # in the file
        # In this case, a duplicate book would be created (same isbn)
        # Check for found isbn first before saving book
        possible_duplicate_book = Book.get(isbn=b.isbn)
        if possible_duplicate_book:
            b = possible_duplicate_book
        else:
            b.save()  # save book

    else:  # if book in DB, set b.id to id of DB book
        b.id = query_book.id

    # Make purchase choice
    p = PurchaseChoice()
    p.id = b.id
    p.price = randint(55, 250)  # default to 10
    p.type = 'Hardcover'
    p.isRental = False
    p.link = ''  # change to valid link
    p.local_seller_id = user_id  # change to user once login is created
    p.isLocalSeller = True
    p.save()

    booksV = BooksViewed()
    booksV.user_id = user_id
    booksV.book_id = b.id
    booksV.save()
    return True
Ejemplo n.º 5
0
def set_user_recommendation():
    """
    params: book_isbn
    """
    user_id = current_user.id
    book_isbn = request.form.get('book_isbn')
    book = Book.get(isbn=book_isbn)

    if not book:
        book = Book()
        # First try amazon scraper
        amazon_books = AmazonScraper().get_amazon_books_for_keyword(
            book_isbn,
        )
        if not amazon_books:
            # use google books if not amazon
            google_books = get_books_for_book_title_using_google_books(
                book_isbn,
            )
            if not google_books:
                return 'Book could not be found', 500
            google_book = google_books[0]
            book.title = google_book.title
            book.author = google_book.author
            book.isbn = to_isbn13(google_book.isbn)
            book.thumbnail_link = google_book.thumbnail_link
        else:
            amazon_book = amazon_books[0]
            book.isbn = to_isbn13(book_isbn)
            if 'title' in amazon_book:
                book.title = amazon_book['title']
            if 'author' in amazon_book:
                book.author = amazon_book['author']
            if not 'thumbnail_link':
                return 'No thumbnail', 500
            book.thumbnail_link = amazon_book['thumbnail_link']
        book.save()

    existing_bv = BooksViewed.get(user_id=user_id, book_id=book.id)
    if not existing_bv:
        bv = BooksViewed()
        bv.user_id = user_id
        bv.book_id = book.id
        bv.save()
        return 'Book View added successfully', 201

    return 'Book View already exists', 200