isbn-sort/isbn_sort/copy-book-data.py

60 lines
1.5 KiB
Python
Raw Normal View History

2024-10-08 14:37:12 +02:00
"""
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
from modules import book as bk
2024-10-08 14:37:12 +02:00
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)