Skip to content

Instantly share code, notes, and snippets.

@praseodym
Created May 31, 2013 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save praseodym/5684310 to your computer and use it in GitHub Desktop.
Save praseodym/5684310 to your computer and use it in GitHub Desktop.
Textual Last.fm 'Now playing' script
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# a last.fm now playing script originally written by Brandon Sutton
# some modifications by lifning
# and more modifications by praseodym
import urllib2
import sys
# change me
me = 'praseodymium'
def lastfm_get(username):
print("/debug /lastfm called, it might take a few seconds to fetch the track list...")
xml = urllib2.urlopen("http://ws.audioscrobbler.com/2.0/user/" + username + "/recenttracks.xml?limit=1")
xml = xml.read().replace('&','&')
username = xml.split('" page="')[0].split('<recenttracks user="')[1]
artist = xml.split('</artist>')[0].split('>')[-1]
song = xml.split('</name>')[0].split('<name>')[1]
album = xml.split('</album>')[0].split('>')[-1]
if album != "":
albumtext = " from the album " + album + ""
else:
albumtext = ""
if xml.find("<track nowplaying=\"true\">") == -1:
nowplaying = " last listened"
else:
nowplaying = " is listening"
prefix = "/me" if username == me else username
print(prefix + nowplaying + " to " + song + " by " + artist + albumtext)
username = sys.argv[1] if len(sys.argv) == 2 and sys.argv[1] != '' else me
lastfm_get(username)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment