Files
Stash/reddit_downloader.sh
T

37 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
# Check if username is provided
if [ -z "$1" ]; then
echo "Usage: $0 <reddit_username> <destination_folder>"
exit 1
fi
# Inputs
USER="$1"
DEST_FOLDER="${2:-./${USER}_images}"
# Make destination folder
mkdir -p "$DEST_FOLDER"
# API URL for user submissions (last 100 posts)
REDDIT_URL="https://www.reddit.com/user/$USER/submitted/.json?limit=1000"
# Fetch posts with proper User-Agent
echo "Fetching posts from user $USER..."
RESPONSE=$(curl -s -A "Mozilla/5.0 (compatible; RedditImageDownloader/1.0)" "$REDDIT_URL")
# Check if response is valid JSON
if ! echo "$RESPONSE" | jq empty 2>/dev/null; then
echo "Error: Failed to fetch data or parse JSON. Reddit may have blocked the request."
exit 1
fi
# Download image posts
echo "$RESPONSE" | jq -r '.data.children[] | select(.data.url | test("\\.(jpg|jpeg|png|gif)$")) | .data.url' | while read -r image_url; do
FILENAME=$(basename "$image_url")
echo "Downloading $FILENAME..."
curl -s -A "Mozilla/5.0" -o "$DEST_FOLDER/$FILENAME" "$image_url"
done
echo "Download completed. Images saved to $DEST_FOLDER"