Skip to content

Instantly share code, notes, and snippets.

@saucecode
Created September 16, 2015 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saucecode/7b23ab136d45a1c957af to your computer and use it in GitHub Desktop.
Save saucecode/7b23ab136d45a1c957af to your computer and use it in GitHub Desktop.
import urllib2, json, os, time
albumurl = raw_input('imgur album url: ')
foldername = albumurl.split('/')[-1].split('#')[0]
if not os.path.exists(foldername+'/'): os.mkdir(foldername+'/')
print 'downloading html...'
albumhtml = urllib2.urlopen(albumurl).read()
jdata = '{"count":'+albumhtml.split("{\"count\":")[1].split('\n')[0][:-1]
data = json.loads(jdata)
print 'going to download',len(data['items']),'images into folder:',foldername
for i in data['items']:
fname = i['hash']+i['ext']
if os.path.exists(foldername+'/'+fname):
print fname,'exists - skipping...'
continue
print 'downloading',fname
while 1:
try:
with open(foldername+'/'+fname,'wb') as f:
f.write(urllib2.urlopen('http://i.imgur.com/'+fname).read())
break
except:
print 'failed to get',fname,'- retrying in 3...'
time.sleep(3)
raw_input('Download complete.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment