back to scripts

nas-media-review.sh

bash 92 lines secrets redacted

Generates a review of the media library for the media agent.

Note Live script from my home-lab server. Tokens, IDs, phone numbers and other secrets have been replaced with placeholders like <WHATSAPP_GROUP_ID> — everything else is the real, running code.
#!/usr/bin/env bash
set -euo pipefail

REPORT_DIR=/home/lanky/reports
LOCK_FILE=/home/lanky/reports/nas-media-review.lock
mkdir -p "$REPORT_DIR"

if [ "${1:-}" = "--worker" ]; then
  exec 9>"$LOCK_FILE"
  if ! flock -n 9; then
    exit 0
  fi

  stamp=$(date +%Y%m%d-%H%M%S)
  started=$(date -Is)
  audit_start_lines=$(wc -l < "$REPORT_DIR/media-recycle-audit.jsonl" 2>/dev/null || echo 0)
  run_log="$REPORT_DIR/nas-media-review-$stamp.log"

  set +e
  {
    echo "NAS media review"
    echo "Started: $started"
    echo
    echo "== Exact duplicate cleanup to recycle =="
    /home/lanky/scripts/nas-duplicate-cleanup.sh --apply
    echo
    echo "== Definite junk cleanup to recycle =="
    /home/lanky/scripts/nas-junk-cleanup.sh --apply
    echo
    echo "== Playback and language audit =="
    /home/lanky/scripts/nas-media-check.py
  } > "$run_log" 2>&1
  status=$?
  set -e

  audit_end_lines=$(wc -l < "$REPORT_DIR/media-recycle-audit.jsonl" 2>/dev/null || echo 0)
  moved=$((audit_end_lines - audit_start_lines))
  moved_report="$REPORT_DIR/nas-media-recycled-$stamp.txt"
  if [ "$moved" -gt 0 ]; then
    tail -n "$moved" "$REPORT_DIR/media-recycle-audit.jsonl" |
      python3 -c 'import json,sys
print("Files moved to NAS recycle for review")
for line in sys.stdin:
    try:
        item=json.loads(line)
        print(f"{item[\"source\"]}\n  -> {item[\"destination\"]}\n  reason: {item[\"reason\"]}")
    except Exception:
        pass' > "$moved_report"
  else
    printf 'No files were moved to recycle.\n' > "$moved_report"
  fi
  moved_list=$(
    tail -n "$moved" "$REPORT_DIR/media-recycle-audit.jsonl" 2>/dev/null |
      python3 -c 'import json,sys
for line in sys.stdin:
    try:
        item=json.loads(line)
        print(f"- {item[\"source\"]} -> {item[\"destination\"]}")
    except Exception:
        pass' |
      head -20
  )

  compatibility="$REPORT_DIR/nas-media-check-latest.txt"
  summary=$(
    printf 'NAS media review completed.\n- Status: %s\n- Files moved to recycle: %s\n- Full run log: %s\n- Compatibility report: %s' \
      "$status" "$moved" "$run_log" "$compatibility"
    if [ -n "$moved_list" ]; then
      printf '\n\nMoved files (first 20):\n%s' "$moved_list"
    fi
  )
  if [ -z "${SERVICE_DESK_JOB_ID:-}" ]; then
    openclaw message send \
      --channel whatsapp \
      --target <ADMIN_PHONE> \
      --message "$summary" \
      --media "$moved_report" \
      --force-document \
      --json >> "$run_log" 2>&1 || true
  fi
  exit "$status"
fi

if flock -n "$LOCK_FILE" true; then
  nohup "$0" --worker >/dev/null 2>&1 &
  echo "NAS media review started in the background."
  echo "It will send a WhatsApp summary when complete."
  echo "Exact duplicates and definite junk will be moved to @Recycle/CodexReview."
  echo "Ambiguous playback or language findings will only be reported."
else
  echo "A NAS media review is already running."
fi

back to scripts