From ca257000c84883a2171222a686d6e85c5704b111 Mon Sep 17 00:00:00 2001 From: ginnoir Date: Sat, 21 Nov 2026 17:20:02 -0600 Subject: [PATCH] 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 --- stacks/media/docker-compose.yml | 28 ++++++++++ stacks/media/mam-bonus/buy-upload.sh | 77 ++++++++++++++++++++++++++++ stacks/media/stack.env | 6 +++ 3 files changed, 111 insertions(+) create mode 100644 stacks/media/mam-bonus/buy-upload.sh diff --git a/stacks/media/docker-compose.yml b/stacks/media/docker-compose.yml index 212fb60..3ce1228 100644 --- a/stacks/media/docker-compose.yml +++ b/stacks/media/docker-compose.yml @@ -336,6 +336,34 @@ services: ports: - "6161:6161" + mam-bonus: + container_name: mam_bonus + # Once a day, spend MAM bonus points on upload credit (json/bonusBuy.php, + # spendtype=upload, "Max Affordable ") so seedbonus never caps out. Uses its + # own dedicated MAM session (MAM_BONUS_MAM_ID); the rotating cookie is kept + # in /config/mam-bonus. Failures go to ntfy topic `mam`. Script: ./mam-bonus. + image: alpine:3.20 + restart: unless-stopped + networks: [edge] # egress + ntfy:80 for failure alerts + env_file: + - stack.env + volumes: + - ./mam-bonus:/app:ro + - /config/mam-bonus:/data + entrypoint: ["/bin/sh", "-c"] + command: + - | + apk add --no-cache curl jq tzdata >/dev/null + # crond jobs get a bare env, so hand the job its vars via a file + printenv | grep -E '^(MAM_BONUS_|TZ=)' | sed "s/^\([^=]*\)=\(.*\)/export \1='\2'/" > /etc/mam-bonus.env + chmod 600 /etc/mam-bonus.env + # busybox crond silently skips a crontab not owned by root + echo '17 4 * * * . /etc/mam-bonus.env && /bin/sh /app/buy-upload.sh >> /proc/1/fd/1 2>&1' > /etc/crontabs/root + chown root:root /etc/crontabs/root && chmod 600 /etc/crontabs/root + exec crond -f -l 8 -c /etc/crontabs + labels: + - "com.centurylabs.watchtower.enable=false" + networks: media: name: media diff --git a/stacks/media/mam-bonus/buy-upload.sh b/stacks/media/mam-bonus/buy-upload.sh new file mode 100644 index 0000000..53b1a5f --- /dev/null +++ b/stacks/media/mam-bonus/buy-upload.sh @@ -0,0 +1,77 @@ +#!/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 diff --git a/stacks/media/stack.env b/stacks/media/stack.env index 040e153..d5fcd45 100644 --- a/stacks/media/stack.env +++ b/stacks/media/stack.env @@ -39,3 +39,9 @@ AUTOBRR__OIDC_DISABLE_BUILT_IN_LOGIN=true # autobrr API key for scripted ops (row "homelab-ops" in api_key table) AUTOBRR_API_KEY=3076c984c78d9ef477228379faea6b04 + +# mam-bonus: dedicated MAM session (bare mam_id) for the daily upload-credit buy. +# Seeds /config/mam-bonus/cookies.txt; MAM rotates it and the job re-saves it there. +MAM_BONUS_MAM_ID=JbRgCtBtbsFjCbXsDOUhtMpuUO-oe2dc1i1wT8-8OZRJ0dmBspWkG2SmK5euqhgX-XbtncjiGHWHBYMRS_DSvO4DXsdH--OHZAcZmUgcDuPBaDAcSMEmKSkpvPayUazed0YU3ncV6om380HXPnBgaAvTvocM5lcPruIHWgcji4771TuLriZ7D5xPtqjkO9YdsjOh2A_ckhQQLhFPmejFjgx9mJJ6fc4Xw3ctFwO2CY8EDcH6u3ViF7jD73Qpr8gWnfwGokuHVpKSyJDUzUWPkj7NbldwUhdS-C4l +MAM_BONUS_RESERVE=0 +MAM_BONUS_NTFY=http://ntfy:80/mam