#!/bin/bash # Check if user has provided a Reddit username if [ -z "$1" ]; then echo "Usage: $0 " exit 1 fi USERNAME=$1 USER_AGENT="reddit-image-downloader" OUTPUT_DIR="${USERNAME}_images" # Create a directory to save images mkdir -p "$OUTPUT_DIR" # Reddit API URL to fetch user submissions REDDIT_API_URL="https://www.reddit.com/user/$USERNAME/submitted.json?limit=100" # Function to download images download_images() { echo "Fetching images from user: $USERNAME" # Loop through pages of user submissions (pagination handled) while :; do # Fetch data from Reddit JSON=$(curl -s -A "$USER_AGENT" "$REDDIT_API_URL") # Get image URLs from the media_metadata field (looking for 's' key for highest quality image) IMAGE_URLS=$(echo "$JSON" | jq -r '.data.children[] | select(.data.media_metadata != null) | .data.media_metadata | to_entries[] | .value.s.u') # Check if we found any image URLs if [ -z "$IMAGE_URLS" ]; then echo "No more images found." break fi # Download each image URL for IMAGE_URL in $IMAGE_URLS; do echo "Downloading $IMAGE_URL..." wget -q -P "$OUTPUT_DIR" "$IMAGE_URL" done # Check if there's a next page of submissions NEXT_PAGE=$(echo "$JSON" | jq -r '.data.after') # if [ "$NEXT_PAGE" != "null" ]; then # REDDIT_API_URL="https://www.reddit.com/user/$USERNAME/submitted.json?limit= done