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

Source Code for Module lastfm.artist

  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, Sharable, Shoutable, Taggable 
  9  from lastfm.lazylist import lazylist 
10 11 -class Artist(LastfmBase, Cacheable, Sharable, Shoutable, Searchable, Taggable):
12 """A class representing an artist."""
13 - def init(self, 14 api, 15 name = None, 16 mbid = None, 17 url = None, 18 image = None, 19 streamable = None, 20 stats = None, 21 similar = None, 22 top_tags = None, 23 bio = None):
24 if not isinstance(api, Api): 25 raise InvalidParametersError("api reference must be supplied as an argument") 26 Sharable.init(self, api) 27 Shoutable.init(self, api) 28 Taggable.init(self, api) 29 30 self._api = api 31 self._name = name 32 self._mbid = mbid 33 self._url = url 34 self._image = image 35 self._streamable = streamable 36 self._stats = stats and Stats( 37 subject = self, 38 listeners = stats.listeners, 39 playcount = stats.playcount, 40 weight = stats.weight, 41 match = stats.match, 42 rank = stats.rank 43 ) 44 self._similar = similar 45 self._top_tags = top_tags 46 self._bio = bio and Wiki( 47 subject = self, 48 published = bio.published, 49 summary = bio.summary, 50 content = bio.content 51 )
52 53 @property
54 - def name(self):
55 """name of the artist""" 56 return self._name
57 58 @property
59 - def mbid(self):
60 """mbid of the artist""" 61 if self._mbid is None: 62 self._fill_info() 63 return self._mbid
64 65 @property
66 - def url(self):
67 """url of the artist's page""" 68 if self._url is None: 69 self._fill_info() 70 return self._url
71 72 @property
73 - def image(self):
74 """images of the artist""" 75 if self._image is None: 76 self._fill_info() 77 return self._image
78 79 @property
80 - def streamable(self):
81 """is the artist streamable""" 82 if self._streamable is None: 83 self._fill_info() 84 return self._streamable
85 86 @property
87 - def stats(self):
88 """stats for the artist""" 89 if self._stats is None: 90 self._fill_info() 91 return self._stats
92
93 - def get_similar(self, limit = None):
94 params = self._default_params({'method': 'artist.getSimilar'}) 95 if limit is not None: 96 params.update({'limit': limit}) 97 data = self._api._fetch_data(params).find('similarartists') 98 self._similar = [ 99 Artist( 100 self._api, 101 subject = self, 102 name = a.findtext('name'), 103 mbid = a.findtext('mbid'), 104 stats = Stats( 105 subject = a.findtext('name'), 106 match = float(a.findtext('match')), 107 ), 108 url = 'http://' + a.findtext('url'), 109 image = {'large': a.findtext('image')} 110 ) 111 for a in data.findall('artist') 112 ] 113 return self._similar[:]
114 115 @property
116 - def similar(self):
117 """artists similar to this artist""" 118 if self._similar is None or len(self._similar) < 6: 119 return self.get_similar() 120 return self._similar[:]
121 122 @LastfmBase.top_property("similar")
123 - def most_similar(self):
124 """artist most similar to this artist""" 125 pass
126 127 @property
128 - def top_tags(self):
129 """top tags for the artist""" 130 if self._top_tags is None or len(self._top_tags) < 6: 131 params = self._default_params({'method': 'artist.getTopTags'}) 132 data = self._api._fetch_data(params).find('toptags') 133 self._top_tags = [ 134 Tag( 135 self._api, 136 subject = self, 137 name = t.findtext('name'), 138 url = t.findtext('url') 139 ) 140 for t in data.findall('tag') 141 ] 142 return self._top_tags[:]
143 144 @LastfmBase.top_property("top_tags")
145 - def top_tag(self):
146 """top tag for the artist""" 147 pass
148 149 @property
150 - def bio(self):
151 """biography of the artist""" 152 if self._bio is None: 153 self._fill_info() 154 return self._bio
155 156 @LastfmBase.cached_property
157 - def events(self):
158 """events for the artist""" 159 params = self._default_params({'method': 'artist.getEvents'}) 160 data = self._api._fetch_data(params).find('events') 161 162 return [ 163 Event.create_from_data(self._api, e) 164 for e in data.findall('event') 165 ]
166 167 @LastfmBase.cached_property
168 - def top_albums(self):
169 """top albums of the artist""" 170 params = self._default_params({'method': 'artist.getTopAlbums'}) 171 data = self._api._fetch_data(params).find('topalbums') 172 173 return [ 174 Album( 175 self._api, 176 subject = self, 177 name = a.findtext('name'), 178 artist = self, 179 mbid = a.findtext('mbid'), 180 url = a.findtext('url'), 181 image = dict([(i.get('size'), i.text) for i in a.findall('image')]), 182 stats = Stats( 183 subject = a.findtext('name'), 184 playcount = int(a.findtext('playcount')), 185 rank = int(a.attrib['rank']) 186 ) 187 ) 188 for a in data.findall('album') 189 ]
190 191 @LastfmBase.top_property("top_albums")
192 - def top_album(self):
193 """top album of the artist""" 194 pass
195 196 @LastfmBase.cached_property
197 - def top_fans(self):
198 """top fans of the artist""" 199 params = self._default_params({'method': 'artist.getTopFans'}) 200 data = self._api._fetch_data(params).find('topfans') 201 return [ 202 User( 203 self._api, 204 subject = self, 205 name = u.findtext('name'), 206 url = u.findtext('url'), 207 image = dict([(i.get('size'), i.text) for i in u.findall('image')]), 208 stats = Stats( 209 subject = u.findtext('name'), 210 weight = int(u.findtext('weight')) 211 ) 212 ) 213 for u in data.findall('user') 214 ]
215 216 @LastfmBase.top_property("top_fans")
217 - def top_fan(self):
218 """top fan of the artist""" 219 pass
220 221 @LastfmBase.cached_property
222 - def top_tracks(self):
223 """top tracks of the artist""" 224 params = self._default_params({'method': 'artist.getTopTracks'}) 225 data = self._api._fetch_data(params).find('toptracks') 226 return [ 227 Track( 228 self._api, 229 subject = self, 230 name = t.findtext('name'), 231 artist = self, 232 mbid = t.findtext('mbid'), 233 stats = Stats( 234 subject = t.findtext('name'), 235 playcount = int(t.findtext('playcount')), 236 rank = int(t.attrib['rank']) 237 ), 238 streamable = (t.findtext('streamable') == '1'), 239 full_track = (t.find('streamable').attrib['fulltrack'] == '1'), 240 image = dict([(i.get('size'), i.text) for i in t.findall('image')]), 241 ) 242 for t in data.findall('track') 243 ]
244 245 @LastfmBase.top_property("top_tracks")
246 - def top_track(self):
247 """topmost fan of the artist""" 248 pass
249 250 @staticmethod
251 - def get_info(api, 252 artist = None, 253 mbid = None):
254 data = Artist._fetch_data(api, artist, mbid) 255 256 a = Artist(api, name = data.findtext('name')) 257 a._fill_info() 258 return a
259
260 - def _default_params(self, extra_params = {}):
261 if not self.name: 262 raise InvalidParametersError("artist has to be provided.") 263 params = {'artist': self.name} 264 params.update(extra_params) 265 return params
266 267 @staticmethod
268 - def _fetch_data(api, 269 artist = None, 270 mbid = None):
271 params = {'method': 'artist.getInfo'} 272 if not (artist or mbid): 273 raise InvalidParametersError("either artist or mbid has to be given as argument.") 274 if artist: 275 params.update({'artist': artist}) 276 elif mbid: 277 params.update({'mbid': mbid}) 278 return api._fetch_data(params).find('artist')
279
280 - def _fill_info(self):
281 data = Artist._fetch_data(self._api, self.name) 282 self._name = data.findtext('name') 283 self._mbid = data.findtext('mbid') 284 self._url = data.findtext('url') 285 self._image = dict([(i.get('size'), i.text) for i in data.findall('image')]) 286 self._streamable = (data.findtext('streamable') == 1) 287 if not self._stats: 288 self._stats = Stats( 289 subject = self, 290 listeners = int(data.findtext('stats/listeners')), 291 playcount = int(data.findtext('stats/playcount')) 292 ) 293 self._similar = [ 294 Artist( 295 self._api, 296 subject = self, 297 name = a.findtext('name'), 298 url = a.findtext('url'), 299 image = dict([(i.get('size'), i.text) for i in a.findall('image')]) 300 ) 301 for a in data.findall('similar/artist') 302 ] 303 self._top_tags = [ 304 Tag( 305 self._api, 306 subject = self, 307 name = t.findtext('name'), 308 url = t.findtext('url') 309 ) 310 for t in data.findall('tags/tag') 311 ] 312 self._bio = Wiki( 313 self, 314 published = data.findtext('bio/published').strip() and 315 datetime(*(time.strptime( 316 data.findtext('bio/published').strip(), 317 '%a, %d %b %Y %H:%M:%S +0000' 318 )[0:6])), 319 summary = data.findtext('bio/summary'), 320 content = data.findtext('bio/content') 321 )
322 323 @staticmethod
324 - def _search_yield_func(api, artist):
325 return Artist( 326 api, 327 name = artist.findtext('name'), 328 mbid = artist.findtext('mbid'), 329 url = artist.findtext('url'), 330 image = dict([(i.get('size'), i.text) for i in artist.findall('image')]), 331 streamable = (artist.findtext('streamable') == '1'), 332 )
333 @staticmethod
334 - def _hash_func(*args, **kwds):
335 try: 336 return hash(kwds['name'].lower()) 337 except KeyError: 338 try: 339 return hash(args[1].lower()) 340 except IndexError: 341 raise InvalidParametersError("name has to be provided for hashing")
342
343 - def __hash__(self):
344 return self.__class__._hash_func(name = self.name)
345
346 - def __eq__(self, other):
347 if self.mbid and other.mbid: 348 return self.mbid == other.mbid 349 if self.url and other.url: 350 return self.url == other.url 351 return self.name == other.name
352
353 - def __lt__(self, other):
354 return self.name < other.name
355
356 - def __repr__(self):
357 return "<lastfm.Artist: %s>" % self._name
358 359 from datetime import datetime 360 import time 361 362 from lastfm.album import Album 363 from lastfm.api import Api 364 from lastfm.error import InvalidParametersError 365 from lastfm.event import Event 366 from lastfm.stats import Stats 367 from lastfm.tag import Tag 368 from lastfm.track import Track 369 from lastfm.user import User 370 from lastfm.wiki import Wiki 371