working but only download less than 10 at the time

This commit is contained in:
pierre geeraert
2025-04-03 23:12:48 +01:00
parent f71062147f
commit 731865a102
2 changed files with 25 additions and 36 deletions
+1 -1
View File
@@ -2,6 +2,6 @@
usage :
./reddit_downloader.sh Countem1a
./reddit_downloader.sh USERNAME LOCATION
+22 -33
View File
@@ -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 <reddit-username>"
echo "Usage: $0 <reddit_username> <destination_folder>"
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")
# 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
# 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 each image URL
for IMAGE_URL in $IMAGE_URLS; do
echo "Downloading $IMAGE_URL..."
wget -q -P "$OUTPUT_DIR" "$IMAGE_URL"
# 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 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"