crayon_eaters/scripts/games.py

179 lines
7.3 KiB
Python

#!/usr/bin/env python3
"""Add Steam games and move Markdown entries. Python standard library only."""
import argparse
import html
import json
import re
import unicodedata
from pathlib import Path
from urllib.error import URLError
from urllib.parse import urlencode, urlparse
from urllib.request import Request, urlopen
SRC = Path(__file__).resolve().parent.parent / "src"
HEADING = re.compile(r"^[ \t]*###[ \t]+(.+?)[ \t]*$", re.MULTILINE)
def categories():
summary = re.sub(r"<!--.*?-->", "", (SRC / "SUMMARY.md").read_text(encoding="utf-8"), flags=re.S)
return {
Path(file).stem: SRC / file
for file in re.findall(r"^\s*-\s+\[[^\]]+\]\(\.?/?([^\s)]+\.md)\)", summary, re.M)
}
def entries(path):
text = path.read_text(encoding="utf-8")
headings = list(HEADING.finditer(text))
return text, [
(match[1], match.start(), headings[i + 1].start() if i + 1 < len(headings) else len(text))
for i, match in enumerate(headings)
]
def matching(title, paths):
matches = []
for path in paths:
text, blocks = entries(path)
for name, start, end in blocks:
if name.casefold() == title.casefold():
matches.append((path, text, start, end))
return matches
def append(text, block):
# Preserve existing contents, including manually written notes and spacing.
separator = "" if not text or text.endswith("\n\n") else "\n" if text.endswith("\n") else "\n\n"
return text + separator + block
def fetch(url):
request = Request(url, headers={"User-Agent": "crayon-eaters/1.0"})
with urlopen(request, timeout=20) as response:
return response.read()
def api(endpoint, **params):
return json.loads(fetch("https://store.steampowered.com/api/" + endpoint + "?" + urlencode(params)))
def app_id(value):
if value.isascii() and value.isdigit():
return value
url = urlparse(value)
if url.scheme or value.startswith("store.steampowered.com/"):
url = urlparse(value if url.scheme else "https://" + value)
match = re.match(r"/app/(\d+)(?:/|$)", url.path)
if url.scheme not in ("http", "https") or url.hostname != "store.steampowered.com" or not match:
raise ValueError("Use a Steam /app/ URL, an app ID, or a game name.")
return match[1]
items = api("storesearch/", term=value, l="english", cc="us").get("items", [])
exact = [item for item in items if item["name"].casefold() == value.casefold()]
if len(exact) == 1:
return str(exact[0]["id"])
choices = "\n".join(f" {item['name']}: https://store.steampowered.com/app/{item['id']}/" for item in items[:10])
raise ValueError("No unique exact name match. Retry with a Steam URL or ID." + ("\n" + choices if choices else ""))
def plain(value):
return " ".join(html.unescape(re.sub(r"<[^>]*>", " ", value)).split())
def markdown(value):
return re.sub(r"([\\`*_\[\]<>])", r"\\\1", plain(value))
def add(args, paths):
ident = app_id(args.game)
result = api("appdetails", appids=ident, l="english", cc="us").get(ident, {})
if not result.get("success"):
raise ValueError(f"Steam has no available details for app {ident}.")
data = result["data"]
title = markdown(data["name"])
duplicates = matching(title, paths.values())
if duplicates:
raise ValueError("Entry already exists in: " + ", ".join(item[0].stem for item in duplicates))
slug = unicodedata.normalize("NFKD", data["name"]).encode("ascii", "ignore").decode().lower()
slug = re.sub(r"[^a-z0-9]+", "_", slug).strip("_") or "game"
image = SRC / "images" / f"{slug}_{ident}.jpg"
if image.exists():
raise ValueError(f"Image already exists: {image}")
# Steam's structured genres are a starting point; custom tags can override them.
tags = args.tags if args.tags is not None else ", ".join(g["description"] for g in data.get("genres", [])[:10])
block = (
f"### {title}\n![{title}](images/{image.name})\n"
f" * **Possesseur**: {markdown(args.owners)}\n"
f" * **Description**: {markdown(data['short_description'])}\n"
f" * **Catégories**: {markdown(tags)}\n"
)
destination = paths[args.category]
updated = append(destination.read_text(encoding="utf-8"), block)
if args.dry_run:
print(f"Would add to {destination}:\n\n{block}")
return
picture = fetch(data["header_image"])
if not picture.startswith(b"\xff\xd8\xff"):
raise ValueError("Steam header image is not a JPEG; no entry was written.")
image.parent.mkdir(exist_ok=True)
with image.open("xb") as output:
output.write(picture)
try:
destination.write_text(updated, encoding="utf-8")
except OSError:
# A failed Markdown write must not leave a misleading image behind.
if image.exists():
image.unlink()
raise
print(f"Added {data['name']} to {destination.stem}.")
def move(args, paths):
sources = [paths[args.source]] if args.source else paths.values()
found = matching(args.game, sources)
if not found:
raise ValueError(f"No entry named {args.game!r}. Use its full Markdown heading (case insensitive).")
if len(found) > 1:
raise ValueError("Entry appears in multiple categories; choose --from: " + ", ".join(item[0].stem for item in found))
source, original, start, end = found[0]
destination = paths[args.category]
if source == destination:
raise ValueError("Entry is already in that category.")
if matching(args.game, [destination]):
raise ValueError("Destination already contains that entry.")
target = destination.read_text(encoding="utf-8")
if args.dry_run:
print(f"Would move {args.game} from {source.stem} to {destination.stem}.")
return
# Write the destination first so a failed write cannot lose the source entry.
destination.write_text(append(target, original[start:end]), encoding="utf-8")
source.write_text(original[:start] + original[end:], encoding="utf-8")
print(f"Moved {args.game} from {source.stem} to {destination.stem}.")
def main():
paths = categories()
parser = argparse.ArgumentParser(description=__doc__)
commands = parser.add_subparsers(dest="command", required=True)
create = commands.add_parser("add", help="Create an entry from a Steam URL, app ID, or exact name")
create.add_argument("game")
create.add_argument("--category", choices=paths, default="news")
create.add_argument("--owners", required=True, help='For example: "Dandaedre, Vky"')
create.add_argument("--tags", help="Comma-separated tags; defaults to Steam genres")
create.add_argument("--dry-run", action="store_true", help="Preview without writing files")
transfer = commands.add_parser("move", help="Move a complete entry into another category")
transfer.add_argument("game", help="Full entry heading, case insensitive")
transfer.add_argument("category", choices=paths)
transfer.add_argument("--from", dest="source", choices=paths, help="Resolve entries present in multiple categories")
transfer.add_argument("--dry-run", action="store_true")
args = parser.parse_args()
try:
(add if args.command == "add" else move)(args, paths)
except (OSError, URLError, ValueError, KeyError) as error:
parser.exit(1, f"Error: {error}\n")
if __name__ == "__main__":
main()