Skip to content

Instantly share code, notes, and snippets.

@mnuck
Created June 11, 2012 19:53
Show Gist options
  • Save mnuck/2912270 to your computer and use it in GitHub Desktop.
Save mnuck/2912270 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'])
@vincentjames501
Copy link

I'm on osx as well so not sure if this would work:

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 sys
import subprocess
from PIL.Image import Image

base_url = "http://apod.nasa.gov/apod/"
matcher = r'<IMG SRC="([\w\d./-]+).jpg"'
filename = "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)

if "darwin" in sys.platform:
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'])
elif "linux" in sys.platform:
subprocess.call(['gsettings set org.gnome.desktop.background picture-uri %s' % filename])
else:
im = Image.open(filename).convert("RGB")
with open("C:\badwindows.bmp", "wb") as background:
background.write(image)
subprocess.call(['reg add "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "" /f'])
subprocess.call(['reg add "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "C:\badwindows.bmp" /f'])
subprocess.call(['reg delete "hkcu\Software\Microsoft\Internet Explorer\Desktop\General" /v WallpaperStyle /f'])
subprocess.call(['reg add "hkcu\control panel\desktop" /v WallpaperStyle /t REG_SZ /d 2 /f'])
subprocess.call(['RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters'])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment