Created
September 8, 2016 22:09
-
-
Save esecules/4a6e6ab4140f220c0773796985245636 to your computer and use it in GitHub Desktop.
Download the top 15 images from an imgur subreddit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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