This commit is contained in:
Amr Hassan 2008-08-19 10:28:55 +00:00
parent 8a2dce339d
commit b1ed3eef5b

View file

@ -100,11 +100,12 @@ def _status2str(lastfm_status):
class ServiceException(Exception): class ServiceException(Exception):
"""Exception related to the Last.fm web service""" """Exception related to the Last.fm web service"""
def __init__(self, lastfm_status): def __init__(self, lastfm_status, details):
self.lastfm_status = lastfm_status self._lastfm_status = lastfm_status
self._details = details
def __str__(self): def __str__(self):
return "Last.fm Service Error: %s" % _status2str(self.lastfm_status) return "%s: %s." %(_status2str(self._lastfm_status), self._details)
class Asynchronizer(threading.Thread): class Asynchronizer(threading.Thread):
"""Hopingly, this class would help perform asynchronous operations less painfully. """Hopingly, this class would help perform asynchronous operations less painfully.
@ -156,6 +157,10 @@ class Exceptionable(object):
return None return None
def _report_error(self, exception): def _report_error(self, exception):
if self.get_raising_exceptions():
raise exception
self.__errors.append(exception) self.__errors.append(exception)
if self._parent: if self._parent:
@ -263,7 +268,8 @@ class Request(Exceptionable):
else: else:
e = doc.getElementsByTagName('error')[0] e = doc.getElementsByTagName('error')[0]
status = e.getAttribute('code') status = e.getAttribute('code')
self._report_error(ServiceException(status)) details = e.firstChild.data.strip()
self._report_error(ServiceException(status, details))
class SessionGenerator(Asynchronizer, Exceptionable): class SessionGenerator(Asynchronizer, Exceptionable):
"""Steps of authorization: """Steps of authorization:
@ -573,7 +579,7 @@ class Album(BaseObject, Cacheable):
uri = 'lastfm://playlist/album/%s' %self._getCachedInfo()['id'] uri = 'lastfm://playlist/album/%s' %self._getCachedInfo()['id']
return Playlist(uri, self.api_key).fetch() return Playlist(uri, *self.auth_data).fetch()
def getURL(self, domain_name = DOMAIN_ENGLISH): def getURL(self, domain_name = DOMAIN_ENGLISH):
"""Returns the url of the album page on Last.fm. """Returns the url of the album page on Last.fm.
@ -687,7 +693,7 @@ class Track(BaseObject, Cacheable):
title = self._extract(track, 'name', 0) title = self._extract(track, 'name', 0)
artist = self._extract(track, 'name', 1) artist = self._extract(track, 'name', 1)
t['images'] = self._extract_all(track, 'image') extra_info['images'] = self._extract_all(track, 'image')
data.append(Track(artist, title, self.api_key, self.secret, self.session_key, extra_info)) data.append(Track(artist, title, self.api_key, self.secret, self.session_key, extra_info))
@ -702,7 +708,12 @@ class Track(BaseObject, Cacheable):
if not doc: if not doc:
return None return None
return self._extract_all(doc, 'name') names = self._extract_all(doc, 'name')
list = []
for name in names:
list.append(Tag(name, *self.auth_data))
return list
def getTopFans(self): def getTopFans(self):
"""Returns the top fans for this track on Last.fm. """ """Returns the top fans for this track on Last.fm. """
@ -730,8 +741,7 @@ class Track(BaseObject, Cacheable):
return None return None
list = [] list = []
tags = doc.getElementsByTagName('tag') names = self._extract_all(doc, 'name')
names = self._extract_all(tags, 'name')
for name in names: for name in names:
list.append(Tag(name, *self.auth_data)) list.append(Tag(name, *self.auth_data))
@ -956,10 +966,10 @@ class Artist(BaseObject, Cacheable):
return artists return artists
def getTags(self): def getTags(self):
"""Get the tags applied by an individual user to an artist""" """Returns a list of the user-set tags to this artist. """
params = self._getParams() params = self._getParams()
doc = Request(self, 'album.getTags', self.api_key, params, True, self.secret).execute() doc = Request(self, 'artist.getTags', self.api_key, params, True, self.secret).execute()
if not doc: if not doc:
return None return None
@ -984,7 +994,7 @@ class Artist(BaseObject, Cacheable):
albums = [] albums = []
for name in names: for name in names:
albums.append(Album(name, *self.auth_data)) albums.append(Album(self.getName(), name, *self.auth_data))
return albums return albums
@ -1243,12 +1253,13 @@ class Event(BaseObject, Cacheable):
def getGeoPoint(self): def getGeoPoint(self):
"""Returns a tuple of latitude and longitude values of where the event is held. """ """Returns a tuple of latitude and longitude values of where the event is held. """
return (self._getCachedInfo()['geo']['lat'], self._getCachedInfo()['geo']['long']) i = self._getCachedInfo()
return (i['venue']['geo']['lat'], i['venue']['geo']['long'])
def getTimeZone(self): def getTimeZone(self):
"""Returns the timezone of where the event is held. """ """Returns the timezone of where the event is held. """
return self._getCachedInfo()['time_zone'] return self._getCachedInfo()['venue']['time_zone']
def getDescription(self): def getDescription(self):
"""Returns the description of the event. """ """Returns the description of the event. """
@ -1313,7 +1324,7 @@ class Event(BaseObject, Cacheable):
sa += ' and ' sa += ' and '
sa += artists[i].getName() sa += artists[i].getName()
return sa + ' at ' + self.getVenueName() return "%(title)s: %(artists)s at %(place)s" %{'title': self.getTitle(), 'artists': sa, 'place': self.getVenueName()}
class Country(BaseObject): class Country(BaseObject):
@ -1323,7 +1334,7 @@ class Country(BaseObject):
self._country_name = country_name self._country_name = country_name
def _getParams(self): def _getParams(self):
return {'country': self.country_name} return {'country': self._country_name}
def getName(self): def getName(self):
"""Returns the country name. """ """Returns the country name. """
@ -1348,9 +1359,14 @@ class Country(BaseObject):
return artists return artists
def getTopTracks(self, location = None): def getTopTracks(self, location = None):
"""Returns a tuple of the most popular Tracks in the country, ordered by popularity. """ """Returns a tuple of the most popular Tracks in the country, ordered by popularity.
* location: A metro name, to fetch the charts for (must be within the country specified).
"""
params = self._getParams() params = self._getParams()
if location:
params['location'] = location
doc = Request(self, 'geo.getTopTracks', self.api_key, params).execute() doc = Request(self, 'geo.getTopTracks', self.api_key, params).execute()
list = [] list = []
@ -1364,7 +1380,7 @@ class Country(BaseObject):
info['play_count'] = self._extract(n, 'playcount') info['play_count'] = self._extract(n, 'playcount')
info['images'] = self._extract_all(n, 'image') info['images'] = self._extract_all(n, 'image')
list.append(Track(artist, title, *self.auth_data)) list.append(Track(artist, title, self.api_key, self.secret, self.session_key, info))
return list return list
@ -1971,13 +1987,11 @@ class Tag(BaseObject):
* free_tracks_only: Set to True to include only free tracks. * free_tracks_only: Set to True to include only free tracks.
""" """
uri = 'lastfm://playlist/tag/%s' %self.tag uri = 'lastfm://playlist/tag/%s' %self.getName()
if free_tracks_only: if free_tracks_only:
uri += '/freetracks' uri += '/freetracks'
print uri return Playlist(uri, *self.auth_data).fetch()
return Playlist(uri, self.api_key).fetch()
def getURL(self, domain_name = DOMAIN_ENGLISH): def getURL(self, domain_name = DOMAIN_ENGLISH):
"""Returns the url of the tag page on Last.fm. """Returns the url of the tag page on Last.fm.