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:
|
||||
|
||||
@@ -3,6 +3,7 @@ from pathlib import Path
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
||||
CONTENT_DIR = PROJECT_ROOT / "content"
|
||||
DATA_DIR = PROJECT_ROOT / "data"
|
||||
INLINE_LIST_MAX_LENGTH = 80
|
||||
INDENT = "\t"
|
||||
|
||||
@@ -11,14 +12,35 @@ def content_path(name: str) -> Path:
|
||||
return CONTENT_DIR / name
|
||||
|
||||
|
||||
def load(name: str):
|
||||
with content_path(name).open(encoding="utf-8") as file:
|
||||
def data_path(name: str) -> Path:
|
||||
return DATA_DIR / name
|
||||
|
||||
|
||||
def read_json(path: Path):
|
||||
with path.open(encoding="utf-8") as file:
|
||||
return json.load(file)
|
||||
|
||||
|
||||
def write_json(path: Path, data) -> None:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(format_value(data, 0) + "\n", encoding="utf-8")
|
||||
|
||||
|
||||
def load(name: str):
|
||||
return read_json(content_path(name))
|
||||
|
||||
|
||||
def save(name: str, data) -> None:
|
||||
text = format_value(data, 0) + "\n"
|
||||
content_path(name).write_text(text, encoding="utf-8")
|
||||
write_json(content_path(name), data)
|
||||
|
||||
|
||||
def load_data(name: str, default):
|
||||
path = data_path(name)
|
||||
return read_json(path) if path.exists() else default
|
||||
|
||||
|
||||
def save_data(name: str, data) -> None:
|
||||
write_json(data_path(name), data)
|
||||
|
||||
|
||||
def is_primitive(value) -> bool:
|
||||
|
||||
Reference in new issue
Block a user