r/ScriptSwap • • Aug 23 '12

[Bash] Download images from subreddit

Usage: In case you named this bash file as rdt and you want to download e.g. subreddit GirlswithGlasses, then:

/bin/bash rdt GirlswithGlasses

Or you can make it executable and run it as ./rdt, you know this guys.

On line 17 you can can change allowed file types, gif is pretty obvious if you want to download some gif subreddit

#!/bin/bash

#cfg
output="down"
useragent="Love by u/gadelat"

subreddit=$1
url="https://pay.reddit.com/r/$subreddit/.json"
content=`wget -U "$useragent" --no-check-certificate -q -O - $url`
mkdir -p $output
while : ; do
	urls=$(echo -e "$content"|grep -Po '"url":.*?[^\\]",'|cut -f 4 -d '"')
	names=$(echo -e "$content"|grep -Po '"title":.*?[^\\]",'|cut -f 4 -d '"')
	ids=$(echo -e "$content"|grep -Po '"id":.*?[^\\]",'|cut -f 4 -d '"')
	a=1
	for url in $(echo -e "$urls"); do
		if [ -n  "`echo "$url"|egrep \".gif|.jpg\"`" ]; then
			#echo -n "$url: "
			name=`echo -e "$names"|sed -n "$a"p`
			id=`echo -e "$ids"|sed -n "$a"p`
			echo $name
			newname="$name"_"$subreddit"_$id.${url##*.}
			wget -U "$useragent" --no-check-certificate -nv -nc -P down -O "$output/$newname" $url
		fi
		a=$(($a+1))
	done
	after=$(echo -e "$content"|grep -Po '"after":.*?[^\\]",'|cut -f 4 -d '"'|tail -n 1)
	if [ -z $after ]; then
		break
	fi
	url="https://www.reddit.com/r/$subreddit/.json?count=200&after=$after"
	content=`wget -U "$useragent" --no-check-certificate -q -O - $url`
	#echo -e "$urls"
done
19 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/fritigerngothly Feb 11 '13

Found out by accident that Reddit has changed their JSON stuffs from https to http. So change line 8 from url="https://www.reddit.com/r/$subreddit/.json" to url="http://www.reddit.com/r/$subreddit/.json" and everything works again!