Add copy-book-data.py
This commit is contained in:
parent
583ae8cd80
commit
8fc94603db
41
add_category.py
Normal file
41
add_category.py
Normal file
@ -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))
|
35
add_no_isbn.py
Normal file
35
add_no_isbn.py
Normal file
@ -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}")
|
@ -125,9 +125,10 @@ def update_book():
|
|||||||
def export_csv():
|
def export_csv():
|
||||||
books = isbn_db.get_all_books()
|
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:
|
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 as file with a good filename
|
||||||
return current_app.response_class(
|
return current_app.response_class(
|
||||||
|
@ -106,3 +106,18 @@ class Book:
|
|||||||
"category": self.category,
|
"category": self.category,
|
||||||
"count": self.count,
|
"count": self.count,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
59
isbn_sort/copy-book-data.py
Normal file
59
isbn_sort/copy-book-data.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user