37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
import json
|
|
|
|
import goodreads
|
|
from jsonfile import PROJECT_ROOT
|
|
|
|
SCRAPE_CACHE_DIR = PROJECT_ROOT / "tools" / "content" / ".cache" / "goodreads"
|
|
|
|
|
|
def scrape_cache_path(identifier: str):
|
|
return SCRAPE_CACHE_DIR / f"{identifier}.json"
|
|
|
|
|
|
def cached_scrape(identifier: str) -> dict | None:
|
|
path = scrape_cache_path(identifier)
|
|
if not path.exists():
|
|
return None
|
|
try:
|
|
return json.loads(path.read_text(encoding="utf-8"))
|
|
except ValueError:
|
|
return None
|
|
|
|
|
|
def store_scrape(scraped: dict) -> None:
|
|
SCRAPE_CACHE_DIR.mkdir(parents=True, exist_ok=True)
|
|
scrape_cache_path(scraped["goodreadsId"]).write_text(json.dumps(scraped, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
|
|
|
|
def scrape_book(url: str, fresh: bool = False) -> dict:
|
|
identifier = goodreads.book_id(url)
|
|
cached = None if fresh or not identifier else cached_scrape(identifier)
|
|
if cached:
|
|
return cached
|
|
scraped = goodreads.scrape_book(url)
|
|
if scraped.get("title"):
|
|
store_scrape(scraped)
|
|
return scraped
|