mirror of
https://github.com/jlu5/SupyPlugins.git
synced 2025-04-28 22:41:06 -05:00
Fix null album strings, add tests
Thanks to Thomas Coppi for the hint. Related to https://github.com/krf/supybot-lastfm/pull/2
This commit is contained in:
parent
1dbffdf6f6
commit
5e91a27ace
45
plugin.py
45
plugin.py
@ -44,6 +44,32 @@ from time import time
|
|||||||
|
|
||||||
from LastFMDB import *
|
from LastFMDB import *
|
||||||
|
|
||||||
|
class LastFMParser:
|
||||||
|
|
||||||
|
def parseRecentTracks(self, stream):
|
||||||
|
"""
|
||||||
|
@return Tuple with track information of last track
|
||||||
|
"""
|
||||||
|
|
||||||
|
xml = minidom.parse(stream).getElementsByTagName("recenttracks")[0]
|
||||||
|
user = xml.getAttribute("user")
|
||||||
|
|
||||||
|
t = xml.getElementsByTagName("track")[0] # most recent track
|
||||||
|
isNowPlaying = (t.getAttribute("nowplaying") == "true")
|
||||||
|
if not isNowPlaying:
|
||||||
|
time = int(t.getElementsByTagName("date")[0].getAttribute("uts"))
|
||||||
|
else:
|
||||||
|
time = None
|
||||||
|
|
||||||
|
artist = t.getElementsByTagName("artist")[0].firstChild.data
|
||||||
|
track = t.getElementsByTagName("name")[0].firstChild.data
|
||||||
|
try:
|
||||||
|
albumNode = t.getElementsByTagName("album")[0].firstChild
|
||||||
|
album = albumNode.data
|
||||||
|
except (IndexError, AttributeError):
|
||||||
|
album = None
|
||||||
|
return (user, isNowPlaying, artist, track, album, time)
|
||||||
|
|
||||||
class LastFM(callbacks.Plugin):
|
class LastFM(callbacks.Plugin):
|
||||||
# 1.0 API (deprecated)
|
# 1.0 API (deprecated)
|
||||||
APIURL_1_0 = "http://ws.audioscrobbler.com/1.0/user"
|
APIURL_1_0 = "http://ws.audioscrobbler.com/1.0/user"
|
||||||
@ -115,22 +141,13 @@ class LastFM(callbacks.Plugin):
|
|||||||
irc.error("Unknown ID (%s)" % id)
|
irc.error("Unknown ID (%s)" % id)
|
||||||
return
|
return
|
||||||
|
|
||||||
xml = minidom.parse(f).getElementsByTagName("recenttracks")[0]
|
parser = LastFMParser()
|
||||||
user = xml.getAttribute("user")
|
(user, isNowPlaying, artist, track, album, time) = parser.parseRecentTracks(f)
|
||||||
t = xml.getElementsByTagName("track")[0] # most recent track
|
if isNowPlaying:
|
||||||
isNowplaying = (t.getAttribute("nowplaying") == "true")
|
albumStr = "[" + album + "]" if album else ""
|
||||||
artist = t.getElementsByTagName("artist")[0].firstChild.data
|
|
||||||
track = t.getElementsByTagName("name")[0].firstChild.data
|
|
||||||
try:
|
|
||||||
album = "["+t.getElementsByTagName("album")[0].firstChild.data+"]"
|
|
||||||
except IndexError:
|
|
||||||
album = ""
|
|
||||||
|
|
||||||
if isNowplaying:
|
|
||||||
irc.reply(('%s is listening to "%s" by %s %s'
|
irc.reply(('%s is listening to "%s" by %s %s'
|
||||||
% (user, track, artist, album)).encode("utf8"))
|
% (user, track, artist, )).encode("utf8"))
|
||||||
else:
|
else:
|
||||||
time = int(t.getElementsByTagName("date")[0].getAttribute("uts"))
|
|
||||||
irc.reply(('%s listened to "%s" by %s %s more than %s'
|
irc.reply(('%s listened to "%s" by %s %s more than %s'
|
||||||
% (user, track, artist, album,
|
% (user, track, artist, album,
|
||||||
self._formatTimeago(time))).encode("utf-8"))
|
self._formatTimeago(time))).encode("utf-8"))
|
||||||
|
47
test.py
47
test.py
@ -31,6 +31,9 @@
|
|||||||
#from __future__ import print_function
|
#from __future__ import print_function
|
||||||
|
|
||||||
from supybot.test import *
|
from supybot.test import *
|
||||||
|
from plugin import LastFMParser
|
||||||
|
|
||||||
|
from StringIO import StringIO
|
||||||
|
|
||||||
class LastFMTestCase(PluginTestCase):
|
class LastFMTestCase(PluginTestCase):
|
||||||
plugins = ('LastFM',)
|
plugins = ('LastFM',)
|
||||||
@ -53,5 +56,49 @@ class LastFMTestCase(PluginTestCase):
|
|||||||
print self.assertNotError("lastfm compare krf czshadow")
|
print self.assertNotError("lastfm compare krf czshadow")
|
||||||
print self.assertNotError("lastfm compare krf")
|
print self.assertNotError("lastfm compare krf")
|
||||||
|
|
||||||
|
def testLastFMParseRecentTracks(self):
|
||||||
|
"""Parser tests"""
|
||||||
|
|
||||||
|
# noalbum, nowplaying
|
||||||
|
data1 = """<recenttracks user="USER" page="1" perPage="10" totalPages="3019">
|
||||||
|
<track nowplaying="true">
|
||||||
|
<artist mbid="2f9ecbed-27be-40e6-abca-6de49d50299e">ARTIST</artist>
|
||||||
|
<name>TRACK</name>
|
||||||
|
<mbid/>
|
||||||
|
<album mbid=""/>
|
||||||
|
<url>www.last.fm/music/Aretha+Franklin/_/Sisters+Are+Doing+It+For+Themselves</url>
|
||||||
|
<date uts="1213031819">9 Jun 2008, 17:16</date>
|
||||||
|
<streamable>1</streamable>
|
||||||
|
</track>
|
||||||
|
</recenttracks>"""
|
||||||
|
|
||||||
|
# album, not nowplaying
|
||||||
|
data2 = """<recenttracks user="USER" page="1" perPage="10" totalPages="3019">
|
||||||
|
<track nowplaying="false">
|
||||||
|
<artist mbid="2f9ecbed-27be-40e6-abca-6de49d50299e">ARTIST</artist>
|
||||||
|
<name>TRACK</name>
|
||||||
|
<mbid/>
|
||||||
|
<album mbid="">ALBUM</album>
|
||||||
|
<url>www.last.fm/music/Aretha+Franklin/_/Sisters+Are+Doing+It+For+Themselves</url>
|
||||||
|
<date uts="1213031819">9 Jun 2008, 17:16</date>
|
||||||
|
<streamable>1</streamable>
|
||||||
|
</track>
|
||||||
|
</recenttracks>"""
|
||||||
|
|
||||||
|
parser = LastFMParser()
|
||||||
|
(user, isNowPlaying, artist, track, album, time) = \
|
||||||
|
parser.parseRecentTracks(StringIO(data1))
|
||||||
|
self.assertEqual(user, "USER")
|
||||||
|
self.assertEqual(isNowPlaying, True)
|
||||||
|
self.assertEqual(artist, "ARTIST")
|
||||||
|
self.assertEqual(track, "TRACK")
|
||||||
|
self.assertEqual(album, None)
|
||||||
|
self.assertEqual(time, None)
|
||||||
|
|
||||||
|
(user, isNowPlaying, artist, track, album, time) = \
|
||||||
|
parser.parseRecentTracks(StringIO(data2))
|
||||||
|
self.assertEqual(album, "ALBUM")
|
||||||
|
self.assertEqual(time, 1213031819)
|
||||||
|
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user