← back to Dw Seo Report
refresh.sh
42 lines
#!/bin/bash
# Pull fresh GSC data from the local OpenSEO MCP endpoint and rebuild the report.
# Self-contained: depends only on the running OpenSEO server at :3001 + python3 + jq.
set -e
DIR="$HOME/Projects/dw-seo-report"
PROJ="20a5b2e9-ea7b-4645-964a-46f04c9992e7"
MCP="http://localhost:3001/mcp"
mkdir -p "$DIR/data"
LOG="$DIR/refresh.log"
ts() { date "+%Y-%m-%d %H:%M:%S"; }
call() { # $1=arguments-json $2=outfile
curl -s -X POST "$MCP" \
-H 'Content-Type: application/json' \
-H 'Accept: application/json, text/event-stream' \
-d "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":\"get_search_console_performance\",\"arguments\":$1}}" \
| sed 's/^data: //' | jq -c 'if .result then .result.structuredContent else . end' > "$2"
}
echo "[$(ts)] refresh start" >> "$LOG"
call "{\"projectId\":\"$PROJ\",\"dimensions\":[\"query\"],\"dateRange\":\"last_28_days\",\"rowLimit\":1000}" "$DIR/data/q.json"
call "{\"projectId\":\"$PROJ\",\"dimensions\":[\"query\",\"page\"],\"dateRange\":\"last_28_days\",\"rowLimit\":1000}" "$DIR/data/qp.json"
call "{\"projectId\":\"$PROJ\",\"dimensions\":[\"date\"],\"dateRange\":\"last_6_months\",\"rowLimit\":200}" "$DIR/data/date.json"
# sanity: all three must have rows
for f in q qp date; do
n=$(jq '.rows|length' "$DIR/data/$f.json" 2>/dev/null || echo 0)
if [ "${n:-0}" -lt 1 ]; then echo "[$(ts)] ABORT: $f.json empty (GSC/server down?)" >> "$LOG"; exit 1; fi
done
python3 "$DIR/build.py" >> "$LOG" 2>&1
echo "[$(ts)] refresh done" >> "$LOG"
# Email the report via George only when asked (weekly launchd passes --email; manual runs don't).
if [ "$1" = "--email" ]; then
if python3 "$DIR/send_report.py" >> "$LOG" 2>&1; then
echo "[$(ts)] emailed report to steve@dw via George" >> "$LOG"
else
echo "[$(ts)] EMAIL FAILED (George down?) — report still rebuilt" >> "$LOG"
fi
fi