Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshtrom <shtrom@1991c358-8f32-0410-a49a-990740bdf4c2>2007-11-03 20:46:49 +0000
committershtrom <shtrom@1991c358-8f32-0410-a49a-990740bdf4c2>2007-11-03 20:46:49 +0000
commit157c67ae3c4ae76dc9a67958aa95afd77f6da114 (patch)
tree339c5598c1e3e0c1c68e8d5dff9e49460854e84d /lastfm.py
parent5c6601088980ae6a3d1498b707e9d6889ea12ae8 (diff)
Hopefully better handling of border situations.
git-svn-id: svn+ssh://scm.narf.ssji.net/svn/shtrom/scripts@50 1991c358-8f32-0410-a49a-990740bdf4c2
Diffstat (limited to 'lastfm.py')
-rwxr-xr-xlastfm.py104
1 files changed, 53 insertions, 51 deletions
diff --git a/lastfm.py b/lastfm.py
index 5673a7b..a5fa957 100755
--- a/lastfm.py
+++ b/lastfm.py
@@ -51,7 +51,7 @@ class LastFM:
self.setUsername(username)
self._proxies = proxies
self.lastSongs = []
- self.lastScrobblingTime = 0
+ self.scrobbling = False
self.updateData()
def __str__(self):
@@ -72,9 +72,14 @@ class LastFM:
Return True if the last played time has changed, False otherwise.
"""
- xmldocument = urlopen(self.LASTFM_FORMAT_URL % self._username,
- self._proxies)
- xmltree = minidom.parse(xmldocument)
+ try:
+ xmldocument = urlopen(self.LASTFM_FORMAT_URL % self._username,
+ self._proxies)
+ xmltree = minidom.parse(xmldocument)
+ except:
+ print "Error parsing XML from Last.fm..."
+ return False
+
if xmltree.childNodes.length != 1:
raise Exception("XML document not formed as expected")
@@ -82,46 +87,43 @@ 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]
- 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"))))
-
- if len(self.lastSongs) > 0:
- self.lastScrobblingTime = self.lastSongs[0][LastFM.TIME]
- else:
- self.lastScrobblingTime = 0
-
- return True
-
+ # do not update if nothing more has been scrobbled since last time
+ if len(tracklist) > 0 and \
+ int(tracklist[0].getElementsByTagName("date")[0].
+ getAttribute("uts")) != self.getLastScrobbledTime():
+ 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"))))
+ self.scrobbling = True
+ return True
+
+ # if nothing has been scrobbled for too long, an update to the
+ # "currently" playing song should be made
+ if self.scrobbling and not self.lastSongIsRecent():
+ self.scrobbling = False
+ return True
+
+ return False
def getLastSong(self):
"""
@@ -132,16 +134,16 @@ class LastFM:
return None
return self.lastSongs[0]
- def getLastPlayedTime(self):
+ def getLastScrobbledTime(self):
"""
Return the Unix time the last song was played.
"""
if len(self.lastSongs) < 1:
- return None
+ return 0
return self.lastSongs[0][self.TIME]
- def timeLastScrobbled(self, lst=None):
+ def timeSinceLastScrobbled(self, lst=None):
"""
Return the time in seconds since the last song has been scrobbled.
@@ -150,7 +152,7 @@ class LastFM:
"""
if lst == None:
- lst = self.getLastPlayedTime()
+ lst = self.getLastScrobbledTime()
return int(time()) - lst
def lastSongIsRecent(self, delay=None):
@@ -162,7 +164,7 @@ class LastFM:
"""
if delay == None: delay = self.MAX_DELAY
- return self.timeLastScrobbled() < delay
+ return self.timeSinceLastScrobbled() < delay
def getLastRecentSong(self, delay=None):
"""
@@ -217,5 +219,5 @@ if __name__ == "__main__":
print lfm
while 1:
if lfm.updateData():
- print lfm.getLastRecentSong()
- sleep(10)
+ print lfm.formatSongTitle()
+ sleep(60)