Skip to content

Instantly share code, notes, and snippets.

@esecules
Created September 8, 2016 22:09
Show Gist options
  • Save esecules/4a6e6ab4140f220c0773796985245636 to your computer and use it in GitHub Desktop.
Save esecules/4a6e6ab4140f220c0773796985245636 to your computer and use it in GitHub Desktop.
Download the top 15 images from an imgur subreddit
#! /usr/bin/python
import json
import requests
import os
dest='/home/eric/Pictures/wallpaper/'
subreddit='wallpaper'
baseurl = 'http://imgur.com'
r = requests.get(baseurl + '/r/%s/top.json' % subreddit)
j = json.loads(r.text)
urls = ["%s/%s%s" % (baseurl, img['hash'], img['ext']) for img in j['data'][:15]]
for the_file in os.listdir(dest):
file_path = os.path.join(dest, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print e
for url in urls:
r=requests.get(url)
with open(dest + url.split('/')[-1], 'wb') as f:
f.write(r.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment