← back to Crazy News Channel

daily-edition/run-daily.sh

34 lines

#!/usr/bin/env bash
# P24 Daily Edition runner (TK-12354). Builds today's edition (daily-edition/build.py) and,
# ONLY when P24_EDITION_PUSH=1, rsyncs the edition data + today's cartoons to the p24 prod
# docroot on Kamatera (same rsync-a-data-file pattern as allnewsdaily's push-wire.sh).
# First run of the day locks the top 6 + generates 6 cartoons (~$0.018, capped at
# P24_EDITION_CAP_USD, default $0.10/day); later runs refresh the all-news list for $0.
# Stop: unload the launchd job (com.steve.p24-daily-edition). Undo a push: re-rsync the previous
# daily-edition-data.js (git history) — nothing here deletes remote files.
set -uo pipefail
cd "$(dirname "$0")/.."
export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:$PATH"
PROD="${PROD_HOST:-root@45.61.58.125}"
REMOTE="${P24_REMOTE_DIR:-/root/Projects/crazy-news-channel}"
TODAY="$(date +%F)"
echo "=== $(date '+%F %T') p24 daily edition start"
python3 daily-edition/build.py; rc=$?
echo "build exit=$rc"
if [ "$rc" -ne 0 ] && [ "$rc" -ne 3 ]; then echo "build failed — not pushing"; exit "$rc"; fi
# validate before any push: non-empty edition with >=1 story
node -e "const vm=require('vm'),fs=require('fs');const c={window:{}};vm.createContext(c);vm.runInContext(fs.readFileSync('daily-edition-data.js','utf8'),c);const d=c.window.P24_DAILY;if(!d||!d.all||!d.all.length||!d.top||!d.top.length)process.exit(2)" \
  || { echo "daily-edition-data.js invalid — not pushing"; exit 2; }
if [ "${P24_EDITION_PUSH:-0}" = "1" ]; then
  SSH="ssh -o ConnectTimeout=10 -o BatchMode=yes"
  $SSH "$PROD" "mkdir -p '$REMOTE/daily/$TODAY'" \
   && rsync -az -e "$SSH" "daily/$TODAY/" "$PROD:$REMOTE/daily/$TODAY/" \
   && rsync -az -e "$SSH" daily/index.json "$PROD:$REMOTE/daily/index.json" \
   && rsync -az -e "$SSH" daily-edition-data.js "$PROD:$REMOTE/daily-edition-data.js" \
   && echo "pushed $TODAY edition → $PROD:$REMOTE" || { echo "PUSH FAILED"; exit 4; }
else
  echo "P24_EDITION_PUSH!=1 — built locally only, not pushed"
fi
echo "=== $(date '+%F %T') done (build rc=$rc)"
exit "$rc"