from __future__ import annotations from typing import Any, Dict, Sequence import json from . import register import models import pipeline as ctx from helper import hydrus as hydrus_wrapper from ._shared import Cmdlet, CmdletArg, normalize_hash from helper.logger import log CMDLET = Cmdlet( name="add-url", summary="Associate a URL with a Hydrus file.", usage="add-url [-hash ] ", args=[ CmdletArg("-hash", description="Override the Hydrus file hash (SHA256) to target instead of the selected result."), CmdletArg("url", required=True, description="The URL to associate with the file."), ], details=[ "- Adds the URL to the Hydrus file's known URL list.", ], ) @register(["add-url", "ass-url", "associate-url", "add_url"]) # aliases def add(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int: # Help try: if any(str(a).lower() in {"-?", "/?", "--help", "-h", "help", "--cmdlet"} for a in args): log(json.dumps(CMDLET, ensure_ascii=False, indent=2)) return 0 except Exception: pass from ._shared import parse_cmdlet_args parsed = parse_cmdlet_args(args, CMDLET) override_hash = parsed.get("hash") url = parsed.get("url") if not url: log("Requires a URL argument") return 1 url = str(url).strip() if not url: log("Requires a non-empty URL") return 1 # Handle @N selection which creates a list - extract the first item if isinstance(result, list) and len(result) > 0: result = result[0] hash_hex = normalize_hash(override_hash) if override_hash else normalize_hash(getattr(result, "hash_hex", None)) if not hash_hex: log("Selected result does not include a Hydrus hash") return 1 try: client = hydrus_wrapper.get_client(config) except Exception as exc: log(f"Hydrus client unavailable: {exc}") return 1 if client is None: log("Hydrus client unavailable") return 1 try: client.associate_url(hash_hex, url) except Exception as exc: log(f"Hydrus add-url failed: {exc}") return 1 preview = hash_hex[:12] + ('…' if len(hash_hex) > 12 else '') ctx.emit(f"Associated URL with {preview}: {url}") return 0