2025-11-25 20:09:33 -08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import Any, Dict, Sequence
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
from . import register
|
|
|
|
|
import models
|
|
|
|
|
import pipeline as ctx
|
2025-12-11 19:04:02 -08:00
|
|
|
from API import HydrusNetwork as hydrus_wrapper
|
2025-12-11 12:47:30 -08:00
|
|
|
from ._shared import Cmdlet, CmdletArg, SharedArgs, normalize_hash, get_hash_for_operation, fetch_hydrus_metadata, get_field, should_show_help
|
2025-12-11 19:04:02 -08:00
|
|
|
from SYS.logger import log
|
2025-11-25 20:09:33 -08:00
|
|
|
|
|
|
|
|
CMDLET = Cmdlet(
|
|
|
|
|
name="get-note",
|
|
|
|
|
summary="List notes on a Hydrus file.",
|
|
|
|
|
usage="get-note [-hash <sha256>]",
|
2025-12-11 12:47:30 -08:00
|
|
|
arg=[
|
|
|
|
|
SharedArgs.HASH,
|
2025-11-25 20:09:33 -08:00
|
|
|
],
|
2025-12-11 12:47:30 -08:00
|
|
|
detail=[
|
2025-11-25 20:09:33 -08:00
|
|
|
"- Prints notes by service and note name.",
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@register(["get-note", "get-notes", "get_note"]) # aliases
|
|
|
|
|
def get_notes(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
|
|
|
|
# Help
|
2025-12-11 12:47:30 -08:00
|
|
|
if should_show_help(args):
|
|
|
|
|
log(json.dumps(CMDLET, ensure_ascii=False, indent=2))
|
|
|
|
|
return 0
|
2025-11-25 20:09:33 -08:00
|
|
|
|
2025-12-11 12:47:30 -08:00
|
|
|
from ._shared import parse_cmdlet_args, get_hash_for_operation, fetch_hydrus_metadata
|
2025-11-25 20:09:33 -08:00
|
|
|
parsed = parse_cmdlet_args(args, CMDLET)
|
|
|
|
|
override_hash = parsed.get("hash")
|
|
|
|
|
|
2025-12-11 12:47:30 -08:00
|
|
|
hash_hex = get_hash_for_operation(override_hash, result)
|
2025-11-25 20:09:33 -08:00
|
|
|
if not hash_hex:
|
|
|
|
|
log("Selected result does not include a Hydrus hash")
|
|
|
|
|
return 1
|
|
|
|
|
|
2025-12-11 12:47:30 -08:00
|
|
|
meta, error_code = fetch_hydrus_metadata(config, hash_hex, include_service_keys_to_tags=False, include_notes=True)
|
|
|
|
|
if error_code != 0:
|
|
|
|
|
return error_code
|
|
|
|
|
|
2025-11-25 20:09:33 -08:00
|
|
|
notes = {}
|
|
|
|
|
if isinstance(meta, dict):
|
|
|
|
|
# Hydrus returns service_keys_to_tags; for notes we expect 'service_names_to_notes' in modern API
|
|
|
|
|
notes = meta.get('notes') or meta.get('service_names_to_notes') or {}
|
|
|
|
|
if notes:
|
|
|
|
|
ctx.emit("Notes:")
|
|
|
|
|
# Print flattened: service -> (name: text)
|
|
|
|
|
if isinstance(notes, dict) and any(isinstance(v, dict) for v in notes.values()):
|
|
|
|
|
for svc, mapping in notes.items():
|
|
|
|
|
ctx.emit(f"- {svc}:")
|
|
|
|
|
if isinstance(mapping, dict):
|
|
|
|
|
for k, v in mapping.items():
|
|
|
|
|
ctx.emit(f" • {k}: {str(v).strip()}")
|
|
|
|
|
elif isinstance(notes, dict):
|
|
|
|
|
for k, v in notes.items():
|
|
|
|
|
ctx.emit(f"- {k}: {str(v).strip()}")
|
|
|
|
|
else:
|
|
|
|
|
ctx.emit("No notes found.")
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|