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

Source Code for Module lastfm.album

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