Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshtrom <shtrom@1991c358-8f32-0410-a49a-990740bdf4c2>2007-11-03 13:56:01 +0000
committershtrom <shtrom@1991c358-8f32-0410-a49a-990740bdf4c2>2007-11-03 13:56:01 +0000
commit4d92dc2bb3df8135ff2298c84f48c7f7a1397f70 (patch)
tree5eb029104d12b7e123ad6e6b4084e9d270d74b48 /lastfm.py
parent38f4342deca6c806b7077646b6932855b20e6dcf (diff)
Added formatting function to pretty print song names.
git-svn-id: svn+ssh://scm.narf.ssji.net/svn/shtrom/scripts@47 1991c358-8f32-0410-a49a-990740bdf4c2
Diffstat (limited to 'lastfm.py')
-rwxr-xr-xlastfm.py52
1 files changed, 47 insertions, 5 deletions
diff --git a/lastfm.py b/lastfm.py
index 6230587..3837e9e 100755
--- a/lastfm.py
+++ b/lastfm.py
@@ -1,6 +1,6 @@
#!/bin/env python
"""
-LastFM
+LastFM Python class
Copyright (C) 2007 Olivier Mehani <shtrom@ssji.net>
$Id$
@@ -25,7 +25,7 @@ __version__ = "$Revision$"
from urllib import urlopen
from xml.dom import minidom
-from time import time
+from time import time, strftime
class LastFM:
# Where to fetch the played song information
@@ -54,7 +54,9 @@ class LastFM:
self.updateData()
def __str__(self):
- return "Last.fm song tracker for user %s." % self._username
+ return "Last.fm song tracker for user %s.%s" % (self._username,
+ self.formatSongTitle(
+ " Last song was %(n)s by %(a)s in album %(b)s."))
def updateData(self):
"""
@@ -95,7 +97,8 @@ class LastFM:
album = None
timeNode = track.getElementsByTagName("date")[0]
- self.lastSongs.append((artist, name, album, int(timeNode.getAttribute("uts"))))
+ self.lastSongs.append((artist, name, album,
+ int(timeNode.getAttribute("uts"))))
def getLastSong(self):
"""
@@ -115,6 +118,18 @@ class LastFM:
return None
return self.lastSongs[0][self.TIME]
+ def timeLastScrobbled(self, lst=None):
+ """
+ Return the time in seconds since the last song has been scrobbled.
+
+ lst, the Unix time at which a song has been scrobbled, defaults to that
+ of the last song
+
+ """
+ if lst == None:
+ lst = self.getLastPlayedTime()
+ return int(time()) - lst
+
def lastSongIsRecent(self, delay=None):
"""
Return a boolean stating whether the last song has been played less
@@ -124,7 +139,7 @@ class LastFM:
"""
if delay == None: delay = self.MAX_DELAY
- return int(time()) - self.getLastPlayedTime() < delay
+ return self.timeLastScrobbled() < delay
def getLastRecentSong(self, delay=None):
"""
@@ -141,6 +156,33 @@ class LastFM:
return self.getLastSong()
return None
+ def formatSongTitle(self, formatString="%(a)s - %(n)s", songTuple=None):
+ """
+ Format a song tuple according to a format string. This makes use of the
+ basic Python string formatting operations.
+
+ formatString, the string according to which the song should be formated:
+ "%(a)s" is replaced by the artist;
+ "%(n)s" is replaced by the name of the song;
+ "%(b)s" is replaced by the album;
+ defaults to "%s - %t".
+ songTuple, the tuple representing the song, defaults to the last song
+
+ """
+ str = ""
+ if songTuple == None:
+ songTuple = self.getLastSong()
+
+ if songTuple != None:
+ dict = {
+ "a": songTuple[0],
+ "n": songTuple[1],
+ "b": songTuple[2]
+ }
+ str = formatString % dict
+
+ return str
+
# Fallback if the script is called directly
if __name__ == "__main__":
from sys import argv