Created
April 20, 2015 11:55
-
-
Save brodul/5afe6b4d3e3b509e912c to your computer and use it in GitHub Desktop.
Wallpaper script
This file contains 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
from urllib import urlretrieve | |
import collections | |
import hashlib | |
import json | |
import os | |
import pickle | |
import urllib2 | |
url = "http://www.reddit.com/r/wallpapers/.json?limit=100" | |
url_cache = '/home/brodul/.wallpaper_cache' | |
wallpaper_dir = '/home/brodul/.wallpapers/' | |
def get(url): | |
"""@todo: Docstring for get | |
""" | |
req = urllib2.Request(url) | |
response = urllib2.urlopen(req) | |
the_page = response.read() | |
return the_page | |
if os.path.isfile(url_cache): | |
with open(url_cache) as f: | |
feched_urls = pickle.load(f) | |
else: | |
feched_urls = collections.deque([], 100) | |
resp = get(url) | |
data = json.loads(resp) | |
for elem in data['data']['children']: | |
url = elem['data']['url'] | |
if url in feched_urls: | |
continue | |
exten = url.split(".")[-1] | |
if exten not in ['jpg', 'jpeg', 'png']: | |
continue | |
hash_ = hashlib.sha224(url).hexdigest().replace('/', '') | |
feched_urls.append(url) | |
print url | |
urlretrieve( | |
url, | |
os.path.join(wallpaper_dir, hash_ + '.' + exten) | |
) | |
with open(url_cache, 'wb') as f: | |
pickle.dump(feched_urls, f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment