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 : usage :
./reddit_downloader.sh Countem1a ./reddit_downloader.sh USERNAME LOCATION
+24 -35
View File
@@ -1,47 +1,36 @@
#!/bin/bash #!/bin/bash
# Check if user has provided a Reddit username # Check if username is provided
if [ -z "$1" ]; then if [ -z "$1" ]; then
echo "Usage: $0 <reddit-username>" echo "Usage: $0 <reddit_username> <destination_folder>"
exit 1 exit 1
fi fi
USERNAME=$1 # Inputs
USER_AGENT="reddit-image-downloader" USER="$1"
OUTPUT_DIR="${USERNAME}_images" DEST_FOLDER="${2:-./${USER}_images}"
# Create a directory to save images # Make destination folder
mkdir -p "$OUTPUT_DIR" mkdir -p "$DEST_FOLDER"
# Reddit API URL to fetch user submissions # API URL for user submissions (last 100 posts)
REDDIT_API_URL="https://www.reddit.com/user/$USERNAME/submitted.json?limit=100" REDDIT_URL="https://www.reddit.com/user/$USER/submitted/.json?limit=1000"
# Function to download images # Fetch posts with proper User-Agent
download_images() { echo "Fetching posts from user $USER..."
echo "Fetching images from user: $USERNAME" RESPONSE=$(curl -s -A "Mozilla/5.0 (compatible; RedditImageDownloader/1.0)" "$REDDIT_URL")
# Loop through pages of user submissions (pagination handled) # Check if response is valid JSON
while :; do if ! echo "$RESPONSE" | jq empty 2>/dev/null; then
# Fetch data from Reddit echo "Error: Failed to fetch data or parse JSON. Reddit may have blocked the request."
JSON=$(curl -s -A "$USER_AGENT" "$REDDIT_API_URL") exit 1
fi
# Get image URLs from the media_metadata field (looking for 's' key for highest quality image) # Download image posts
IMAGE_URLS=$(echo "$JSON" | jq -r '.data.children[] | select(.data.media_metadata != null) | .data.media_metadata | to_entries[] | .value.s.u') 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 echo "Download completed. Images saved to $DEST_FOLDER"
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