From 3c998f94fc88f6a271813f627aaca65e483e9287 Mon Sep 17 00:00:00 2001 From: ariesthio Date: Sat, 24 Oct 2020 14:06:49 +0200 Subject: [PATCH] Add google book api --- utils/config-sample.py | 1 + utils/gooodreads.py | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/utils/config-sample.py b/utils/config-sample.py index 4c2c2b7..8c12a25 100644 --- a/utils/config-sample.py +++ b/utils/config-sample.py @@ -2,3 +2,4 @@ GOODREADS_PUBLIC_API_KEY = "write here your goodreads public API key" GOOGLE_SEARCH_RAPIDAPI_HOST = "" GOOGLE_SEARCH_RAPIDAPI_KEY = "" +GOOGLE_BOOK_API_KEY = "" diff --git a/utils/gooodreads.py b/utils/gooodreads.py index 9f44829..253311e 100644 --- a/utils/gooodreads.py +++ b/utils/gooodreads.py @@ -3,9 +3,11 @@ import xml.etree.ElementTree as ET import urllib.request import urllib.error +import requests + from bs4 import BeautifulSoup -from config import GOODREADS_PUBLIC_API_KEY, GOOGLE_SEARCH_RAPIDAPI_HOST, GOOGLE_SEARCH_RAPIDAPI_KEY +from config import GOODREADS_PUBLIC_API_KEY, GOOGLE_SEARCH_RAPIDAPI_HOST, GOOGLE_SEARCH_RAPIDAPI_KEY, GOOGLE_BOOK_API_KEY from googlesearch import search def get_details(book_object): @@ -13,7 +15,7 @@ def get_details(book_object): url = "http://www.goodreads.com/book/title.xml?key={}&title={}".format( GOODREADS_PUBLIC_API_KEY, urllib.parse.quote_plus(book_object["title"]) ) - + print(url) try: time_to_sleep = 1 while True: @@ -33,14 +35,26 @@ def get_details(book_object): book_object["rating"] = book.find("average_rating").text book_object["pages"] = book.find("num_pages").text book_object["image_url"] = book.find("image_url").text - if (description := book.find("description").text): + book_object["isbn"] = book.find("isbn").text + + description = book.find("description").text + if description: book_object["description"] = BeautifulSoup(description).text else: book_object["description"] = "" - book_object["isbn"] = book.find("isbn").text - print("Fetching amazon link") - import requests + # Attempt to use Google Book API + url = "https://www.googleapis.com/books/v1/volumes?q={}+inauthor:{}&key={}".format( + book_object["title"], book_object["author"], GOOGLE_BOOK_API_KEY, + ) + response = requests.request("GET", url) + for item in response.json()["items"]: + if "description" in item["volumeInfo"]: + book_object["description"] = item["volumeInfo"]["description"] + break + + print("Fetching amazon link") + url = "https://google-search3.p.rapidapi.com/api/v1/search/q=site:amazon.com {} {}".format(book_object["title"], book_object["author"]) headers = {