Initial app

This commit is contained in:
augustin64 2024-04-03 16:54:54 +02:00
parent 77597d054f
commit a1351f5187
7 changed files with 150 additions and 4 deletions

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"cSpell.words": [
"isbn",
"openlibrary"
]
}

38
app.py Normal file
View File

@ -0,0 +1,38 @@
from flask import Flask, render_template, redirect, url_for, request, abort, g
import json
from book import Book
import isbn_db
app = Flask(__name__)
app.config["DATABASE"] = "data/books.sqlite"
@app.route("/submit-isbn")
def submit_isbn():
if "isbn" not in request.args:
return "/submit-isbn?isbn=xxxxxx"
try:
book = Book(request.args["isbn"])
except ValueError:
return "Pas le bon format de QR code"
try:
book.load()
except KeyError:
isbn_db.add_book(book)
return "Pas trouvé, à rajouter manuellement"
print("Got ", book)
if isbn_db.add_book(book) == "duplicate":
return f"{book.title} ajouté (plusieurs occurrences)"
return f"{book.title} ajouté"
@app.after_request
def after_request(response):
"""Automatically close db after each request"""
if ('db' in g) and (g.db is not None):
g.db.close()
return response

View File

@ -7,6 +7,7 @@ class Book:
return None return None
try: try:
isbn = isbn.replace("-", "")
int(isbn) int(isbn)
except ValueError: except ValueError:
raise ValueError("ISBN must be an int") raise ValueError("ISBN must be an int")
@ -16,6 +17,7 @@ class Book:
self.publisher = None self.publisher = None
self.publish_date = None self.publish_date = None
self.author = None 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") r = requests.get(f"https://openlibrary.org/api/books?bibkeys=ISBN:{self.isbn}&jscmd=details&format=json")
@ -41,11 +43,12 @@ class Book:
self.publish_date = isbn_data["details"]["publish_date"] self.publish_date = isbn_data["details"]["publish_date"]
def _manual_load(self, title, publisher=None, publish_date=None, author=None): def _manual_load(self, title, publisher=None, publish_date=None, author=None, count=-1):
self.title = title self.title = title
self.publisher = publisher self.publisher = publisher
self.publish_date = publish_date self.publish_date = publish_date
self.author = author self.author = author
self.count = count
def load(self, loader="openlibrary"): def load(self, loader="openlibrary"):
if loader == "openlibrary": if loader == "openlibrary":

14
init.sh Executable file
View File

@ -0,0 +1,14 @@
#!/bin/bash
DATABASE=books.sqlite
mkdir -p data
ls data | grep "$DATABASE" > /dev/null
if [ $? -eq 0 ]; then
echo "Created data/books-backup.db"
mv "data/$DATABASE" data/books-backup.db
fi
sqlite3 "data/$DATABASE" < schema.sql

76
isbn_db.py Normal file
View File

@ -0,0 +1,76 @@
from flask import current_app
from flask import g
import sqlite3
from book import Book
def get_db():
"""Connect to the application's configured database. The connection
is unique for each request and will be reused if this is called
again.
"""
if "db" not in g:
g.db = sqlite3.connect(
current_app.config["DATABASE"], detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db
def get_book(isbn):
db = get_db()
data = db.execute(
"""
SELECT * FROM book WHERE isbn=?
""",
(isbn,)
).fetchone()
if data is None:
raise IndexError("Book not in db")
book = Book(isbn)
book._manual_load(
data["title"],
publisher=data["publisher"],
publish_date=data["publish_date"],
author=data["author"],
count=data["count"]
)
return book
def increment_count(book):
if book.count == -1:
book = get_book(book.isbn)
db = get_db()
db.execute(
"""
UPDATE book SET count=?
WHERE isbn=?
""",
(book.count+1, book.isbn)
)
db.commit()
def add_book(book):
try:
book = get_book(book.isbn)
increment_count(book)
return "duplicate"
except IndexError:
if book.count == -1:
book.count = 1
db = get_db()
db.execute(
"""
INSERT INTO book (isbn, count, title, author, publisher, publish_date)
VALUES (?, ?, ?, ?, ?, ?)
""",
(book.isbn, book.count, book.title, book.author, book.publisher, book.publish_date)
)
db.commit()
return "added"

View File

@ -1,2 +1 @@
flask flask
sqlite3

10
schema.sql Normal file
View File

@ -0,0 +1,10 @@
DROP TABLE IF EXISTS book;
CREATE TABLE book (
isbn INT PRIMARY KEY,
count INT DEFAULT 1,
title TEXT,
author TEXT,
publisher TEXT,
publish_date TEXT
);