Skip to content

Instantly share code, notes, and snippets.

@brodul
Created April 20, 2015 11:55
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 brodul/5afe6b4d3e3b509e912c to your computer and use it in GitHub Desktop.
Save brodul/5afe6b4d3e3b509e912c to your computer and use it in GitHub Desktop.
Wallpaper script
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