Add SQLite library with API, library desktop apps and MobyGames scraper

This commit is contained in:
SirBlob committed 2026-11-02 23:26:31 -05:00
1 parent 6674428346
commit a16444451a
69 files changed
+2988 -1026

No files matched your search

+21 -16
View File
@@ -1,17 +1,16 @@
import json
import os
import re
import urllib.error
import urllib.parse
import urllib.request
from jsonfile import PROJECT_ROOT
from envfile import env_value
IMDB_ID_PATTERN = re.compile(r"tt\d{7,9}")
ENV_FILE = PROJECT_ROOT / ".env"
OMDB_URL = "https://www.omdbapi.com/"
OMDB_DEMO_URL = "http://www.omdbapi.com/demo.aspx/"
RATE_LIMITED_STATUS = 401
MISSING_VALUE = "N/A"
def imdb_id(text: str) -> str | None:
@@ -23,25 +22,13 @@ def title_url(identifier: str) -> str:
return f"https://www.imdb.com/title/{identifier}/"
def omdb_api_key() -> str | None:
if os.environ.get("OMDB_API_KEY"):
return os.environ["OMDB_API_KEY"]
if not ENV_FILE.exists():
return None
for line in ENV_FILE.read_text(encoding="utf-8").splitlines():
key, _, value = line.partition("=")
if key.strip() == "OMDB_API_KEY" and value.strip():
return value.strip().strip("\"'")
return None
def request_json(url: str, parameters: dict) -> dict:
with urllib.request.urlopen(f"{url}?{urllib.parse.urlencode(parameters)}", timeout=10) as response:
return json.load(response)
def lookup(identifier: str) -> dict | None:
api_key = omdb_api_key()
api_key = env_value("OMDB_API_KEY")
if not api_key:
return None
try:
@@ -54,3 +41,21 @@ def lookup(identifier: str) -> dict | None:
except (OSError, ValueError):
return None
return result if result.get("Response") == "True" else None
def present(value: str | None) -> str | None:
return value if value and value != MISSING_VALUE else None
def details(identifier: str) -> dict:
result = lookup(identifier)
if not result:
raise ValueError(f"Could not look up {identifier} on OMDb. Check OMDB_API_KEY in .env")
return {
"name": present(result.get("Title")),
"year": present(result.get("Year")),
"genre": present(result.get("Genre")),
"runtime": present(result.get("Runtime")),
"plot": present(result.get("Plot")),
"posterSource": present(result.get("Poster")),
}