Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshtrom <shtrom@1991c358-8f32-0410-a49a-990740bdf4c2>2007-11-03 13:10:15 +0000
committershtrom <shtrom@1991c358-8f32-0410-a49a-990740bdf4c2>2007-11-03 13:10:15 +0000
commit55c9a5dd98a62a5cb182e38539d45f513cb8501d (patch)
treed864de1a4fd1d35a49d70efeb2d75160a108029f /lastfm.py
Last.fm Python handy script.
git-svn-id: svn+ssh://scm.narf.ssji.net/svn/shtrom/scripts@41 1991c358-8f32-0410-a49a-990740bdf4c2
Diffstat (limited to 'lastfm.py')
-rw-r--r--lastfm.py143
1 files changed, 143 insertions, 0 deletions
diff --git a/lastfm.py b/lastfm.py
new file mode 100644
index 0000000..11c28a3
--- /dev/null
+++ b/lastfm.py
@@ -0,0 +1,143 @@
+#!/bin/env python
+"""
+LastFM
+Copyright (C) 2007 Olivier Mehani <shtrom@ssji.net>
+
+$Id$
+Python class to handily retrieve song information from a Last.fm account.
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+"""
+
+__version__ = "$Revision$"
+# $Source$
+
+from urllib import urlopen
+from xml.dom import minidom
+from time import time
+
+class LastFM:
+ # Where to fetch the played song information
+ LASTFM_FORMAT_URL = \
+ "http://ws.audioscrobbler.com/1.0/user/%s/recenttracks.xml"
+ # Delay in seconds after which the last song entry is considered too old tox # be displayed.
+ MAX_DELAY = 600
+
+ ARTIST = 0
+ NAME = 1
+ ALBUM = 2
+ TIME = 3
+
+ def __init__(self, username, proxies=None):
+ """
+ Create a new LastFM object.
+
+ username, the Last.fm username
+ proxies, the list of proxies to use to connect to the Last.fm data, as
+ expected by urllib.urlopen()
+
+ """
+ self._username = username
+ self._proxies = proxies
+ self.lastSongs = []
+ self.updateData()
+
+ def __str__(self):
+ return "Last.fm song tracker for user %s." % self._username
+
+ def updateData(self):
+ """
+ Fetch the last recent tracks list and update the object accordingly.
+
+ """
+ xmldocument = urlopen(self.LASTFM_FORMAT_URL % self._username,
+ self._proxies)
+ xmltree = minidom.parse(xmldocument)
+ if xmltree.childNodes.length != 1:
+ raise Exception("XML document not formed as expected")
+
+ recenttracks = xmltree.childNodes[0]
+
+ if recenttracks.attributes.getNamedItem("user").value != self._username:
+ raise Exception("Usernames from request and XML mismatch.")
+
+ tracklist = recenttracks.getElementsByTagName("track")
+
+ self.lastSongs = []
+ for track in tracklist:
+ artistNode = track.getElementsByTagName("artist")[0]
+ if artistNode.firstChild:
+ artist = artistNode.firstChild.data
+ else:
+ artist = None
+
+ nameNode = track.getElementsByTagName("name")[0]
+ if nameNode.firstChild:
+ name = nameNode.firstChild.data
+ else:
+ name = None
+
+ albumNode = track.getElementsByTagName("album")[0]
+ if albumNode.firstChild:
+ album = albumNode.firstChild.data
+ else:
+ album = None
+
+ timeNode = track.getElementsByTagName("date")[0]
+ self.lastSongs.append((artist, name, album, int(timeNode.getAttribute("uts"))))
+
+ def getLastSong(self):
+ """
+ Return the last played song as a tuple of (ARTIST, SONG, ALBUM, TIME).
+
+ """
+ if len(self.lastSongs) < 1:
+ return None
+ return self.lastSongs[0]
+
+ def getLastPlayedTime(self):
+ """
+ Return the Unix time the last song was played.
+
+ """
+ if len(self.lastSongs) < 1:
+ return None
+ return self.lastSongs[0][self.TIME]
+
+ def lastSongIsRecent(self, delay=None):
+ """
+ Return a boolean stating whether the last song has been played less
+ the specified delay earlier.
+
+ delay, the delay to use, defaults to self.MAX_DELAY
+
+ """
+ if delay == None: delay = self.MAX_DELAY
+ return int(time()) - self.getLastPlayedTime() < delay
+
+ def getLastRecentSong(self, delay=None):
+ """
+ Return the last *recently* played song.
+
+ "Recently" means that the song has been played less than delay
+ earlier.
+
+ delay, the delay to use, see lastSongIsRecent for the semantics
+
+ """
+ self.updateData()
+ if self.lastSongIsRecent(delay):
+ return self.getLastSong()
+ return None