diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..4720c9f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "cSpell.words": [ + "isbn", + "openlibrary" + ] +} \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..6ad7fd8 --- /dev/null +++ b/app.py @@ -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 diff --git a/book.py b/book.py index 085dec8..a9e046b 100644 --- a/book.py +++ b/book.py @@ -7,6 +7,7 @@ class Book: return None try: + isbn = isbn.replace("-", "") int(isbn) except ValueError: raise ValueError("ISBN must be an int") @@ -16,6 +17,7 @@ class Book: self.publisher = None self.publish_date = None self.author = None + self.count = -1 def _openlibrary_load(self): r = requests.get(f"https://openlibrary.org/api/books?bibkeys=ISBN:{self.isbn}&jscmd=details&format=json") @@ -33,7 +35,7 @@ class Book: self.title = isbn_data["details"]["title"] if "publishers" in isbn_data["details"] and len(isbn_data["details"]["publishers"]) > 0: self.publisher = isbn_data["details"]["publishers"][0] - + if "authors" in isbn_data["details"] and len(isbn_data["details"]["authors"]) > 0: self.author = isbn_data["details"]["authors"][0]["name"] @@ -41,11 +43,12 @@ class Book: 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.publisher = publisher self.publish_date = publish_date self.author = author + self.count = count def load(self, loader="openlibrary"): if loader == "openlibrary": diff --git a/init.sh b/init.sh new file mode 100755 index 0000000..a1b7f24 --- /dev/null +++ b/init.sh @@ -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 \ No newline at end of file diff --git a/isbn_db.py b/isbn_db.py new file mode 100644 index 0000000..e82a4dc --- /dev/null +++ b/isbn_db.py @@ -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" + diff --git a/requirements.txt b/requirements.txt index c90b7f2..8ab6294 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1 @@ -flask -sqlite3 \ No newline at end of file +flask \ No newline at end of file diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..c292837 --- /dev/null +++ b/schema.sql @@ -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 +); \ No newline at end of file