Files
raucao 6750c61d87 Add Nostr photo feeds and wallpaper selection
Read the curated photo feeds from wss://nostr.kosmos.org, verify events, cache originals and thumbnails, and let users pick a wallpaper from the Photos section. Nature is the default feed; City Nights is opt-in. Adds the manual publishing scripts, real event fixtures, protocol documentation, and tests with stubbed network connections.
2026-11-08 21:51:23 +04:00

262 lines
7.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Shared helpers for the Dashtab Nostr photo publishing scripts.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
MANIFEST="${NOSTR_PHOTOS_MANIFEST:-$SCRIPT_DIR/manifest.json}"
WORK_DIR="${NOSTR_PHOTOS_WORK_DIR:-$ROOT/tmp/nostr-photos}"
THUMBS_DIR="$WORK_DIR/thumbs"
EVENTS_DIR="$WORK_DIR/events"
STATE_DIR="$WORK_DIR/state"
LOG_DIR="$WORK_DIR/logs"
DRY_RUN="${DRY_RUN:-0}"
ASSUME_YES="${ASSUME_YES:-0}"
FORCE="${FORCE:-0}"
USER_AGENT="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36"
RELAY="$(jq -r '.relay' "$MANIFEST")"
BLOSSOM="$(jq -r '.blossom' "$MANIFEST")"
CURATOR="$(jq -r '.curator' "$MANIFEST")"
# Interactive key prompts by default. Users may export NOSTR_SECRET_KEY in their
# own shell to let nak read it from the environment instead; the scripts never
# read, store, or print it.
NAK_PROMPT_ARGS=()
if [[ -z "${NOSTR_SECRET_KEY:-}" ]]; then
NAK_PROMPT_ARGS=(--prompt-sec)
fi
die() {
printf 'error: %s\n' "$*" >&2
exit 1
}
warn() {
printf 'warning: %s\n' "$*" >&2
}
info() {
printf '%s\n' "$*"
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "missing required command: $1"
}
ensure_workdirs() {
mkdir -p "$THUMBS_DIR" "$EVENTS_DIR" "$STATE_DIR" "$LOG_DIR"
}
validate_manifest() {
[[ -f "$MANIFEST" ]] || die "manifest not found: $MANIFEST"
jq -e '
(.relay | type == "string" and startswith("wss://")) and
(.blossom | type == "string" and startswith("https://")) and
(.curator | type == "string" and test("^[0-9a-f]{64}$")) and
(.feeds | type == "array" and length > 0) and
(.photos | type == "array" and length > 0) and
([.feeds[].id] | length == (unique | length)) and
([.photos[].file] | length == (unique | length)) and
(all(.feeds[];
(.id | startswith("dt:")) and
(.title | length > 0) and
(.description | type == "string") and
(.tags | type == "array") and
(.order | type == "array" and length > 0)
)) and
(all(.photos[];
(.file | length > 0) and
(.url | startswith("https://")) and
(.feed | startswith("dt:")) and
(.title | length > 0) and
(.alt | length > 0) and
(.content | length > 0) and
(.tags | type == "array") and
(.sha256 | test("^[0-9a-f]{64}$")) and
(.size | type == "number" and . > 0) and
(.dim | test("^[0-9]+x[0-9]+$")) and
(.mime | startswith("image/")) and
(.creator | length > 0) and
(.profile | startswith("https://")) and
(.source | startswith("https://")) and
(.license | length > 0) and
(.licenseUrl | startswith("https://")) and
(.portfolio | type == "string") and
(.verified | type == "object" and (.at | length > 0) and
(.profile.status == 200 and .profile.name == true) and
(.source.status == 200 and .source.name == true) and
(.license.status == 200)) and
(.originId | length > 0)
))
' "$MANIFEST" >/dev/null || die "manifest validation failed: $MANIFEST"
jq -e '([.feeds[].id] | unique) as $feeds | all(.photos[]; (.feed as $f | $feeds | index($f)) != null)' "$MANIFEST" >/dev/null ||
die "a photo references an unknown feed"
jq -e '([.photos[] | {key: .file, value: .feed}] | from_entries) as $photo_feeds | all(.feeds[]; .id as $id | all(.order[]; . as $file | $photo_feeds[$file] == $id))' "$MANIFEST" >/dev/null ||
die "a feed order references a photo that is not assigned to that feed"
jq -e '([.photos[].file] | unique) as $files | all(.feeds[]; . as $feed | ($feed.cover == "") or (($files | index($feed.cover)) != null))' "$MANIFEST" >/dev/null ||
die "a feed cover references an unknown photo"
}
photo_count() {
jq '.photos | length' "$MANIFEST"
}
photo_files() {
jq -r '.photos[].file' "$MANIFEST"
}
feed_ids() {
jq -r '.feeds[].id' "$MANIFEST"
}
photo_field() {
jq -r --arg f "$1" --arg k "$2" '.photos[] | select(.file == $f) | .[$k] // empty' "$MANIFEST"
}
feed_field() {
jq -r --arg f "$1" --arg k "$2" '.feeds[] | select(.id == $f) | .[$k] // empty' "$MANIFEST"
}
feed_slug() {
printf '%s' "${1#dt:}"
}
sha256_file() {
sha256sum "$1" | awk '{print $1}'
}
file_size() {
stat -c '%s' "$1"
}
image_dim() {
magick identify -format '%wx%h' "$1"
}
file_mime() {
file --mime-type -b "$1"
}
is_https() {
[[ "$1" == https://* ]]
}
http_status() {
curl -A "$USER_AGENT" -L -sS -o /dev/null -w '%{http_code}' \
--max-time 60 --retry 2 "$1" 2>/dev/null || true
}
fetch_text() {
curl -A "$USER_AGENT" -L -fsS --max-time 60 --retry 2 "$1"
}
url_contains_sha() {
local url="$1" sha="$2"
[[ "$url" == *"$sha"* ]]
}
confirm() {
if [[ "$ASSUME_YES" == "1" ]]; then
return 0
fi
local reply
read -r -p "$1 [y/N] " reply
[[ "$reply" =~ ^[Yy]$ ]]
}
print_cmd() {
printf ' '
printf '%q ' "$@"
printf '\n'
}
run_cmd() {
if [[ "$DRY_RUN" == "1" ]]; then
print_cmd "$@"
else
"$@"
fi
}
state_init() {
local path="$1"
if [[ ! -f "$path" ]]; then
printf '{}\n' > "$path"
fi
}
event_body() {
jq -S -c '{kind: .kind, content: .content, tags: .tags}' "$1"
}
relay_photo_by_x() {
local sha="$1"
nak req --quiet -k 20 -a "$CURATOR" --tag "x=$sha" -l 1 "$RELAY" < /dev/null 2>/dev/null | head -n 1 || true
}
relay_photo_by_id() {
local id="$1"
nak req --quiet -k 20 -i "$id" -l 1 "$RELAY" < /dev/null 2>/dev/null | head -n 1 || true
}
relay_feed() {
local d="$1"
nak req --quiet -k 30006 -a "$CURATOR" -d "$d" -l 1 "$RELAY" < /dev/null 2>/dev/null | head -n 1 || true
}
parse_common_args() {
for arg in "$@"; do
case "$arg" in
--force) FORCE=1; export FORCE ;;
--yes) ASSUME_YES=1; export ASSUME_YES ;;
--dry-run) DRY_RUN=1; export DRY_RUN ;;
*) die "unknown option: $arg" ;;
esac
done
}
require_nak() {
require_cmd nak
command -v nak >/dev/null 2>&1 || die "nak is required"
}
require_publish_tools() {
require_cmd jq
require_cmd curl
require_cmd sha256sum
require_cmd stat
require_cmd file
require_nak
}
summary() {
local thumbs_state="$STATE_DIR/thumbs.json"
local photos_state="$STATE_DIR/photos.json"
local feeds_state="$STATE_DIR/feeds.json"
local total_photos uploaded_photos published_photos published_feeds
total_photos=$(photo_count)
uploaded_photos=0
published_photos=0
published_feeds=0
if [[ -f "$thumbs_state" ]]; then
uploaded_photos=$(jq '[to_entries[] | select((.value.url // "") != "")] | length' "$thumbs_state")
fi
if [[ -f "$photos_state" ]]; then
published_photos=$(jq '[to_entries[] | select((.value.id // "") != "")] | length' "$photos_state")
fi
if [[ -f "$feeds_state" ]]; then
published_feeds=$(jq '[to_entries[] | select((.value.id // "") != "")] | length' "$feeds_state")
fi
info "Manifest: $MANIFEST"
info "Relay: $RELAY"
info "Blossom: $BLOSSOM"
info "Curator: $CURATOR"
info "Photos: $published_photos/$total_photos published, $uploaded_photos/$total_photos thumbnails uploaded"
info "Feeds: $published_feeds/$(jq '.feeds | length' "$MANIFEST") published"
info "Work dir: $WORK_DIR"
}