From 8b9c525182e7bc6ce57644da42a9089bfc1dd12d Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Fri, 25 Jul 2008 14:54:34 +0000 Subject: [PATCH] --- src/api.py | 5 +- src/artist.py | 6 +-- src/stats.py | 14 ++++++ src/tag.py | 124 +++++++++++++++++++++++++++++++++++++++++++++++--- src/track.py | 4 +- 5 files changed, 140 insertions(+), 13 deletions(-) diff --git a/src/api.py b/src/api.py index 575ac04..ecf66e6 100644 --- a/src/api.py +++ b/src/api.py @@ -246,7 +246,10 @@ class Api(object): params.update({'api_key': self.__apiKey}) xml = self._fetchUrl(Api.API_ROOT_URL, params, no_cache = self._no_cache) #print xml - data = ElementTree.XML(xml) + try: + data = ElementTree.XML(xml) + except SyntaxError, e: + raise LastfmError("Error in parsing XML: %s" % e) if data.get('status') != "ok": raise LastfmError("Error code: %s (%s)" % (data.find("error").get('code'), data.findtext('error'))) if parse: diff --git a/src/artist.py b/src/artist.py index f9c3e36..675c435 100644 --- a/src/artist.py +++ b/src/artist.py @@ -65,7 +65,7 @@ class Artist(LastfmBase): self._fillInfo() return self.__image - def getStreamable(self): + def isStreamable(self): if self.__streamable is None: self._fillInfo() return self.__streamable @@ -89,7 +89,7 @@ class Artist(LastfmBase): name = a.findtext('name'), mbid = a.findtext('mbid'), stats = Stats( - artist = a.findtext('name'), + subject = a.findtext('name'), match = float(a.findtext('match')), ), url = 'http://' + a.findtext('url'), @@ -129,7 +129,7 @@ class Artist(LastfmBase): image = property(getImage, None, None, "Image's Docstring") - streamable = property(getStreamable, None, None, "Streamable's Docstring") + streamable = property(isStreamable, None, None, "Streamable's Docstring") stats = property(getStats, None, None, "Stats's Docstring") diff --git a/src/stats.py b/src/stats.py index ba662f9..6c3b843 100644 --- a/src/stats.py +++ b/src/stats.py @@ -10,6 +10,8 @@ class Stats(object): subject, listeners = None, playcount = None, + tagcount = None, + count = None, match = None, rank = None, weight = None, @@ -18,6 +20,8 @@ class Stats(object): self.__subject = subject self.__listeners = listeners self.__playcount = playcount + self.__tagcount = tagcount + self.__count = count self.__match = match self.__rank = rank self.__weight = weight @@ -35,7 +39,13 @@ class Stats(object): def getPlaycount(self): return self.__playcount + + def getTagcount(self): + return self.__tagcount + def getCount(self): + return self.__count + def getMatch(self): return self.__match @@ -51,6 +61,10 @@ class Stats(object): listeners = property(getListeners, None, None, "Listeners's Docstring") playcount = property(getPlaycount, None, None, "Plays's Docstring") + + tagcount = property(getTagcount, None, None, "Plays's Docstring") + + count = property(getCount, None, None, "Plays's Docstring") match = property(getMatch, None, None, "Match's Docstring") diff --git a/src/tag.py b/src/tag.py index 9e1fabb..236a316 100644 --- a/src/tag.py +++ b/src/tag.py @@ -11,12 +11,18 @@ class Tag(LastfmBase): def init(self, api, name = None, - url = None): + url = None, + streamable = None): if not isinstance(api, Api): raise LastfmError("api reference must be supplied as an argument") self.__api = api self.__name = name self.__url = url + self.__streamable = streamable + self.__similar = None + self.__topAlbums = None + self.__topArtists = None + self.__topTracks = None def getName(self): return self.__name @@ -24,21 +30,124 @@ class Tag(LastfmBase): def getUrl(self): return self.__url + def isStreamable(self): + return self.__streamable + name = property(getName, None, None, "Name's Docstring") - url = property(getUrl, None, None, "Url's Docstring") + url = property(getUrl, None, None, "Url's Docstring") + + streamable = property(isStreamable, None, None, "Docstring") def getSimilar(self): - pass + if self.__similar is None: + params = {'method': 'tag.getsimilar', 'tag': self.name} + data = self.__api._fetchData(params).find('similartags') + self.__similar = [ + Tag( + self.__api, + name = t.findtext('name'), + url = t.findtext('url'), + streamable = (t.findtext('streamable') == "1"), + ) + for t in data.findall('tag') + ] + return self.__similar + + similar = property(getSimilar, None, None, "Docstring") def getTopAlbums(self): - pass + if self.__topAlbums is None: + params = {'method': 'tag.gettopalbums', 'tag': self.name} + data = self.__api._fetchData(params).find('topalbums') + self.__topAlbums = [ + Album( + self.__api, + name = a.findtext('name'), + artist = Artist( + self.__api, + name = a.findtext('artist/name'), + mbid = a.findtext('artist/mbid'), + url = a.findtext('artist/url'), + ), + mbid = a.findtext('mbid'), + url = a.findtext('url'), + image = dict([(i.get('size'), i.text) for i in a.findall('image')]), + stats = Stats( + subject = a.findtext('name'), + tagcount = a.findtext('tagcount') and int(a.findtext('tagcount')) or None, + rank = a.attrib['rank'].strip() and int(a.attrib['rank']) or None + ) + ) + for a in data.findall('album') + ] + return self.__topAlbums + + topAlbums = property(getTopAlbums, None, None, "Docstring") + topAlbum = property( + lambda self: len(self.topAlbums) and self.topAlbums[0] or None, + None, None, "Docstring" + ) def getTopArtists(self): - pass + if self.__topArtists is None: + params = {'method': 'tag.gettopartists', 'tag': self.name} + data = self.__api._fetchData(params).find('topartists') + self.__topArtists = [ + Artist( + self.__api, + name = a.findtext('name'), + mbid = a.findtext('mbid'), + stats = Stats( + subject = a.findtext('name'), + rank = a.attrib['rank'].strip() and int(a.attrib['rank']) or None, + tagcount = a.findtext('tagcount') and int(a.findtext('tagcount')) or None + ), + url = a.findtext('url'), + streamable = (a.findtext('streamable') == "1"), + image = dict([(i.get('size'), i.text) for i in a.findall('image')]), + ) + for a in data.findall('artist') + ] + return self.__topArtists + + topArtists = property(getTopArtists, None, None, "Docstring") + topArtist = property( + lambda self: len(self.topArtists) and self.topArtists[0] or None, + None, None, "Docstring" + ) def getTopTracks(self): - pass + if self.__topTracks is None: + params = {'method': 'tag.gettoptracks', 'tag': self.name} + data = self.__api._fetchData(params).find('toptracks') + self.__topTracks = [ + Track( + self.__api, + name = t.findtext('name'), + artist = Artist( + self.__api, + name = t.findtext('artist/name'), + mbid = t.findtext('artist/mbid'), + url = t.findtext('artist/url'), + ), + mbid = t.findtext('mbid'), + stats = Stats( + subject = t.findtext('name'), + rank = t.attrib['rank'].strip() and int(t.attrib['rank']) or None, + tagcount = t.findtext('tagcount') and int(t.findtext('tagcount')) or None + ), + streamable = (t.findtext('streamable') == '1'), + fullTrack = (t.find('streamable').attrib['fulltrack'] == '1'), + image = dict([(i.get('size'), i.text) for i in t.findall('image')]), + ) + for t in data.findall('track') + ] + return self.__topTracks + + topTracks = property(getTopTracks, None, None, "Docstring") + topTrack = property(lambda self: len(self.topTracks) and self.topTracks[0], + None, None, "docstring") @staticmethod def getTopTags(api): @@ -74,4 +183,5 @@ from api import Api from error import LastfmError from album import Album from artist import Artist -from track import Track \ No newline at end of file +from track import Track +from stats import Stats \ No newline at end of file diff --git a/src/track.py b/src/track.py index d6a58a1..2547f19 100644 --- a/src/track.py +++ b/src/track.py @@ -44,7 +44,7 @@ class Track(LastfmBase): def getUrl(self): return self.__url - def getStreamable(self): + def isStreamable(self): return self.__streamable def getArtist(self): @@ -65,7 +65,7 @@ class Track(LastfmBase): url = property(getUrl, None, None, "Url's Docstring") - streamable = property(getStreamable, None, None, "Streamable's Docstring") + streamable = property(isStreamable, None, None, "Streamable's Docstring") artist = property(getArtist, None, None, "Artist's Docstring")