Skip to content

Instantly share code, notes, and snippets.

@miku
Forked from mnuck/getAPOD.py
Created August 10, 2012 01:17
Show Gist options
  • Select an option

  • Save miku/3309992 to your computer and use it in GitHub Desktop.

Select an option

Save miku/3309992 to your computer and use it in GitHub Desktop.
Grabs the Astronomy Picture of the Day
#!/usr/bin/env python
# Grab the Astronomical Picture of the Day from NASA's site.
# Make it the desktop background, if it's a jpg
#
# Matthew Nuckolls
import urllib2
import re
import subprocess
base_url = "http://apod.nasa.gov/apod/"
matcher = r'<IMG SRC=\"([\w\d./-]+).jpg"'
filename = "/tmp/background.jpg"
#page = urllib2.urlopen(base_url + "ap120609.html")
page = urllib2.urlopen(base_url + "index.html")
page_data = page.read()
page.close()
m = re.search(matcher,page_data)
if not m:
exit()
image_page = urllib2.urlopen(base_url + m.group(1) + ".jpg")
image = image_page.read()
image_page.close()
with open(filename, "wb") as background:
background.write(image)
subprocess.call(['/usr/bin/osascript',
'-e', 'tell application "Finder"',
'-e', 'set pFile to POSIX file "%s" as string' % filename,
'-e', 'set desktop picture to file pFile',
'-e', 'end tell'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment