83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Dict, Sequence
|
|
import json
|
|
|
|
from . import register
|
|
from helper import hydrus as hydrus_wrapper
|
|
from ._shared import Cmdlet, CmdletArg, normalize_hash
|
|
from helper.logger import log
|
|
|
|
CMDLET = Cmdlet(
|
|
name="delete-url",
|
|
summary="Remove a URL association from a Hydrus file.",
|
|
usage="delete-url [-hash <sha256>] <url>",
|
|
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 remove from the file."),
|
|
],
|
|
details=[
|
|
"- Removes the URL from the Hydrus file's known URL list.",
|
|
],
|
|
)
|
|
|
|
|
|
def _parse_hash_and_rest(args: Sequence[str]) -> tuple[str | None, list[str]]:
|
|
override_hash: str | None = None
|
|
rest: list[str] = []
|
|
i = 0
|
|
while i < len(args):
|
|
a = args[i]
|
|
low = str(a).lower()
|
|
if low in {"-hash", "--hash", "hash"} and i + 1 < len(args):
|
|
override_hash = str(args[i + 1]).strip()
|
|
i += 2
|
|
continue
|
|
rest.append(a)
|
|
i += 1
|
|
return override_hash, rest
|
|
|
|
|
|
@register(["del-url", "delete-url", "delete_url"]) # aliases
|
|
def delete(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
|
|
override_hash, rest = _parse_hash_and_rest(args)
|
|
if not rest:
|
|
log("Requires a URL argument")
|
|
return 1
|
|
url = str(rest[0] or '').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.delete_url(hash_hex, url)
|
|
except Exception as exc:
|
|
log(f"Hydrus del-url failed: {exc}")
|
|
return 1
|
|
log(f"Deleted URL: {url}")
|
|
return 0
|