Save OMDb details and posters locally
This commit is contained in:
1 parent
1f7e88f117
commit
4f78883f0b
6 files changed
+239
-7
No files matched your search
+29
-5
@@ -1,5 +1,6 @@
|
||||
import imdb
|
||||
import jsonfile
|
||||
import media_cache
|
||||
|
||||
ABOUT_FILE = "about.json"
|
||||
MEDIA_KINDS = {
|
||||
@@ -52,11 +53,13 @@ def find_index(items: list[dict], text: str) -> int | None:
|
||||
return None
|
||||
|
||||
|
||||
def look_up_name(identifier: str) -> str:
|
||||
details = imdb.lookup(identifier)
|
||||
if not details:
|
||||
def details_for(identifier: str, name: str | None) -> dict:
|
||||
try:
|
||||
return media_cache.cached_details(identifier)
|
||||
except (OSError, ValueError):
|
||||
if name:
|
||||
return {}
|
||||
raise ValueError("Could not look up the title on OMDb. Enter a name, or set OMDB_API_KEY in .env")
|
||||
return details.get("Title") or identifier
|
||||
|
||||
|
||||
def with_optional(item: dict, key: str, value) -> dict:
|
||||
@@ -83,7 +86,8 @@ def add_item(kind: str, text: str, rating: int | None, name: str | None = None,
|
||||
data, items = load_items(kind)
|
||||
if find_index(items, identifier) is not None:
|
||||
raise ValueError(f"{identifier} is already listed")
|
||||
item = build_item(name or look_up_name(identifier), imdb.title_url(identifier), rating, status, collection)
|
||||
details = details_for(identifier, name)
|
||||
item = build_item(name or details.get("title") or identifier, imdb.title_url(identifier), rating, status, collection)
|
||||
items.append(item)
|
||||
jsonfile.save(ABOUT_FILE, data)
|
||||
return item
|
||||
@@ -106,6 +110,9 @@ def remove_item(kind: str, text: str) -> dict:
|
||||
raise ValueError(f"{text} is not listed")
|
||||
removed = items.pop(index)
|
||||
jsonfile.save(ABOUT_FILE, data)
|
||||
identifier = imdb.imdb_id(removed.get("url", ""))
|
||||
if identifier:
|
||||
media_cache.remove_details(identifier)
|
||||
return removed
|
||||
|
||||
|
||||
@@ -121,3 +128,20 @@ def set_status(kind: str, text: str, status: str) -> dict:
|
||||
|
||||
def set_collection(kind: str, text: str, collection: str | None) -> dict:
|
||||
return update_item(kind, text, lambda item: with_optional(item, "collection", collection))
|
||||
|
||||
|
||||
def item_identifiers(kind: str) -> list[str]:
|
||||
_, items = load_items(kind)
|
||||
return [identifier for identifier in (imdb.imdb_id(item.get("url", "")) for item in items) if identifier]
|
||||
|
||||
|
||||
def missing_details(kind: str) -> list[str]:
|
||||
return media_cache.missing_identifiers(item_identifiers(kind))
|
||||
|
||||
|
||||
def refresh_details(identifier: str) -> dict:
|
||||
return media_cache.cached_details(identifier, fresh=True)
|
||||
|
||||
|
||||
def stored_details(identifier: str) -> dict | None:
|
||||
return media_cache.load_details().get(identifier)
|
||||
Reference in new issue
Block a user