Files
ginnoirandClaude Opus 5.5 ca257000c8 feat(media): add mam-bonus daily upload-credit buyer
Spends MAM bonus points once a day (04:17) on upload credit via
json/bonusBuy.php (spendtype=upload, "Max Affordable ") so seedbonus
never caps out. Dedicated MAM session; rotated mam_id is persisted in a
curl cookie jar under /config/mam-bonus. Skips when below the 50 GiB
minimum; failures alert ntfy topic `mam`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 17:20:02 -06:00

78 lines
3.1 KiB
Bash

#!/bin/sh
# Daily MAM bonus-point spend: buy as much upload credit as the balance allows
# so seedbonus never sits at the cap. Run by crond in the mam-bonus container.
#
# Session handling: MAM rotates mam_id on responses. Prowlarr survives that by
# saving the new cookie; autobrr didn't and died (2026-11-20). So this keeps a
# curl cookie jar in /data and lets curl re-save the rotated value on each call.
# The jar is (re)seeded from MAM_BONUS_MAM_ID only when that env value changes.
#
# Env (stack.env):
# MAM_BONUS_MAM_ID bare mam_id of a DEDICATED MAM session (not shared w/ autobrr/prowlarr)
# MAM_BONUS_RESERVE points to leave unspent (default 0 = "Max Affordable ")
# MAM_BONUS_NTFY ntfy topic URL for failures (default http://ntfy:80/mam)
# MAM_BONUS_DRY_RUN 1 = check balance + log what would be bought, don't buy
set -eu
BASE=https://www.myanonamouse.net
JAR=/data/cookies.txt
SEED=/data/seed.sha
RESERVE="${MAM_BONUS_RESERVE:-0}"
NTFY="${MAM_BONUS_NTFY:-http://ntfy:80/mam}"
PER_GIB=500 # MAM store price: 500 BP per GiB of upload
MIN_GIB=50 # API rejects < 50 GiB
log() { echo "$(date '+%F %T') mam-bonus: $*"; }
fail() {
log "FAIL: $*"
curl -fsS -m 15 -H "Title: MAM bonus buy failed" -H "Priority: high" -H "Tags: warning" \
-d "$*" "$NTFY" >/dev/null 2>&1 || log "(ntfy post failed too)"
exit 1
}
[ -n "${MAM_BONUS_MAM_ID:-}" ] || fail "MAM_BONUS_MAM_ID is not set in media stack.env"
# (Re)seed the jar when the configured session changes.
want=$(printf %s "$MAM_BONUS_MAM_ID" | sha256sum | cut -d' ' -f1)
if [ ! -s "$JAR" ] || [ "$(cat "$SEED" 2>/dev/null)" != "$want" ]; then
printf '# Netscape HTTP Cookie File\n.myanonamouse.net\tTRUE\t/\tTRUE\t0\tmam_id\t%s\n' \
"$MAM_BONUS_MAM_ID" > "$JAR"
echo "$want" > "$SEED"
chmod 600 "$JAR" "$SEED"
log "cookie jar seeded from MAM_BONUS_MAM_ID"
fi
mam() { curl -sS -m 30 -b "$JAR" -c "$JAR" -A "homelab-mam-bonus/1.0" "$@"; }
# Balance check (also proves the session is alive).
me=$(mam "$BASE/jsonLoad.php") || fail "jsonLoad.php request failed"
bp=$(printf %s "$me" | jq -er '.seedbonus | floor' 2>/dev/null) \
|| fail "session rejected or bad response from jsonLoad.php (mam_id expired/IP-locked?): $(printf %s "$me" | head -c 200)"
spendable=$((bp - RESERVE))
gib=$((spendable / PER_GIB))
log "seedbonus=$bp reserve=$RESERVE -> can buy ${gib} GiB"
if [ "$gib" -lt "$MIN_GIB" ]; then
log "below the ${MIN_GIB} GiB minimum (${MIN_GIB}*${PER_GIB}=$((MIN_GIB * PER_GIB)) BP); nothing to do"
exit 0
fi
if [ "$RESERVE" -eq 0 ]; then amount="Max Affordable "; else amount="$gib"; fi
if [ "${MAM_BONUS_DRY_RUN:-0}" = 1 ]; then
log "DRY RUN: would buy upload amount='$amount'"
exit 0
fi
res=$(mam -G "$BASE/json/bonusBuy.php/" \
--data-urlencode "spendtype=upload" \
--data-urlencode "amount=$amount" \
--data-urlencode "_=$(date +%s)000") || fail "bonusBuy.php request failed"
if [ "$(printf %s "$res" | jq -r '.success // false' 2>/dev/null)" = true ]; then
log "bought upload: $(printf %s "$res" | jq -c 'del(.success)')"
else
fail "bonusBuy rejected (seedbonus was $bp): $(printf %s "$res" | head -c 300)"
fi