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 for the album 47 @type top_tags: L{list} of L{Tag} 48 @param streamable: flag indicating if the album is streamable from last.fm 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 on last.fm 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, artist = None, album = None, mbid = None):
199 """ 200 Get the data for the album. 201 202 @param api: an instance of L{Api} 203 @type api: L{Api} 204 @param artist: the album artist name 205 @type artist: L{str} OR L{Artist} 206 @param album: the album name 207 @type album: L{str} 208 @param mbid: MBID of the album 209 @type mbid: L{str} 210 211 @return: an Album object corresponding the provided album name 212 @rtype: L{Album} 213 214 @raise lastfm.InvalidParametersError: Either album and artist parameters or 215 mbid parameter has to be provided. 216 Otherwise exception is raised. 217 218 @note: Use the L{Api.get_album} method instead of using this method directly. 219 """ 220 data = Album._fetch_data(api, artist, album, mbid) 221 a = Album( 222 api, 223 name = data.findtext('name'), 224 artist = Artist( 225 api, 226 name = data.findtext('artist'), 227 ), 228 ) 229 a._fill_info() 230 return a
231
232 - def _default_params(self, extra_params = {}):
233 if not (self.artist and self.name): 234 raise InvalidParametersError("artist and album have to be provided.") 235 params = {'artist': self.artist.name, 'album': self.name} 236 params.update(extra_params) 237 return params
238 239 @staticmethod
240 - def _fetch_data(api, 241 artist = None, 242 album = None, 243 mbid = None):
244 params = {'method': 'album.getInfo'} 245 if not ((artist and album) or mbid): 246 raise InvalidParametersError("either (artist and album) or mbid has to be given as argument.") 247 if artist and album: 248 params.update({'artist': artist, 'album': album}) 249 elif mbid: 250 params.update({'mbid': mbid}) 251 return api._fetch_data(params).find('album')
252
253 - def _fill_info(self):
254 data = Album._fetch_data(self._api, self.artist.name, self.name) 255 self._id = int(data.findtext('id')) 256 self._mbid = data.findtext('mbid') 257 self._url = data.findtext('url') 258 self._release_date = data.findtext('releasedate') and data.findtext('releasedate').strip() and \ 259 datetime(*(time.strptime(data.findtext('releasedate').strip(), '%d %b %Y, 00:00')[0:6])) 260 self._image = dict([(i.get('size'), i.text) for i in data.findall('image')]) 261 if not self._stats: 262 self._stats = Stats( 263 subject = self, 264 listeners = int(data.findtext('listeners')), 265 playcount = int(data.findtext('playcount')), 266 ) 267 self._top_tags = [ 268 Tag( 269 self._api, 270 subject = self, 271 name = t.findtext('name'), 272 url = t.findtext('url') 273 ) 274 for t in data.findall('toptags/tag') 275 ]
276 277 @staticmethod
278 - def _search_yield_func(api, album):
279 return Album( 280 api, 281 name = album.findtext('name'), 282 artist = Artist( 283 api, 284 name = album.findtext('artist') 285 ), 286 id = int(album.findtext('id')), 287 url = album.findtext('url'), 288 image = dict([(i.get('size'), i.text) for i in album.findall('image')]), 289 streamable = (album.findtext('streamable') == '1'), 290 )
291 292 @staticmethod
293 - def _hash_func(*args, **kwds):
294 try: 295 return hash("%s%s" % (kwds['name'], hash(kwds['artist']))) 296 except KeyError: 297 raise InvalidParametersError("name and artist have to be provided for hashing")
298
299 - def __hash__(self):
300 return self.__class__._hash_func(name = self.name, artist = self.artist)
301
302 - def __eq__(self, other):
303 if self.id and other.id: 304 return self.id == other.id 305 if self.mbid and other.mbid: 306 return self.mbid == other.mbid 307 if self.url and other.url: 308 return self.url == other.url 309 if (self.name and self.artist) and (other.name and other.artist): 310 return (self.name == other.name) and (self.artist == other.artist) 311 return super(Album, self).__eq__(other)
312
313 - def __lt__(self, other):
314 return self.name < other.name
315
316 - def __repr__(self):
317 return "<lastfm.Album: '%s' by %s>" % (self.name, self.artist.name)
318 319 320 from datetime import datetime 321 import time 322 323 from lastfm.api import Api 324 from lastfm.artist import Artist 325 from lastfm.error import InvalidParametersError 326 from lastfm.playlist import Playlist 327 from lastfm.stats import Stats 328 from lastfm.tag import Tag 329