Move scraped book details into data folder
This commit is contained in:
1 parent
9ce67f7600
commit
1f7e88f117
9 files changed
+190
-252
No files matched your search
+14
-2
@@ -5,6 +5,8 @@ import goodreads
|
||||
import jsonfile
|
||||
|
||||
BOOKS_FILE = "books.json"
|
||||
BOOK_INFO_FILE = "bookDetails.json"
|
||||
PERSONAL_KEYS = ["goodreadsId", "title", "url", "shelf", "rating", "added", "series"]
|
||||
SHELVES = ["read", "reading", "want-to-read"]
|
||||
DESCRIPTION_MAX_LENGTH = 600
|
||||
GENRE_LIMIT = 5
|
||||
@@ -25,11 +27,21 @@ def check_shelf(shelf: str) -> str:
|
||||
def load_books() -> list[dict]:
|
||||
if not jsonfile.content_path(BOOKS_FILE).exists():
|
||||
return []
|
||||
return jsonfile.load(BOOKS_FILE)
|
||||
info = jsonfile.load_data(BOOK_INFO_FILE, {})
|
||||
return [{**info.get(book["goodreadsId"], {}), **book} for book in jsonfile.load(BOOKS_FILE)]
|
||||
|
||||
|
||||
def personal_fields(book: dict) -> dict:
|
||||
return {key: book[key] for key in PERSONAL_KEYS if key in book}
|
||||
|
||||
|
||||
def info_fields(book: dict) -> dict:
|
||||
return {key: value for key, value in book.items() if key not in PERSONAL_KEYS}
|
||||
|
||||
|
||||
def save_books(books: list[dict]) -> None:
|
||||
jsonfile.save(BOOKS_FILE, books)
|
||||
jsonfile.save(BOOKS_FILE, [personal_fields(book) for book in books])
|
||||
jsonfile.save_data(BOOK_INFO_FILE, {book["goodreadsId"]: info_fields(book) for book in books})
|
||||
|
||||
|
||||
def find_index(books: list[dict], text: str) -> int | None:
|
||||
|
||||
Reference in new issue
Block a user