Add SQLite library with API, library desktop apps and MobyGames scraper
This commit is contained in:
1 parent
6674428346
commit
a16444451a
69 files changed
+2988
-1026
No files matched your search
@@ -1,14 +1,9 @@
|
||||
import json
|
||||
import re
|
||||
import urllib.request
|
||||
|
||||
import goodreads
|
||||
from jsonfile import PROJECT_ROOT
|
||||
|
||||
SCRAPE_CACHE_DIR = PROJECT_ROOT / "tools" / "content" / ".cache" / "goodreads"
|
||||
COVERS_DIR = PROJECT_ROOT / "static" / "covers" / "books"
|
||||
COVERS_PUBLIC_PATH = "/covers/books"
|
||||
IMAGE_EXTENSION_PATTERN = re.compile(r"\.(jpe?g|png|webp|gif)(?:$|\?)", re.I)
|
||||
|
||||
|
||||
def scrape_cache_path(identifier: str):
|
||||
@@ -39,37 +34,3 @@ def scrape_book(url: str, fresh: bool = False) -> dict:
|
||||
if scraped.get("title"):
|
||||
store_scrape(scraped)
|
||||
return scraped
|
||||
|
||||
|
||||
def is_local_cover(cover: str | None) -> bool:
|
||||
return bool(cover) and cover.startswith(COVERS_PUBLIC_PATH)
|
||||
|
||||
|
||||
def existing_cover(identifier: str):
|
||||
return next(iter(sorted(COVERS_DIR.glob(f"{identifier}.*"))), None) if COVERS_DIR.exists() else None
|
||||
|
||||
|
||||
def cache_cover(identifier: str, source_url: str | None, fresh: bool = False) -> str | None:
|
||||
existing = existing_cover(identifier)
|
||||
if existing and not fresh:
|
||||
return f"{COVERS_PUBLIC_PATH}/{existing.name}"
|
||||
if not source_url or is_local_cover(source_url):
|
||||
return f"{COVERS_PUBLIC_PATH}/{existing.name}" if existing else None
|
||||
|
||||
match = IMAGE_EXTENSION_PATTERN.search(source_url)
|
||||
extension = match.group(1).lower().replace("jpeg", "jpg") if match else "jpg"
|
||||
request = urllib.request.Request(source_url, headers=goodreads.REQUEST_HEADERS)
|
||||
with urllib.request.urlopen(request, timeout=15) as response:
|
||||
image = response.read()
|
||||
|
||||
COVERS_DIR.mkdir(parents=True, exist_ok=True)
|
||||
if existing:
|
||||
existing.unlink()
|
||||
(COVERS_DIR / f"{identifier}.{extension}").write_bytes(image)
|
||||
return f"{COVERS_PUBLIC_PATH}/{identifier}.{extension}"
|
||||
|
||||
|
||||
def remove_cover(identifier: str) -> None:
|
||||
existing = existing_cover(identifier)
|
||||
if existing:
|
||||
existing.unlink()
|
||||
Reference in new issue
Block a user