Pop-Feed (Reddit)


Pop-Feed generiert eine Collage aus dem Subreddit „popular“ und zwar von den Image-Downloads des aktuellen Tages, die letzte Zelle beinhaltet das Datum. Man kann natürlich auch andere Subreddits einstellen, wichtig zu wissen, dass dann alle tages-aktuellen Images in die Collage eingebaut werden. Der Font für das Datum ist hier fest verdrahtet und sollte geändert werden.


#!/bin/bash

URL="https://www.reddit.com/r/popular/.rss"
IMAGE_DIR="./rss-images"

mkdir -p "$IMAGE_DIR"
mkdir -p "assets"

INPUT_DIR="rss-images"
OUTPUT="daily-pop-feed.jpg"

CANVAS_W=1920
CANVAS_H=1080

BACKGROUND="#111111"

# Current date
DATE_TEXT=$(date +"%d")

echo "Downloading RSS feed..."
wget \
  --header="Accept: application/rss+xml,application/xml,text/xml" \
  --user-agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36" \
  --https-only \
  -O ./assets/rss-download \
  "$URL"

echo "Extracting image URLs..."
grep -hoiE 'url="https?://[^"]+"' ./assets/rss-download \
| sed -E 's/^url="//; s/"$//' \
| sed 's/&/\&/g' \
| sort -u > ./assets/images.txt

echo "Found $(wc -l < ./assets/images.txt) unique image URLs"

while read -r url; do
    [ -z "$url" ] && continue

    # Stable filename from full URL
    hash=$(echo -n "$url" | sha1sum | cut -c1-16)

    # Extract extension from URL
    ext=$(echo "$url" | sed -E 's/.*\.([a-zA-Z0-9]+)(\?.*)?$/\1/')
    outfile="$IMAGE_DIR/${hash}.${ext}"
    tmpfile="$IMAGE_DIR/${hash}.tmp"

    # Skip if already downloaded
    if [ -f "$outfile" ]; then
        echo "Skipping existing: $outfile"
        continue
    fi

    echo "Downloading: $url"

    if wget \
        --user-agent="Mozilla/5.0" \
        --tries=3 \
        --timeout=20 \
        "$url" \
        -O "$tmpfile"; then

        if [ -s "$tmpfile" ]; then
            mv "$tmpfile" "$outfile"
            echo "Saved: $outfile"
        else
            rm -f "$tmpfile"
            echo "Empty file skipped"
        fi
    else
        rm -f "$tmpfile"
        echo "Failed: $url"
    fi

done < ./assets/images.txt

# ----------------------------------------
# Collect today's images (macOS compatible)
# ----------------------------------------

TODAY=$(date +%Y-%m-%d)

IMAGES=()

while IFS= read -r file; do

    FILE_DATE=$(date -r "$file" +%Y-%m-%d)

    if [ "$FILE_DATE" = "$TODAY" ]; then
        IMAGES+=("$file")
    fi

done < <(
find "$INPUT_DIR" -type f \( \
    -iname "*.jpg" -o \
    -iname "*.jpeg" -o \
    -iname "*.png" \
\)
)

# ----------------------------------------
# Exit if no images
# ----------------------------------------

if [ ${#IMAGES[@]} -eq 0 ]; then
    echo "No images from today found."
    exit 1
fi

# ----------------------------------------
# Grid calculation
# One slot reserved for date cell
# ----------------------------------------

IMAGE_COUNT=${#IMAGES[@]}
TOTAL_CELLS=$((IMAGE_COUNT + 1))

COLS=$(python3 - <" \
        -background "$BACKGROUND" \
        -gravity "$GRAVITY" \
        -extent "${CELL_W}x${CELL_H}" \
        "$TMP_DIR/$i.jpg"

    ((i++))

done

# ----------------------------------------
# Date tile
# ----------------------------------------

magick \
    -size "${CELL_W}x${CELL_H}" \
    xc:"$BACKGROUND" \
    -gravity center \
    -fill white \
    -font "./assets/technicality.ttf" \
    -pointsize 160 \
    -annotate +0+0 "$DATE_TEXT" \
    "$TMP_DIR/date.jpg"

# ----------------------------------------
# Final montage
# ----------------------------------------

magick montage \
    "$TMP_DIR"/*.jpg \
    -tile ${COLS}x${ROWS} \
    -geometry +4+4 \
    -background "$BACKGROUND" \
    "$OUTPUT"

# ----------------------------------------
# Cleanup
# ----------------------------------------

rm -rf "$TMP_DIR"

echo "Created $OUTPUT"