Skip to content

Instantly share code, notes, and snippets.

Created August 4, 2015 05:36
Show Gist options
  • Save anonymous/08d2895379b4b26b9e90 to your computer and use it in GitHub Desktop.
Save anonymous/08d2895379b4b26b9e90 to your computer and use it in GitHub Desktop.
Reddit image download script
#!/usr/bin/env bash -e
# These variables can be overridden in the environment
: ${TIMESPAN:=} # hour|day|week|month|year|all
: ${LISTING:=} # hot|new|top
: ${LIMIT:=} # 1-100, default 25
: ${DOWNLOAD_DIR:=~/.reddit-pics}
: ${CURL:=curl -sL}
# "Constants"
REDDIT="https://www.reddit.com/r/$1/$LISTING.json"
IMGUR="https://i.imgur.com"
CURLIT="$CURL -Gd t=$TIMESPAN -d limit=$LIMIT"
SUPPORTED_URLS="(png|jpe?g|gifv?)$"
RED='\033[0;31m'
YELLOW='\033[0;33m'
GREEN='\033[1;32m'
NC='\033[0m'
if [ -z $1 ]; then
echo "Usage: $0 subreddit [index/range]"
exit 0
fi
download() {
local file="$DOWNLOAD_DIR/$1"
echo -n "$file "
if [[ -f "$file" ]]; then
echo -e "(${GREEN}cached${NC})"
else
$CURL "$2" -o "$file" && echo -e "(${GREEN}success${NC})" &&
setfattr $file -n user.xdg.origin.url -v "$2"|| echo -e "(${RED}failure${NC})"
fi
}
# Who needs imgur API... (don't tell anyone)
imgur_get_filetype() {
local response="$($CURL -I $1|grep Content-Type| awk '{ print $2 }'|tr -d '\r')"
if [[ ! $response =~ $SUPPORTED_URLS ]]; then
echo "${YELLOW}Failed to get filetype for $1, trying anyway..${NC}" >&2
echo imgur # FML
else
echo $response|cut -d/ -f2
fi
}
imgur_image_download() {
image=$1
local ext="$(echo $image| cut -s -d. -f2)"
local i="$(echo $image|cut -d. -f1)"
[[ -n "$ext" ]] && ext=".$ext" || ext=".$(imgur_get_filetype $IMGUR/$image.gif)"
download "$i$ext" "$IMGUR/$i$ext"
}
[[ ! -d $DOWNLOAD_DIR ]] && echo "$DOWNLOAD_DIR does not exist, trying to create" >&2 && mkdir $DOWNLOAD_DIR
DATA=$($CURLIT $REDDIT)
RANGE="$2"
for url in $(echo $DATA| jq -r ".data.children[$RANGE].data.url"); do
title=$(echo $DATA| jq "(.data.children[$RANGE].data | select(.url == \"$url\") | .title)")
echo -n "$title -> "
if [[ ! $url =~ "imgur" ]]; then
if [[ $url =~ $SUPPORTED_URLS ]]; then
tail="${url##*/}"
download "${tail%%\?*}" "$url"
else
echo -e "$url (${YELLOW}unsupported format${NC})"
fi
else
path=${url##*imgur.com/}
if [[ ! $url =~ (\/a\/|\/album\/) ]];then
img=$(echo ${path##*gallery/} | cut -d/ -f1|cut -d? -f1)
imgur_image_download $img
else
a=$(echo $path|cut -d/ -f2)
download "$a.zip" "${IMGUR/i./s.}/a/${a}/zip"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment