Compare commits

..

No commits in common. "e48339e2456f57368ba42b37a30564fba2184551" and "6ebdd4e8edaf0081cd338d04422ad7d3b16f8caf" have entirely different histories.

7 changed files with 17 additions and 79 deletions

1
.gitignore vendored
View File

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

View File

@ -1,6 +1,5 @@
from flask import Flask, redirect, g, send_file
from flask import Flask, redirect, g
import secrets
import os
from .app import bp as app_bp
@ -9,29 +8,10 @@ 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,26 +35,16 @@ def submit_isbn():
return "Pas le bon format de QR code"
try:
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]")
book.load()
except KeyError:
isbn_db.add_book(book)
announce_book(book)
return "Pas trouvé, à rajouter manuellement"
isbn_db.add_book(book)
print("Got ", book)
if isbn_db.add_book(book) == "duplicate":
announce_book(isbn_db.get_book(book.isbn), msg_type="update_book")
return f"{book.title} ajouté (plusieurs occurrences)"
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,36 +43,6 @@ 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
@ -80,13 +50,9 @@ class Book:
self.author = author
self.count = count
def load(self, config, loader="openlibrary"):
def load(self, loader="openlibrary"):
if loader == "openlibrary":
return self._openlibrary_load(config)
elif loader == "google_books":
return self._google_books_load(config)
else:
raise ValueError("Invalid loader")
return self._openlibrary_load()
def __repr__(self):
if self.title is None:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

View File

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

View File

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