示例#1
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