videogen/deploy/megaphone/distribute-postiz.sh
caelum0x d56639a594
Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

164 lines
7.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Megaphone -> Postiz distribution (box 1, chained onto run-weekly.sh / the
# menivor-megaphone.timer). Megaphone (run-weekly.sh) renders the videos and
# publishes media.menivor.com/megaphone/manifest.json with a UTM-tagged
# landing_url baked into every entry. This script is the missing other half:
# it watches that manifest and creates a real post on Postiz for every video
# this box hasn't posted yet, through Postiz's own public API
# (POST /public/v1/upload-from-url + POST /public/v1/posts) - the same API
# a human would use from the Postiz UI's "Agents" tab, just automated.
#
# Why this exists: an audit on 2026-10-24 found Railyard Insights recorded
# ZERO utm_source=menivor visits ever, because the plan always assumed a
# human would post Megaphone videos daily from a logged-in TikTok/IG/YouTube
# account - that step never happened once. Postiz (already deployed on this
# box, container `postiz`, 127.0.0.1:4007) can do the actual posting once ONE
# social channel is connected. See plans/menivor.md's Megaphone section for
# exactly which single owner action that is (Telegram bot token is the
# smallest - no OAuth app, no public callback URL needed).
#
# Safe by design when nothing is connected yet: with POSTIZ_API_KEY and/or
# POSTIZ_INTEGRATION_IDS unset, this logs a clear one-line status and exits 0
# every run - it never fails the parent Megaphone pipeline and never spends
# API calls it can't use.
set -euo pipefail
POSTIZ_ENV_FILE="${POSTIZ_ENV_FILE:-/etc/products/menivor-megaphone-postiz.env}"
if [ -f "$POSTIZ_ENV_FILE" ]; then
set -a
# shellcheck disable=SC1090
source "$POSTIZ_ENV_FILE"
set +a
fi
POSTIZ_BASE_URL="${POSTIZ_BASE_URL:-http://127.0.0.1:4007/api}"
POSTIZ_API_KEY="${POSTIZ_API_KEY:-}"
# Comma-separated Postiz integration ids to post to, e.g. the Telegram
# channel's integration id once connected via the Postiz UI/API. Settings >
# Agents in Postiz (or `GET /public/v1/integrations`) lists the real ids.
POSTIZ_INTEGRATION_IDS="${POSTIZ_INTEGRATION_IDS:-}"
MANIFEST_URL="${MEGAPHONE_MANIFEST_URL:-https://media.menivor.com/megaphone/manifest.json}"
LOG_DIR="${MEGAPHONE_LOG_DIR:-/srv/menivor/megaphone}"
STATE_FILE="${POSTIZ_STATE_FILE:-$LOG_DIR/postiz-posted.json}"
MAX_PER_RUN="${POSTIZ_MAX_PER_RUN:-5}"
HOST_ENV_FILE="${MEGAPHONE_HOST_ENV_FILE:-/etc/products/menivor.env}"
mkdir -p "$LOG_DIR"
[ -f "$STATE_FILE" ] || echo '[]' > "$STATE_FILE"
stamp="$(date -u +%Y%m%dT%H%M%SZ)"
out="$LOG_DIR/postiz-$stamp.json"
ntfy() {
local msg="$1"
local url
url="$(grep -E '^MENIVOR_OWNER_NTFY_URL=' "$HOST_ENV_FILE" 2>/dev/null | head -1 | cut -d= -f2-)"
[ -n "$url" ] && curl -fsS -m 20 -d "$msg" "$url" >/dev/null || true
}
if [ -z "$POSTIZ_API_KEY" ] || [ -z "$POSTIZ_INTEGRATION_IDS" ]; then
msg="megaphone->postiz: not wired up yet (POSTIZ_API_KEY and/or POSTIZ_INTEGRATION_IDS unset in $POSTIZ_ENV_FILE) - connect one channel in Postiz (https://social.menivor.com or http://127.0.0.1:4007 on box) and set both to start auto-posting. See plans/menivor.md Megaphone section."
echo "$msg" >&2
printf '{"status":"not_configured","stamp":"%s"}\n' "$stamp" | tee "$out"
# One alert per run is enough signal without being spammy at weekly cadence.
ntfy "$msg"
exit 0
fi
manifest_json="$(curl -fsS -m 30 "$MANIFEST_URL")" || {
echo "megaphone->postiz: failed to fetch $MANIFEST_URL" >&2
printf '{"status":"manifest_fetch_failed","stamp":"%s"}\n' "$stamp" | tee "$out"
ntfy "megaphone->postiz: failed to fetch $MANIFEST_URL at $stamp"
exit 0
}
posted_ids="$(jq -c '.' "$STATE_FILE")"
candidates="$(jq -c --argjson posted "$posted_ids" --argjson max "$MAX_PER_RUN" \
'[.videos[] | select(([.id] | inside($posted)) | not)] | .[0:$max]' \
<<< "$manifest_json")"
count="$(jq 'length' <<< "$candidates")"
if [ "$count" -eq 0 ]; then
echo "megaphone->postiz: no new videos in manifest (all already posted or none published yet)" >&2
printf '{"status":"nothing_new","stamp":"%s"}\n' "$stamp" | tee "$out"
exit 0
fi
newly_posted="[]"
failures="[]"
IFS=',' read -ra INTEGRATIONS <<< "$POSTIZ_INTEGRATION_IDS"
for i in $(seq 0 $((count - 1))); do
video="$(jq -c ".[$i]" <<< "$candidates")"
vid="$(jq -r '.id' <<< "$video")"
video_url="$(jq -r '.video_url' <<< "$video")"
title="$(jq -r '.title' <<< "$video")"
description="$(jq -r '.description' <<< "$video")"
landing_url="$(jq -r '.landing_url' <<< "$video")"
echo "megaphone->postiz: uploading $vid ($video_url)" >&2
upload_resp="$(curl -sS -m 120 -X POST "$POSTIZ_BASE_URL/public/v1/upload-from-url" \
-H "Authorization: $POSTIZ_API_KEY" \
-H "Content-Type: application/json" \
-d "$(jq -n --arg url "$video_url" '{url: $url}')")" || upload_resp=""
media_id="$(jq -r '.id // empty' <<< "$upload_resp" 2>/dev/null || true)"
media_path="$(jq -r '.path // empty' <<< "$upload_resp" 2>/dev/null || true)"
if [ -z "$media_id" ] || [ -z "$media_path" ]; then
echo "megaphone->postiz: upload FAILED for $vid: $upload_resp" >&2
failures="$(jq -c --arg id "$vid" --arg err "$upload_resp" '. + [{id:$id, stage:"upload", error:$err}]' <<< "$failures")"
continue
fi
content="$(printf '%s - %s %s' "$title" "$description" "$landing_url")"
posts_arr="[]"
for integ in "${INTEGRATIONS[@]}"; do
integ="$(echo "$integ" | xargs)"
[ -z "$integ" ] && continue
entry="$(jq -n \
--arg integ "$integ" \
--arg content "$content" \
--arg media_id "$media_id" \
--arg media_path "$media_path" \
'{integration: {id: $integ}, value: [{content: $content, image: [{id: $media_id, path: $media_path}]}], settings: {}}')"
posts_arr="$(jq -c --argjson e "$entry" '. + [$e]' <<< "$posts_arr")"
done
body="$(jq -n --argjson posts "$posts_arr" \
'{type: "now", date: (now | strftime("%Y-%m-%dT%H:%M:%SZ")), shortLink: false, tags: [], posts: $posts}')"
post_resp="$(curl -sS -m 60 -w '\n%{http_code}' -X POST "$POSTIZ_BASE_URL/public/v1/posts" \
-H "Authorization: $POSTIZ_API_KEY" \
-H "Content-Type: application/json" \
-d "$body")" || post_resp=$'\n000'
http_code="$(tail -n1 <<< "$post_resp")"
post_body="$(sed '$d' <<< "$post_resp")"
if [ "$http_code" = "200" ] || [ "$http_code" = "201" ]; then
echo "megaphone->postiz: posted $vid -> $post_body" >&2
newly_posted="$(jq -c --arg id "$vid" '. + [$id]' <<< "$newly_posted")"
else
echo "megaphone->postiz: post FAILED ($http_code) for $vid: $post_body" >&2
failures="$(jq -c --arg id "$vid" --arg err "$post_body" '. + [{id:$id, stage:"post", error:$err}]' <<< "$failures")"
fi
done
if [ "$(jq 'length' <<< "$newly_posted")" -gt 0 ]; then
jq -c --argjson new "$newly_posted" '. + $new | unique' "$STATE_FILE" > "$STATE_FILE.tmp"
mv "$STATE_FILE.tmp" "$STATE_FILE"
fi
if [ "$(jq 'length' <<< "$failures")" -gt 0 ]; then
ntfy "megaphone->postiz: $(jq 'length' <<< "$failures") post(s) failed at $stamp - see $out on box"
fi
jq -n --argjson posted "$newly_posted" --argjson failed "$failures" --arg stamp "$stamp" \
'{status: "ran", stamp: $stamp, posted: $posted, failed: $failed}' | tee "$out"
find "$LOG_DIR" -name 'postiz-*.json' -mtime +60 -delete