Compare commits

...

2 Commits

Author SHA1 Message Date
e48339e245 Add google books provider 2024-04-08 12:18:49 +02:00
cf18eae3b1 Add favicon 2024-04-08 12:18:12 +02:00
7 changed files with 79 additions and 17 deletions

3
.gitignore vendored
View File

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

View File

@ -1,5 +1,6 @@
from flask import Flask, redirect, g
from flask import Flask, redirect, g, send_file
import secrets
import os
from .app import bp as app_bp
@ -8,10 +9,29 @@ 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)
@app.route("/")
def index():
return redirect("/app", 301)
@app.route("/favicon.ico")
def favicon():
return send_file("static/favicon.ico")
@app.route("/<path>")
def handle_redirect(path):
path = path if path.startswith("/") else "/"+path
if not path.startswith("/app"):
return redirect("/app"+path, 301)
abort(404)

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View File

@ -33,7 +33,6 @@
--color-mantle: #181825;
--color-crust: #11111b;
--color-action: #333d5d;
--font-family: Iosevka Web;
}*/
/* Light theme: Catppuccin Latte */
@ -69,9 +68,6 @@
}
/*}*/
* {
font-family: var(--font-family);
}
a {
text-decoration: none;

1
start.sh Normal file
View File

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