From e48339e2456f57368ba42b37a30564fba2184551 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Mon, 8 Apr 2024 12:18:49 +0200 Subject: [PATCH] Add google books provider --- .gitignore | 3 ++- isbn_sort/__init__.py | 7 +++++++ isbn_sort/app.py | 26 ++++++++++++++++++-------- isbn_sort/book.py | 40 +++++++++++++++++++++++++++++++++++++--- start.sh | 1 + 5 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 start.sh diff --git a/.gitignore b/.gitignore index f1023f8..941a021 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .venv data -**/__pycache__ \ No newline at end of file +**/__pycache__ +.google-books-key \ No newline at end of file diff --git a/isbn_sort/__init__.py b/isbn_sort/__init__.py index ece1d82..b45591d 100644 --- a/isbn_sort/__init__.py +++ b/isbn_sort/__init__.py @@ -9,6 +9,13 @@ app = Flask(__name__) app.config["DATABASE"] = "data/books.sqlite" app.config["SECRET_KEY"] = secrets.token_hex() +if not os.path.exists(".google-books-key"): + print("No Google books api key found") + app.config["GOOGLE_BOOKS_KEY"] = None +else: + with open(".google-books-key") as f: + app.config["GOOGLE_BOOKS_KEY"] = f.read().strip() + app.register_blueprint(app_bp) diff --git a/isbn_sort/app.py b/isbn_sort/app.py index 4d40844..523302e 100644 --- a/isbn_sort/app.py +++ b/isbn_sort/app.py @@ -35,16 +35,26 @@ def submit_isbn(): return "Pas le bon format de QR code" try: - book.load() - except KeyError: - isbn_db.add_book(book) - announce_book(book) - return "Pas trouvé, à rajouter manuellement" - - print("Got ", book) - if isbn_db.add_book(book) == "duplicate": + book = isbn_db.get_book(book.isbn) + assert isbn_db.add_book(book) == "duplicate" # duplicate announce_book(isbn_db.get_book(book.isbn), msg_type="update_book") return f"{book.title} ajouté (plusieurs occurrences)" + except IndexError: + pass + + try: + book.load(loader="google_books", config=current_app.config) + print("Got ", book, "[Google]") + except KeyError: + try: + book.load(loader="openlibrary", config=current_app.config) + print("Got ", book, "[OpenLibrary]") + except KeyError: + isbn_db.add_book(book) + announce_book(book) + return "Pas trouvé, à rajouter manuellement" + + isbn_db.add_book(book) announce_book(book) return f"{book.title} ajouté" diff --git a/isbn_sort/book.py b/isbn_sort/book.py index f35cbd6..f4d7c2f 100644 --- a/isbn_sort/book.py +++ b/isbn_sort/book.py @@ -19,7 +19,7 @@ class Book: self.author = None self.count = -1 - def _openlibrary_load(self): + def _openlibrary_load(self, _): r = requests.get(f"https://openlibrary.org/api/books?bibkeys=ISBN:{self.isbn}&jscmd=details&format=json") if r.status_code != 200: @@ -42,6 +42,36 @@ class Book: if "publish_date" in isbn_data["details"]: self.publish_date = isbn_data["details"]["publish_date"] + + def _google_books_load(self, config): + if config["GOOGLE_BOOKS_KEY"] is None: + key_param = "" + else: + key_param = f"&key={config['GOOGLE_BOOKS_KEY']}" + + r = requests.get(f"https://www.googleapis.com/books/v1/volumes?q=isbn:{self.isbn}{key_param}") + + if r.status_code != 200: + raise requests.RequestException(f"Got an error on the request. Status code: {r.status_code}") + + data = json.loads(r.content) + + if data["totalItems"] == 0: + raise KeyError("Not found in Google Books") + + item = data["items"][0] + + self.title = item["volumeInfo"]["title"] + + if "publisher" in item["volumeInfo"]: + self.publisher = item["volumeInfo"]["publisher"] + + if "publishedDate" in item["volumeInfo"]: + self.publish_date = item["volumeInfo"]["publishedDate"] + + if "authors" in item["volumeInfo"]: + self.author = item["volumeInfo"]["authors"][0] + def _manual_load(self, title, publisher=None, publish_date=None, author=None, count=-1): self.title = title @@ -50,9 +80,13 @@ class Book: self.author = author self.count = count - def load(self, loader="openlibrary"): + def load(self, config, loader="openlibrary"): if loader == "openlibrary": - return self._openlibrary_load() + return self._openlibrary_load(config) + elif loader == "google_books": + return self._google_books_load(config) + else: + raise ValueError("Invalid loader") def __repr__(self): if self.title is None: diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..eac4353 --- /dev/null +++ b/start.sh @@ -0,0 +1 @@ +flask run --host=0.0.0.0 --port=5000 \ No newline at end of file