From 8fc94603db8ef10873bbac296ca1f164812c1c83 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Tue, 8 Oct 2024 14:37:12 +0200 Subject: [PATCH] Add copy-book-data.py --- add_category.py | 41 ++++++++++++++++++++++++++ add_no_isbn.py | 35 ++++++++++++++++++++++ isbn_sort/app.py | 7 +++-- isbn_sort/book.py | 17 ++++++++++- isbn_sort/copy-book-data.py | 59 +++++++++++++++++++++++++++++++++++++ 5 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 add_category.py create mode 100644 add_no_isbn.py create mode 100644 isbn_sort/copy-book-data.py diff --git a/add_category.py b/add_category.py new file mode 100644 index 0000000..4984269 --- /dev/null +++ b/add_category.py @@ -0,0 +1,41 @@ +""" +Connect to database, add a categories column if non-existant, +for each element of the db, ask for a category and update the db. +""" +import sqlite3 +from pyfzf.pyfzf import FzfPrompt +import enquiries + +fzf = FzfPrompt() + +conn = sqlite3.connect("books-cat.sqlite") +conn.row_factory = sqlite3.Row +c = conn.cursor() + +categories = [ + # TODO : Put your categories here +] + + +def ensure_categories(): + c.execute("PRAGMA table_info(book)") + columns = c.fetchall() + + if not any([col["name"] == "category" for col in columns]): + c.execute("ALTER TABLE book ADD COLUMN category TEXT DEFAULT ''") + conn.commit() + print("Added category column") + + +def add_category(category): + c.execute("SELECT * FROM book WHERE category=''") + books = c.fetchall() + + selection = fzf.prompt([b["title"] for b in books], f"-m --prompt \"{category} > \"") + for title in selection: + isbn = [book["isbn"] for book in books if book["title"] == title][0] + c.execute("UPDATE book SET category=? WHERE isbn=?", (category, isbn)) + conn.commit() + +ensure_categories() +add_category(enquiries.choose("Choose a category", categories)) \ No newline at end of file diff --git a/add_no_isbn.py b/add_no_isbn.py new file mode 100644 index 0000000..a23fd7d --- /dev/null +++ b/add_no_isbn.py @@ -0,0 +1,35 @@ +""" +Add books with no ISBN from a csv file +""" +import requests +import random + +server = "http://localhost:5000/app" +IDENTIFIER = "ABC" # TODO : fill in any short identifier + +def random_isbn(): + return IDENTIFIER+"".join([str(random.randint(0, 9)) for _ in range(7)]) + +def add_book(data): + isbn = random_isbn() + requests.get(server+f"/submit-isbn?isbn={isbn}&no-search=true") + + data["isbn"] = isbn + r = requests.post(server+"/update-book", data=data) + return isbn + + +with open("livres.csv") as f: + for line in f: + data = [el.strip() for el in line.strip().split(";")] + book = { + "title": data[0], + "author": data[1] if data[1] != "" else "None", + "publisher": data[2] if data[2] != "" else "None", + "publish_date": data[3] if data[3] != "" else "None", + "count": data[4] if data[4] != "" else 1 + } + if book["title"] in ["Titre", ""]: + continue + isbn = add_book(book) + print(f"Added {book['title']} with isbn {isbn}") diff --git a/isbn_sort/app.py b/isbn_sort/app.py index 93c75ed..8391e3d 100644 --- a/isbn_sort/app.py +++ b/isbn_sort/app.py @@ -125,9 +125,10 @@ def update_book(): def export_csv(): books = isbn_db.get_all_books() - csv = "ISBN;Titre;Auteur;Éditeur;Date;Nombre\n" + csv = "ISBN;Titre;Auteur;Éditeur;Date;Catégorie;Quantité\n" for book in books: - csv += f"{book.isbn};{book.title};{book.author};{book.publisher};{book.publish_date};{book.count}\n" + book.replace(";", ",") + csv += f"{book.isbn};{book.title};{book.author};{book.publisher};{book.publish_date};{book.category};{book.count}\n" # return as file with a good filename return current_app.response_class( @@ -152,4 +153,4 @@ def listen(): def ping(): msg = sse.format_sse(data={"type": "pong"}) announcer.announce(msg=msg) - return {}, 200 \ No newline at end of file + return {}, 200 diff --git a/isbn_sort/book.py b/isbn_sort/book.py index 35a1417..a714953 100644 --- a/isbn_sort/book.py +++ b/isbn_sort/book.py @@ -105,4 +105,19 @@ class Book: "author": self.author, "category": self.category, "count": self.count, - } \ No newline at end of file + } + + def replace(self, pattern, replacement): + """Replace element in all fields""" + def rep(el): + if isinstance(el, str): + return el.replace(pattern, replacement) + return el + + self.isbn = rep(self.isbn) + self.title = rep(self.title) + self.publisher = rep(self.publisher) + self.publish_date = rep(self.publish_date) + self.author = rep(self.author) + self.category = rep(self.category) + self.count = rep(self.count) \ No newline at end of file diff --git a/isbn_sort/copy-book-data.py b/isbn_sort/copy-book-data.py new file mode 100644 index 0000000..fc02b60 --- /dev/null +++ b/isbn_sort/copy-book-data.py @@ -0,0 +1,59 @@ +""" +If database has been exported to an Excel/ Google Sheets/... document, +allows to easily add books by copy-pasting +""" +import subprocess +import sys +import os + +import book as bk + + +def clip_copy(data): + subprocess.call(["wl-copy", data]) + +def main(isbns, google_books_key): + config = { + "GOOGLE_BOOKS_KEY": google_books_key + } + def find_book(isbn): + book = bk.Book(isbn) + try: + book.load(loader="google_books", config=config) + return book + except KeyError: + try: + book.load(loader="openlibrary", config=config) + return book + except KeyError: + print("Pas trouvé, à rajouter manuellement") + return None + + res = "" + for isbn in isbns: + isbn_data = find_book(isbn) + if isbn_data is not None: + elements = [isbn_data.isbn, isbn_data.title, isbn_data.author, isbn_data.publisher, isbn_data.publish_date] + elements = [i if i is not None else "" for i in elements] + res += "\t".join(elements)+"\tAucune\t1\n" + + return res + + + +if __name__ == "__main__": + google_books_key = None + if os.path.exists(".google-books-key"): + with open(".google-books-key") as f: + google_books_key = f.read().strip() + else: + print("No Google books api key found") + + + if len(sys.argv) == 1: + res = main([input("ISBN >> ")], google_books_key) + else: + res = main(sys.argv[1:], google_books_key) + + clip_copy(res) + print(res)