Skip to content

Instantly share code, notes, and snippets.

@adrianbartnik
Created June 1, 2013 21:36
Show Gist options
  • Save adrianbartnik/5691805 to your computer and use it in GitHub Desktop.
Save adrianbartnik/5691805 to your computer and use it in GitHub Desktop.
A python script that downloads all gifs from reddit.com/r/cinemagraphs in an infinite loop until the api restriction from reddit triggers
import re, mechanize, os, urllib
br = mechanize.Browser()
br.open("http://www.reddit.com/r/Cinemagraphs")
image = urllib.URLopener()
links = set()
i = 0;
dirname = "gifs"
if not os.path.isdir("./" + dirname + "/"):
os.mkdir("./" + dirname + "/")
while True:
for link in (br.links(url_regex=r"gif$")):
links.add(link.absolute_url)
for l in links:
print str(i) + ". " + l
try:
m = re.search(r"[a-zA-Z0-9]*\.gif", l)
if m:
image.retrieve(l, "./" + dirname + "/" + m.group(0))
else:
image.retrieve(l, "./" + dirname + "/" + str(i) + ".gif")
except Exception, e:
print "Error retrieving gif from " + l
i = i + 1
links.clear()
try:
br.follow_link(url_regex=r"count=[0-9]*&after")
except Exception, e:
print "Error retrieving next reddit page due to api restrictions. Try again."
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment