Skip to content

Instantly share code, notes, and snippets.

@rjkat
Created June 6, 2013 11:07
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 rjkat/5720783 to your computer and use it in GitHub Desktop.
Save rjkat/5720783 to your computer and use it in GitHub Desktop.
# wallpaper.py
# Sets the desktop background to the album art of the current track the user is listening to on lastfm.
import json
import urllib
import urllib2
import subprocess
import re
import time
import cv2
# name of downloaded file
filename = "background.jpg"
# directory where script lives
script_dir = "~/wallpaper/"
# default desktop background
default_background = "/Library/Desktop Pictures/Galaxy.jpg"
# your lastfm username
lastfm_username = "YOUR_USERNAME"
# your lastfm API key
# get one here http://www.last.fm/api/account/create
lastfm_api_key = "YOUR_API_KEY"
# sets background of desktop 2
# see http://stackoverflow.com/questions/431205/how-can-i-programatically-change-the-background-in-mac-os-x
# also http://www.scibuff.com/2009/11/10/applescript-change-the-desktop-picture-wallpaper-of-two-or-more-screens/
SCRIPT = """/usr/bin/osascript<<END
tell application "System Events"
tell desktop 2 to set picture to POSIX file \"%s\"
end tell
END"""
def set_desktop_background(filename):
subprocess.Popen(SCRIPT%filename, shell=True)
def current_track_img_url():
params = {"limit": 1, "user" : lastfm_username}
response = make_lastfm_request("user.getRecentTracks", params)
try:
url = response['recenttracks']['track'][0]['image'][-1]['#text']
except KeyError, e:
url = None
return url
def make_lastfm_request(method, ps):
lastfm = "http://ws.audioscrobbler.com/2.0/"
parameters = {"method": method,
"api_key": lastfm_api_key,
"format": "json"}
parameters.update(ps)
data = urllib.urlencode(parameters)
req = urllib2.Request(lastfm, data)
response = urllib2.urlopen(req)
json_object = json.load(response)
return json_object
currentUrl = ""
x = True
# set to default background initially
set_desktop_background(default_background)
while (1):
url = current_track_img_url()
# if the image couldn't be obtained, display the default background
if url != currentUrl and not url:
set_desktop_background(default_background)
currentUrl = url
# otherwise, show the image for the album cover
elif url != currentUrl and url:
urllib.urlretrieve(url, filename)
# apple only changes the background if the filename has changed
# so alternate between Truebackground.jpg and Falsebackground.jpg
# somewhat hacky
f = str(x) + filename
x = not x
currentUrl = url
try:
img = cv2.imread(filename)
# scale to 800x800 px, chosen arbitrarily
scaled = cv2.resize(img,(800,800))
# blur the scaled image
blurred = cv2.GaussianBlur(scaled,(5,5),0)
cv2.imwrite(f, blurred)
set_desktop_background(script_dir + f)
except cv2.error:
set_desktop_background(default_background)
# sleep for 5s between requests
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment