Package lastfm :: Module album
[hide private]
[frames] | no frames]

Source Code for Module lastfm.album

  1  #!/usr/bin/env python 
  2  """Module for calling Album related last.fm web services API methods""" 
  3   
  4  __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>" 
  5  __version__ = "0.2" 
  6  __license__ = "GNU Lesser General Public License" 
  7   
  8  from lastfm.base import LastfmBase 
  9  from lastfm.mixins import Cacheable, Searchable, Taggable 
10 11 -class Album(LastfmBase, Cacheable, Searchable, Taggable):
12 """A class representing an album."""
13 - def init(self, 14 api, 15 name = None, 16 artist = None, 17 id = None, 18 mbid = None, 19 url = None, 20 release_date = None, 21 image = None, 22 stats = None, 23 top_tags = None, 24 streamable = None):
25 """ 26 Create an Album object by providing all the data related to it. 27 28 @param api: an instance of L{Api} 29 @type api: L{Api} 30 @param name: the album name 31 @type name: str 32 @param artist: the album artist name 33 @type artist: L{Artist} 34 @param id: the album ID 35 @type id: str 36 @param mbid: MBID of the album 37 @type mbid: str 38 @param url: URL of the album on last.fm 39 @type url: str 40 @param release_date: release date of the album 41 @type release_date: datetime.datetime 42 @param image: the cover images of the album in various sizes 43 @type image: dict 44 @param stats: the album statistics 45 @type stats: L{Stats} 46 @param top_tags: top tags of the album 47 @type top_tags: list of L{Tag} 48 @param streamable: flag indicating if the album is streamable 49 @type streamable: bool 50 51 @raise InvalidParametersError: If an instance of L{Api} is not provided as the first 52 parameter then an Exception is raised. 53 """ 54 if not isinstance(api, Api): 55 raise InvalidParametersError("api reference must be supplied as an argument") 56 Taggable.init(self, api) 57 self._api = api 58 self._name = name 59 self._artist = artist 60 self._id = id 61 self._mbid = mbid 62 self._url = url 63 self._release_date = release_date 64 self._image = image 65 self._stats = stats and Stats( 66 subject = self, 67 listeners = stats.listeners, 68 playcount = stats.playcount, 69 match = stats.match, 70 rank = stats.rank 71 ) 72 self._top_tags = top_tags 73 self._streamable = streamable
74 75 @property
76 - def name(self):
77 """name of the album""" 78 return self._name
79 80 @property
81 - def artist(self):
82 """artist of the album""" 83 return self._artist
84 85 @property
86 - def id(self):
87 """id of the album""" 88 if self._id is None: 89 self._fill_info() 90 return self._id
91 92 @property
93 - def mbid(self):
94 """mbid of the album""" 95 if self._mbid is None: 96 self._fill_info() 97 return self._mbid
98 99 @property
100 - def url(self):
101 """url of the album's page""" 102 if self._url is None: 103 self._fill_info() 104 return self._url
105 106 @property
107 - def release_date(self):
108 """release date of the album""" 109 if self._release_date is None: 110 self._fill_info() 111 return self._release_date
112 113 @property
114 - def image(self):
115 """cover images of the album""" 116 if self._image is None: 117 self._fill_info() 118 return self._image
119 120 @property
121 - def stats(self):
122 """stats related to the album""" 123 if self._stats is None: 124 self._fill_info() 125 return self._stats
126 127 @property
128 - def streamable(self):
129 """is the album streamable""" 130 return self._streamable
131 132 @LastfmBase.cached_property
133 - def top_tags(self):
134 """top tags for the album""" 135 params = {'method': 'album.getInfo'} 136 if self.artist and self.name: 137 params.update({'artist': self.artist.name, 'album': self.name}) 138 elif self.mbid: 139 params.update({'mbid': self.mbid}) 140 data = self._api._fetch_data(params).find('album') 141 return [ 142 Tag( 143 self._api, 144 subject = self, 145 name = t.findtext('name'), 146 url = t.findtext('url') 147 ) 148 for t in data.findall('toptags/tag') 149 ]
150 151 @LastfmBase.top_property("top_tags")
152 - def top_tag(self):
153 """top tag for the album""" 154 pass
155 156 @LastfmBase.cached_property
157 - def playlist(self):
158 """playlist for the album""" 159 return Playlist.fetch(self._api, "lastfm://playlist/album/%s" % self.id)
160 161 @staticmethod
162 - def get_info(api, 163 artist = None, 164 album = None, 165 mbid = None):
166 """ 167 Get the data for the album. 168 169 @param api: an instance of L{Api} 170 @type api: L{Api} 171 @param artist: the album artist name 172 @type artist: str OR L{Artist} 173 @param album: the album name 174 @type album: str 175 @param mbid: MBID of the album 176 @type mbid: str 177 178 @return: an Album object corresponding the provided album name 179 @rtype: L{Album} 180 181 @raise lastfm.InvalidParametersError: Either album and artist parameters or 182 mbid parameter has to be provided. 183 Otherwise exception is raised. 184 185 @note: Use the L{Api.get_album} method instead of using this method directly. 186 """ 187 data = Album._fetch_data(api, artist, album, mbid) 188 a = Album( 189 api, 190 name = data.findtext('name'), 191 artist = Artist( 192 api, 193 name = data.findtext('artist'), 194 ), 195 ) 196 a._fill_info() 197 return a
198
199 - def _default_params(self, extra_params = {}):
200 if not (self.artist and self.name): 201 raise InvalidParametersError("artist and album have to be provided.") 202 params = {'artist': self.artist.name, 'album': self.name} 203 params.update(extra_params) 204 return params
205 206 @staticmethod
207 - def _fetch_data(api, 208 artist = None, 209 album = None, 210 mbid = None):
211 params = {'method': 'album.getInfo'} 212 if not ((artist and album) or mbid): 213 raise InvalidParametersError("either (artist and album) or mbid has to be given as argument.") 214 if artist and album: 215 params.update({'artist': artist, 'album': album}) 216 elif mbid: 217 params.update({'mbid': mbid}) 218 return api._fetch_data(params).find('album')
219
220 - def _fill_info(self):
221 data = Album._fetch_data(self._api, self.artist.name, self.name) 222 self._id = int(data.findtext('id')) 223 self._mbid = data.findtext('mbid') 224 self._url = data.findtext('url') 225 self._release_date = data.findtext('releasedate') and data.findtext('releasedate').strip() and \ 226 datetime(*(time.strptime(data.findtext('releasedate').strip(), '%d %b %Y, 00:00')[0:6])) 227 self._image = dict([(i.get('size'), i.text) for i in data.findall('image')]) 228 if not self._stats: 229 self._stats = Stats( 230 subject = self, 231 listeners = int(data.findtext('listeners')), 232 playcount = int(data.findtext('playcount')), 233 ) 234 self._top_tags = [ 235 Tag( 236 self._api, 237 subject = self, 238 name = t.findtext('name'), 239 url = t.findtext('url') 240 ) 241 for t in data.findall('toptags/tag') 242 ]
243 244 @staticmethod
245 - def _search_yield_func(api, album):
246 return Album( 247 api, 248 name = album.findtext('name'), 249 artist = Artist( 250 api, 251 name = album.findtext('artist') 252 ), 253 id = int(album.findtext('id')), 254 url = album.findtext('url'), 255 image = dict([(i.get('size'), i.text) for i in album.findall('image')]), 256 streamable = (album.findtext('streamable') == '1'), 257 )
258 259 @staticmethod
260 - def _hash_func(*args, **kwds):
261 try: 262 return hash("%s%s" % (kwds['name'], hash(kwds['artist']))) 263 except KeyError: 264 raise InvalidParametersError("name and artist have to be provided for hashing")
265
266 - def __hash__(self):
267 return self.__class__._hash_func(name = self.name, artist = self.artist)
268
269 - def __eq__(self, other):
270 if self.id and other.id: 271 return self.id == other.id 272 if self.mbid and other.mbid: 273 return self.mbid == other.mbid 274 if self.url and other.url: 275 return self.url == other.url 276 if (self.name and self.artist) and (other.name and other.artist): 277 return (self.name == other.name) and (self.artist == other.artist) 278 return super(Album, self).__eq__(other)
279
280 - def __lt__(self, other):
281 return self.name < other.name
282
283 - def __repr__(self):
284 return "<lastfm.Album: '%s' by %s>" % (self.name, self.artist.name)
285 286 287 from datetime import datetime 288 import time 289 290 from lastfm.api import Api 291 from lastfm.artist import Artist 292 from lastfm.error import InvalidParametersError 293 from lastfm.playlist import Playlist 294 from lastfm.stats import Stats 295 from lastfm.tag import Tag 296