working and download all the photos

This commit is contained in:
pierre geeraert
2025-04-03 23:13:19 +01:00
parent 731865a102
commit 1c8391d87e
+47 -18
View File
@@ -6,31 +6,60 @@ if [ -z "$1" ]; then
exit 1 exit 1
fi fi
# Inputs
USER="$1" USER="$1"
DEST_FOLDER="${2:-./${USER}_images}" DEST_FOLDER="${2:-./${USER}_images}"
USER_AGENT="Mozilla/5.0 (compatible; RedditImageDownloader/1.0)"
# Make destination folder
mkdir -p "$DEST_FOLDER" mkdir -p "$DEST_FOLDER"
# API URL for user submissions (last 100 posts) AFTER=""
REDDIT_URL="https://www.reddit.com/user/$USER/submitted/.json?limit=1000" BASE_URL="https://www.reddit.com/user/$USER/submitted/.json"
# Fetch posts with proper User-Agent echo "Fetching all image posts from user $USER..."
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 while true; do
if ! echo "$RESPONSE" | jq empty 2>/dev/null; then # Build URL with pagination
echo "Error: Failed to fetch data or parse JSON. Reddit may have blocked the request." if [ -z "$AFTER" ]; then
exit 1 URL="${BASE_URL}?limit=100"
fi else
URL="${BASE_URL}?limit=100&after=${AFTER}"
fi
# Download image posts # Fetch posts
echo "$RESPONSE" | jq -r '.data.children[] | select(.data.url | test("\\.(jpg|jpeg|png|gif)$")) | .data.url' | while read -r image_url; do RESPONSE=$(curl -s -A "$USER_AGENT" "$URL")
FILENAME=$(basename "$image_url")
echo "Downloading $FILENAME..." # Validate JSON
curl -s -A "Mozilla/5.0" -o "$DEST_FOLDER/$FILENAME" "$image_url" if ! echo "$RESPONSE" | jq empty 2>/dev/null; then
echo "Error: Failed to fetch or parse JSON. Exiting."
break
fi
# Extract image URLs
IMAGE_URLS=$(echo "$RESPONSE" | jq -r '.data.children[] | select(.data.url | test("\\.(jpg|jpeg|png|gif)$")) | .data.url')
# Exit if no more images found
if [ -z "$IMAGE_URLS" ]; then
echo "No more images found on this page."
fi
# Download images
echo "$IMAGE_URLS" | while read -r image_url; do
[ -z "$image_url" ] && continue
FILENAME=$(basename "$image_url")
if [ ! -f "$DEST_FOLDER/$FILENAME" ]; then
echo "Downloading $FILENAME..."
curl -s -A "$USER_AGENT" -o "$DEST_FOLDER/$FILENAME" "$image_url"
else
echo "Skipping existing file $FILENAME"
fi
done
# Get 'after' token for pagination
AFTER=$(echo "$RESPONSE" | jq -r '.data.after')
if [ "$AFTER" == "null" ] || [ -z "$AFTER" ]; then
echo "Reached end of posts."
break
fi
done done
echo "Download completed. Images saved to $DEST_FOLDER" echo "Download complete. Images saved to $DEST_FOLDER"