Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshtrom <shtrom@1991c358-8f32-0410-a49a-990740bdf4c2>2007-11-03 19:31:42 +0000
committershtrom <shtrom@1991c358-8f32-0410-a49a-990740bdf4c2>2007-11-03 19:31:42 +0000
commit5c6601088980ae6a3d1498b707e9d6889ea12ae8 (patch)
treed74ad71a37376194c96df7e98b6ce6b0da16883d /lastfm.py
parent167c4580f664e3cb5bf8f8e6d6876bb9014728ca (diff)
Setter and getter for LastFM username; updateData now returns a boolean telling
whether a new song has been scrobbled; minor prettification of some text outputs. git-svn-id: svn+ssh://scm.narf.ssji.net/svn/shtrom/scripts@49 1991c358-8f32-0410-a49a-990740bdf4c2
Diffstat (limited to 'lastfm.py')
-rwxr-xr-xlastfm.py38
1 files changed, 35 insertions, 3 deletions
diff --git a/lastfm.py b/lastfm.py
index d2fa7ab..5673a7b 100755
--- a/lastfm.py
+++ b/lastfm.py
@@ -48,20 +48,29 @@ class LastFM:
expected by urllib.urlopen()
"""
- self._username = username
+ self.setUsername(username)
self._proxies = proxies
self.lastSongs = []
+ self.lastScrobblingTime = 0
self.updateData()
def __str__(self):
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."))
+ " Last song was \"%(n)s\" by \"%(a)s\" in album \"%(b)s\"."))
+
+ def getUsername(self):
+ return self.username
+
+ def setUsername(self, username):
+ self._username = username
def updateData(self):
"""
Fetch the last recent tracks list and update the object accordingly.
+ Return True if the last played time has changed, False otherwise.
+
"""
xmldocument = urlopen(self.LASTFM_FORMAT_URL % self._username,
self._proxies)
@@ -73,6 +82,15 @@ class LastFM:
tracklist = recenttracks.getElementsByTagName("track")
+ # return early if nothing more has been scrobbled since last time
+ if len(tracklist) > 1:
+ tempLastScrobblingTime = int(tracklist[0].
+ getElementsByTagName("date")[0].getAttribute("uts"))
+ if tempLastScrobblingTime == self.lastScrobblingTime:
+ return False
+ elif self.lastScrobblingTime == 0:
+ return False
+
self.lastSongs = []
for track in tracklist:
artistNode = track.getElementsByTagName("artist")[0]
@@ -97,6 +115,14 @@ class LastFM:
self.lastSongs.append((artist, name, album,
int(timeNode.getAttribute("uts"))))
+ if len(self.lastSongs) > 0:
+ self.lastScrobblingTime = self.lastSongs[0][LastFM.TIME]
+ else:
+ self.lastScrobblingTime = 0
+
+ return True
+
+
def getLastSong(self):
"""
Return the last played song as a tuple of (ARTIST, SONG, ALBUM, TIME).
@@ -183,7 +209,13 @@ class LastFM:
# Fallback if the script is called directly
if __name__ == "__main__":
from sys import argv
+ from time import sleep
if len(argv) != 2:
raise Exception("Incorrect number of arguments. Only the Last.fm usernam is required.")
- print LastFM(argv[1])
+ lfm = LastFM(argv[1])
+ print lfm
+ while 1:
+ if lfm.updateData():
+ print lfm.getLastRecentSong()
+ sleep(10)