diff --git a/README.md b/README.md index 9895a11..2832693 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,6 @@ usage : -./reddit_downloader.sh Countem1a +./reddit_downloader.sh USERNAME LOCATION diff --git a/reddit_downloader.sh b/reddit_downloader.sh index bb3fe31..061a0ad 100755 --- a/reddit_downloader.sh +++ b/reddit_downloader.sh @@ -1,47 +1,36 @@ #!/bin/bash -# Check if user has provided a Reddit username +# Check if username is provided if [ -z "$1" ]; then - echo "Usage: $0 " + echo "Usage: $0 " exit 1 fi -USERNAME=$1 -USER_AGENT="reddit-image-downloader" -OUTPUT_DIR="${USERNAME}_images" +# Inputs +USER="$1" +DEST_FOLDER="${2:-./${USER}_images}" -# Create a directory to save images -mkdir -p "$OUTPUT_DIR" +# Make destination folder +mkdir -p "$DEST_FOLDER" -# Reddit API URL to fetch user submissions -REDDIT_API_URL="https://www.reddit.com/user/$USERNAME/submitted.json?limit=100" +# API URL for user submissions (last 100 posts) +REDDIT_URL="https://www.reddit.com/user/$USER/submitted/.json?limit=1000" -# Function to download images -download_images() { - echo "Fetching images from user: $USERNAME" +# 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") - # Loop through pages of user submissions (pagination handled) - while :; do - # Fetch data from Reddit - JSON=$(curl -s -A "$USER_AGENT" "$REDDIT_API_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 - # 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') +# 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 - # 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 +echo "Download completed. Images saved to $DEST_FOLDER"