Skip to content

Instantly share code, notes, and snippets.

@notjuliee
Last active March 19, 2017 19:05
Show Gist options
  • Save notjuliee/aa3de32f6e86ded734e3e0b81eb57aaf to your computer and use it in GitHub Desktop.
Save notjuliee/aa3de32f6e86ded734e3e0b81eb57aaf to your computer and use it in GitHub Desktop.
Automagically change wallpaper
sub = "widescreenwallpaper";
fileName = "kwp";
changeCommands = {"gnome":"gsettings set org.gnome.desktop.background picture-uri \"file://$URI\"","darwin":"osascript -e 'tell application \"Finder\" to set desktop picture to POSIX file \"$URI\"'"}
import urllib2;
import json;
import os;
import re;
import platform;
osType = platform.system();
wm = "";
if 'DESKTOP_SESSION' in os.environ:
wm = os.environ['DESKTOP_SESSION'];
def getData(subreddit):
raw = urllib2.urlopen("https://www.reddit.com/r/%s/top.json?t=day&limit=1" % (subreddit)).read();
parsed = json.loads(raw)['data'];
return parsed['children'][0]['data']['preview']['images'][0]['source'];
def downloadImg(url, path):
with open(path,'wb+') as f:
f.write(urllib2.urlopen(url).read());
def changeBackground(path):
cmdType = wm;
if osType == "Darwin":
cmdType = "darwin";
return os.system(changeCommands[cmdType].replace("$URI",path))
def getExtension(url):
match = re.search('\.(.{3,4})\?', url);
if match:
return match.group(1);
return False;
if wm in changeCommands or osType == "Darwin":
data = getData(sub);
imgPath = ("%s/Pictures/%s.%s" % (os.environ['HOME'],fileName,getExtension(data['url'])));
downloadImg(data['url'], imgPath);
changeBackground(imgPath);
else:
print "Your WM (%s) is not supported." % (wm);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment