Created
August 31, 2016 17:34
-
-
Save alex-hofsteede/26b94e7c9f7a23d9f1c615050b6a3beb to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
import datetime | |
import json | |
import os | |
import subprocess | |
import urllib2 | |
""" | |
Sets your mac desktop background to be a recent picture of the earth from space | |
""" | |
path = os.path.join(os.getenv("HOME"), 'nasa_epic') | |
if not os.path.exists(path): | |
os.makedirs(path) | |
yesterday = datetime.datetime.utcnow() - datetime.timedelta(days=1) | |
images = json.loads(urllib2.urlopen('http://epic.gsfc.nasa.gov/api/images.php').read()) | |
if len(images) > 0: | |
image = min(images, key=lambda i: abs(yesterday - datetime.datetime.strptime(i['date'], "%Y-%m-%d %H:%M:%S")))['image'] + ".png" | |
local_image = os.path.join(path, image) | |
if not os.path.exists(local_image): | |
with open(local_image, 'w') as f: | |
print image | |
f.write(urllib2.urlopen('http://epic.gsfc.nasa.gov/epic-archive/png/%s' % image).read()) | |
subprocess.check_call(['osascript', '-e', 'tell application "System Events" to set picture of every desktop to POSIX file "%s"' % local_image]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment