Cache Goodreads data and slow down bulk requests
This commit is contained in:
1 parent
af73bb5b26
commit
7ef3a29ece
6 files changed
+162
-13
No files matched your search
+26
-3
@@ -1,5 +1,6 @@
|
||||
import datetime
|
||||
|
||||
import book_cache
|
||||
import goodreads
|
||||
import jsonfile
|
||||
|
||||
@@ -47,13 +48,21 @@ def short_description(text: str | None) -> str | None:
|
||||
return text[:DESCRIPTION_MAX_LENGTH].rsplit(" ", 1)[0] + "..."
|
||||
|
||||
|
||||
def local_cover(scraped: dict) -> str | None:
|
||||
try:
|
||||
return book_cache.cache_cover(scraped["goodreadsId"], scraped["cover"]) or scraped["cover"]
|
||||
except OSError:
|
||||
return scraped["cover"]
|
||||
|
||||
|
||||
def stored_fields(scraped: dict) -> dict:
|
||||
return {
|
||||
"goodreadsId": scraped["goodreadsId"],
|
||||
"url": scraped["url"],
|
||||
"title": scraped["title"],
|
||||
"authors": scraped["authors"],
|
||||
"cover": scraped["cover"],
|
||||
"cover": local_cover(scraped),
|
||||
"coverSource": scraped["cover"],
|
||||
"description": short_description(scraped["description"]),
|
||||
"pages": scraped["pages"],
|
||||
"published": scraped["published"],
|
||||
@@ -107,7 +116,7 @@ def add_book(url: str, shelf: str = "read", rating: int | None = None) -> dict:
|
||||
if find_index(load_books(), identifier) is not None:
|
||||
raise ValueError(f"{identifier} is already listed")
|
||||
|
||||
scraped = goodreads.scrape_book(url)
|
||||
scraped = book_cache.scrape_book(url)
|
||||
if not scraped["title"]:
|
||||
raise ValueError("Could not read the book from Goodreads")
|
||||
|
||||
@@ -130,6 +139,7 @@ def remove_book(text: str) -> dict:
|
||||
raise ValueError(f"{text} is not listed")
|
||||
removed = books.pop(index)
|
||||
save_books(books)
|
||||
book_cache.remove_cover(removed["goodreadsId"])
|
||||
return removed
|
||||
|
||||
|
||||
@@ -156,5 +166,18 @@ def refresh_book(text: str) -> dict:
|
||||
index = find_index(books, text)
|
||||
if index is None:
|
||||
raise ValueError(f"{text} is not listed")
|
||||
scraped = goodreads.scrape_book(books[index]["url"])
|
||||
scraped = book_cache.scrape_book(books[index]["url"], fresh=True)
|
||||
return update_book(text, lambda book: {**book, **stored_fields(scraped)})
|
||||
|
||||
|
||||
def books_missing_local_covers() -> list[dict]:
|
||||
return [book for book in load_books() if not book_cache.is_local_cover(book.get("cover"))]
|
||||
|
||||
|
||||
def cache_book_cover(identifier: str) -> dict:
|
||||
def change(book: dict) -> dict:
|
||||
source = book.get("coverSource") or book.get("cover")
|
||||
cover = book_cache.cache_cover(identifier, source)
|
||||
return {**book, "cover": cover or book.get("cover"), "coverSource": source}
|
||||
|
||||
return update_book(identifier, change)
|
||||
Reference in new issue
Block a user