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

Source Code for Module lastfm.tag

  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 
  9  from lastfm.lazylist import lazylist 
 10  from lastfm.decorators import cached_property, top_property 
11 12 -class Tag(LastfmBase, Cacheable, Searchable):
13 """A class representing a tag."""
14 - def init(self, 15 api, 16 name = None, 17 url = None, 18 streamable = None, 19 stats = None, 20 **kwargs):
21 if not isinstance(api, Api): 22 raise InvalidParametersError("api reference must be supplied as an argument") 23 self._api = api 24 self._name = name 25 self._url = url 26 self._streamable = streamable 27 self._stats = stats and Stats( 28 subject = self, 29 count = stats.count, 30 rank = stats.rank 31 )
32 33 @property
34 - def name(self):
35 """name of the tag""" 36 return self._name
37 38 @property
39 - def url(self):
40 """url of the tag's page""" 41 return self._url
42 43 @property
44 - def streamable(self):
45 """is the tag streamable""" 46 return self._streamable
47 48 @property
49 - def stats(self):
50 return self._stats
51 52 @cached_property
53 - def similar(self):
54 """tags similar to this tag""" 55 params = self._default_params({'method': 'tag.getSimilar'}) 56 data = self._api._fetch_data(params).find('similartags') 57 return [ 58 Tag( 59 self._api, 60 subject = self, 61 name = t.findtext('name'), 62 url = t.findtext('url'), 63 streamable = (t.findtext('streamable') == "1"), 64 ) 65 for t in data.findall('tag') 66 ]
67 68 @top_property("similar")
69 - def most_similar(self):
70 """most similar tag to this tag""" 71 pass
72 73 @cached_property
74 - def top_albums(self):
75 """top albums for the tag""" 76 params = self._default_params({'method': 'tag.getTopAlbums'}) 77 data = self._api._fetch_data(params).find('topalbums') 78 return [ 79 Album( 80 self._api, 81 subject = self, 82 name = a.findtext('name'), 83 artist = Artist( 84 self._api, 85 subject = self, 86 name = a.findtext('artist/name'), 87 mbid = a.findtext('artist/mbid'), 88 url = a.findtext('artist/url'), 89 ), 90 mbid = a.findtext('mbid'), 91 url = a.findtext('url'), 92 image = dict([(i.get('size'), i.text) for i in a.findall('image')]), 93 stats = Stats( 94 subject = a.findtext('name'), 95 tagcount = a.findtext('tagcount') and int(a.findtext('tagcount')) or None, 96 rank = a.attrib['rank'].strip() and int(a.attrib['rank']) or None 97 ) 98 ) 99 for a in data.findall('album') 100 ]
101 102 @top_property("top_albums")
103 - def top_album(self):
104 """top album for the tag""" 105 pass
106 107 @cached_property
108 - def top_artists(self):
109 """top artists for the tag""" 110 params = self._default_params({'method': 'tag.getTopArtists'}) 111 data = self._api._fetch_data(params).find('topartists') 112 return [ 113 Artist( 114 self._api, 115 subject = self, 116 name = a.findtext('name'), 117 mbid = a.findtext('mbid'), 118 stats = Stats( 119 subject = a.findtext('name'), 120 rank = a.attrib['rank'].strip() and int(a.attrib['rank']) or None, 121 tagcount = a.findtext('tagcount') and int(a.findtext('tagcount')) or None 122 ), 123 url = a.findtext('url'), 124 streamable = (a.findtext('streamable') == "1"), 125 image = dict([(i.get('size'), i.text) for i in a.findall('image')]), 126 ) 127 for a in data.findall('artist') 128 ]
129 130 @top_property("top_artists")
131 - def top_artist(self):
132 """top artist for the tag""" 133 pass
134 135 @cached_property
136 - def top_tracks(self):
137 """top tracks for the tag""" 138 params = self._default_params({'method': 'tag.getTopTracks'}) 139 data = self._api._fetch_data(params).find('toptracks') 140 return [ 141 Track( 142 self._api, 143 subject = self, 144 name = t.findtext('name'), 145 artist = Artist( 146 self._api, 147 subject = self, 148 name = t.findtext('artist/name'), 149 mbid = t.findtext('artist/mbid'), 150 url = t.findtext('artist/url'), 151 ), 152 mbid = t.findtext('mbid'), 153 stats = Stats( 154 subject = t.findtext('name'), 155 rank = t.attrib['rank'].strip() and int(t.attrib['rank']) or None, 156 tagcount = t.findtext('tagcount') and int(t.findtext('tagcount')) or None 157 ), 158 streamable = (t.findtext('streamable') == '1'), 159 full_track = (t.find('streamable').attrib['fulltrack'] == '1'), 160 image = dict([(i.get('size'), i.text) for i in t.findall('image')]), 161 ) 162 for t in data.findall('track') 163 ]
164 165 @top_property("top_tracks")
166 - def top_track(self):
167 """top track for the tag""" 168 pass
169 170 @cached_property
171 - def playlist(self):
172 return Playlist.fetch(self._api, 173 "lastfm://playlist/tag/%s/freetracks" % self.name)
174 175 @cached_property
176 - def weekly_chart_list(self):
177 params = self._default_params({'method': 'tag.getWeeklyChartList'}) 178 data = self._api._fetch_data(params).find('weeklychartlist') 179 return [ 180 WeeklyChart.create_from_data(self._api, self, c) 181 for c in data.findall('chart') 182 ]
183
184 - def get_weekly_artist_chart(self, 185 start = None, 186 end = None, 187 limit = None):
188 params = self._default_params({'method': 'tag.getWeeklyArtistChart'}) 189 if limit is not None: 190 params['limit'] = limit 191 params = WeeklyArtistChart._check_weekly_chart_params(params, start, end) 192 data = self._api._fetch_data(params).find('weeklyartistchart') 193 return WeeklyArtistChart.create_from_data(self._api, self, data)
194 195 @cached_property
197 return self.get_weekly_artist_chart()
198 199 @cached_property
200 - def weekly_artist_chart_list(self):
201 wcl = list(self.weekly_chart_list) 202 wcl.reverse() 203 @lazylist 204 def gen(lst): 205 for wc in wcl: 206 try: 207 yield self.get_weekly_artist_chart(wc.start, wc.end) 208 except LastfmError: 209 pass
210 return gen()
211 212 @staticmethod
213 - def get_top_tags(api):
214 params = {'method': 'tag.getTopTags'} 215 data = api._fetch_data(params).find('toptags') 216 return [ 217 Tag( 218 api, 219 name = t.findtext('name'), 220 url = t.findtext('url'), 221 stats = Stats( 222 subject = t.findtext('name'), 223 count = int(t.findtext('count')), 224 ) 225 ) 226 for t in data.findall('tag') 227 ]
228
229 - def _default_params(self, extra_params = {}):
230 if not self.name: 231 raise InvalidParametersError("tag has to be provided.") 232 params = {'tag': self.name} 233 params.update(extra_params) 234 return params
235 236 @staticmethod
237 - def _search_yield_func(api, tag):
238 return Tag( 239 api, 240 name = tag.findtext('name'), 241 url = tag.findtext('url'), 242 stats = Stats( 243 subject = tag.findtext('name'), 244 count = int(tag.findtext('count')), 245 ) 246 )
247 248 @staticmethod
249 - def _hash_func(*args, **kwds):
250 try: 251 return hash(kwds['name']) 252 except KeyError: 253 raise InvalidParametersError("name has to be provided for hashing")
254
255 - def __hash__(self):
256 return self.__class__._hash_func(name = self.name)
257
258 - def __eq__(self, other):
259 return self.name == other.name
260
261 - def __lt__(self, other):
262 return self.name < other.name
263
264 - def __repr__(self):
265 return "<lastfm.Tag: %s>" % self.name
266 267 from lastfm.album import Album 268 from lastfm.api import Api 269 from lastfm.artist import Artist 270 from lastfm.error import LastfmError, InvalidParametersError 271 from lastfm.playlist import Playlist 272 from lastfm.stats import Stats 273 from lastfm.track import Track 274 from lastfm.weeklychart import WeeklyChart, WeeklyArtistChart 275