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: L{str} 32 @param artist: the album artist name 33 @type artist: L{Artist} 34 @param id: the album ID 35 @type id: L{str} 36 @param mbid: MBID of the album 37 @type mbid: L{str} 38 @param url: URL of the album on last.fm 39 @type url: L{str} 40 @param release_date: release date of the album 41 @type release_date: C{datetime.datetime} 42 @param image: the cover images of the album in various sizes 43 @type image: L{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: L{list} of L{Tag} 48 @param streamable: flag indicating if the album is streamable 49 @type streamable: L{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 """ 78 name of the album 79 @rtype: L{str} 80 """ 81 return self._name
82 83 @property
84 - def artist(self):
85 """ 86 artist of the album 87 @rtype: L{Artist} 88 """ 89 return self._artist
90 91 @property
92 - def id(self):
93 """ 94 id of the album 95 @rtype: L{int} 96 """ 97 if self._id is None: 98 self._fill_info() 99 return self._id
100 101 @property
102 - def mbid(self):
103 """ 104 mbid of the album 105 @rtype: L{str} 106 """ 107 if self._mbid is None: 108 self._fill_info() 109 return self._mbid
110 111 @property
112 - def url(self):
113 """ 114 url of the album's page 115 @rtype: L{str} 116 """ 117 if self._url is None: 118 self._fill_info() 119 return self._url
120 121 @property
122 - def release_date(self):
123 """ 124 release date of the album 125 @rtype: C{datetime.datetime} 126 """ 127 if self._release_date is None: 128 self._fill_info() 129 return self._release_date
130 131 @property
132 - def image(self):
133 """ 134 cover images of the album 135 @rtype: L{dict} 136 """ 137 if self._image is None: 138 self._fill_info() 139 return self._image
140 141 @property
142 - def stats(self):
143 """ 144 stats related to the album 145 @rtype: L{Stats} 146 """ 147 if self._stats is None: 148 self._fill_info() 149 return self._stats
150 151 @property
152 - def streamable(self):
153 """ 154 is the album streamable 155 @rtype: L{bool} 156 """ 157 return self._streamable
158 159 @LastfmBase.cached_property
160 - def top_tags(self):
161 """ 162 top tags for the album 163 @rtype: L{list} of L{Tag} 164 """ 165 params = {'method': 'album.getInfo'} 166 if self.artist and self.name: 167 params.update({'artist': self.artist.name, 'album': self.name}) 168 elif self.mbid: 169 params.update({'mbid': self.mbid}) 170 data = self._api._fetch_data(params).find('album') 171 return [ 172 Tag( 173 self._api, 174 subject = self, 175 name = t.findtext('name'), 176 url = t.findtext('url') 177 ) 178 for t in data.findall('toptags/tag') 179 ]
180 181 @LastfmBase.top_property("top_tags")
182 - def top_tag(self):
183 """ 184 top tag for the album 185 @rtype: L{Tag} 186 """ 187 pass
188 189 @LastfmBase.cached_property
190 - def playlist(self):
191 """ 192 playlist for the album 193 @rtype: L{Playlist} 194 """ 195 return Playlist.fetch(self._api, "lastfm://playlist/album/%s" % self.id)
196 197 @staticmethod
198 - def get_info(api, 199 artist = None, 200 album = None, 201 mbid = None):
202 """ 203 Get the data for the album. 204 205 @param api: an instance of L{Api} 206 @type api: L{Api} 207 @param artist: the album artist name 208 @type artist: L{str} OR L{Artist} 209 @param album: the album name 210 @type album: L{str} 211 @param mbid: MBID of the album 212 @type mbid: L{str} 213 214 @return: an Album object corresponding the provided album name 215 @rtype: L{Album} 216 217 @raise lastfm.InvalidParametersError: Either album and artist parameters or 218 mbid parameter has to be provided. 219 Otherwise exception is raised. 220 221 @note: Use the L{Api.get_album} method instead of using this method directly. 222 """ 223 data = Album._fetch_data(api, artist, album, mbid) 224 a = Album( 225 api, 226 name = data.findtext('name'), 227 artist = Artist( 228 api, 229 name = data.findtext('artist'), 230 ), 231 ) 232 a._fill_info() 233 return a
234
235 - def _default_params(self, extra_params = {}):
236 if not (self.artist and self.name): 237 raise InvalidParametersError("artist and album have to be provided.") 238 params = {'artist': self.artist.name, 'album': self.name} 239 params.update(extra_params) 240 return params
241 242 @staticmethod
243 - def _fetch_data(api, 244 artist = None, 245 album = None, 246 mbid = None):
247 params = {'method': 'album.getInfo'} 248 if not ((artist and album) or mbid): 249 raise InvalidParametersError("either (artist and album) or mbid has to be given as argument.") 250 if artist and album: 251 params.update({'artist': artist, 'album': album}) 252 elif mbid: 253 params.update({'mbid': mbid}) 254 return api._fetch_data(params).find('album')
255
256 - def _fill_info(self):
257 data = Album._fetch_data(self._api, self.artist.name, self.name) 258 self._id = int(data.findtext('id')) 259 self._mbid = data.findtext('mbid') 260 self._url = data.findtext('url') 261 self._release_date = data.findtext('releasedate') and data.findtext('releasedate').strip() and \ 262 datetime(*(time.strptime(data.findtext('releasedate').strip(), '%d %b %Y, 00:00')[0:6])) 263 self._image = dict([(i.get('size'), i.text) for i in data.findall('image')]) 264 if not self._stats: 265 self._stats = Stats( 266 subject = self, 267 listeners = int(data.findtext('listeners')), 268 playcount = int(data.findtext('playcount')), 269 ) 270 self._top_tags = [ 271 Tag( 272 self._api, 273 subject = self, 274 name = t.findtext('name'), 275 url = t.findtext('url') 276 ) 277 for t in data.findall('toptags/tag') 278 ]
279 280 @staticmethod
281 - def _search_yield_func(api, album):
282 return Album( 283 api, 284 name = album.findtext('name'), 285 artist = Artist( 286 api, 287 name = album.findtext('artist') 288 ), 289 id = int(album.findtext('id')), 290 url = album.findtext('url'), 291 image = dict([(i.get('size'), i.text) for i in album.findall('image')]), 292 streamable = (album.findtext('streamable') == '1'), 293 )
294 295 @staticmethod
296 - def _hash_func(*args, **kwds):
297 try: 298 return hash("%s%s" % (kwds['name'], hash(kwds['artist']))) 299 except KeyError: 300 raise InvalidParametersError("name and artist have to be provided for hashing")
301
302 - def __hash__(self):
303 return self.__class__._hash_func(name = self.name, artist = self.artist)
304
305 - def __eq__(self, other):
306 if self.id and other.id: 307 return self.id == other.id 308 if self.mbid and other.mbid: 309 return self.mbid == other.mbid 310 if self.url and other.url: 311 return self.url == other.url 312 if (self.name and self.artist) and (other.name and other.artist): 313 return (self.name == other.name) and (self.artist == other.artist) 314 return super(Album, self).__eq__(other)
315
316 - def __lt__(self, other):
317 return self.name < other.name
318
319 - def __repr__(self):
320 return "<lastfm.Album: '%s' by %s>" % (self.name, self.artist.name)
321 322 323 from datetime import datetime 324 import time 325 326 from lastfm.api import Api 327 from lastfm.artist import Artist 328 from lastfm.error import InvalidParametersError 329 from lastfm.playlist import Playlist 330 from lastfm.stats import Stats 331 from lastfm.tag import Tag 332