OverviewFilesCommitsIssuesDocsReleasesPeers

keet-tui / keet-watch.sh

55 lines · bash ref:
#!/bin/bash
# keet-watch.sh — persistent watcher for Keet messages via SSE
# Usage: ./keet-watch.sh [roomId]
# Logs to /tmp/keet-watch.log and stdout

ROOM_ID="${1:-ar5jd5tc3od5hbxxg6818pw9ikpgeq39y8ybgonyu9n179cdz8po}"
PORT=3117
LOG="/tmp/keet-watch.log"

log() {
  local ts
  ts="$(date '+%Y-%m-%d %H:%M:%S')"
  echo "[$ts] $*" | tee -a "$LOG"
}

watch_room() {
  log "=== keet watcher started for room $ROOM_ID ==="

  while true; do
    log "Connecting to SSE stream on :${PORT}..."

    curl -sN "http://localhost:${PORT}/rpc/watch/${ROOM_ID}" 2>/dev/null | while IFS= read -r line; do
      case "$line" in
        "event: ready")
          log "--- live ---"
          ;;
        "data: "*)
          data="${line#data: }"
          # Extract name and text with python (available everywhere)
          parsed=$(python3 -c "
import json, sys
try:
    m = json.loads(sys.argv[1])
    name = m.get('name') or (m.get('memberId','???')[:8])
    text = m.get('text','')
    system = m.get('system')
    if system:
        print(f'* system event')
    elif text:
        print(f'{name}: {text}')
except:
    pass
" "$data" 2>/dev/null)
          [ -n "$parsed" ] && log "$parsed"
          ;;
      esac
    done

    log "Stream ended, reconnecting in 5s..."
    sleep 5
  done
}

watch_room