← back to Commercialrealestate
scripts/refresh-listings-rotating.sh
41 lines
#!/bin/bash
# DTD verdict C (2026-07-12): daily ROTATING listings refresh. Each day sweeps a small
# slice (~3 metros) so: every metro refreshes within ~a week, daily Browserbase spend is
# pennies, each per-metro session is short enough to beat the session-lifetime timeout,
# and the email-alerts diff surfaces new listings as a daily trickle.
#
# Deterministic rotation: day-of-year picks which SLICE of the metro list runs today.
# launchd runs with a minimal PATH that does NOT include Homebrew, so bare `node`
# fails with "command not found" (which silently made N=0 → division-by-zero, exit 0,
# no metros swept). Pin PATH so every node / node -e call resolves.
export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin"
export NODE_PATH="$HOME/.claude/skills/browserbase/node_modules"
cd "$HOME/Projects/commercialrealestate" || exit 1
SLICE=${SLICE:-3} # metros per day (override via env)
# canonical metro order (all 22 keys from metro-listings-config.json)
METROS=($(node -e "console.log(Object.keys(require('./data/metro-listings-config.json').metros).join(' '))"))
N=${#METROS[@]}
DOY=$(date +%j | sed 's/^0*//') # day of year, 1..366
START=$(( (DOY * SLICE) % N ))
echo "$(date '+%F %T') rotating refresh: N=$N metros, slice=$SLICE, doy=$DOY, start=$START"
TODAY=()
for ((i=0; i<SLICE; i++)); do TODAY+=("${METROS[$(( (START + i) % N ))]}"); done
echo "today's metros: ${TODAY[*]}"
for m in "${TODAY[@]}"; do
echo "=== $m ==="
node scripts/refresh-listings-multi.js "$m" 2>&1 | grep -E "captured|ADDED|FATAL" | sed "s/^/ [$m] /"
done
# Cost visibility (global CLAUDE.md "always show costs"): log the paid Browserbase
# sessions to the cost-tracker ledger (~1 short session per metro, ~$0.03 each).
COST_METROS="${TODAY[*]}" COST_N="$SLICE" node -e '
const fs=require("fs"); const n=+process.env.COST_N||0; const rate=0.03;
const entry={ts:new Date().toISOString(),api:"browserbase",vendor:"Browserbase",app:"crcp-listings-rotating",note:"metros: "+process.env.COST_METROS,units:[{qty:n,unit:"session",rate,subtotal:+(n*rate).toFixed(4)}],cost_usd:+(n*rate).toFixed(4)};
fs.appendFileSync(process.env.HOME+"/.claude/cost-ledger.jsonl",JSON.stringify(entry)+"\n");
console.log(" cost: ~$"+(n*rate).toFixed(2)+" ("+n+" browserbase session(s)) logged to ledger");
'
echo "$(date '+%F %T') rotating refresh done: ${TODAY[*]}"