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