Add google books provider

This commit is contained in:
augustin64 2024-04-08 12:18:49 +02:00
parent cf18eae3b1
commit e48339e245
5 changed files with 65 additions and 12 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.venv
data
**/__pycache__
.google-books-key

View File

@ -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)

View File

@ -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é"

View File

@ -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:
@ -43,6 +43,36 @@ class Book:
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
self.publisher = publisher
@ -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:

1
start.sh Normal file
View File

@ -0,0 +1 @@
flask run --host=0.0.0.0 --port=5000