1
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):
32
33 @property
35 """name of the tag"""
36 return self._name
37
38 @property
40 """url of the tag's page"""
41 return self._url
42
43 @property
45 """is the tag streamable"""
46 return self._streamable
47
48 @property
51
52 @cached_property
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")
70 """most similar tag to this tag"""
71 pass
72
73 @cached_property
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")
104 """top album for the tag"""
105 pass
106
107 @cached_property
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")
132 """top artist for the tag"""
133 pass
134
135 @cached_property
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")
167 """top track for the tag"""
168 pass
169
170 @cached_property
172 return Playlist.fetch(self._api,
173 "lastfm://playlist/tag/%s/freetracks" % self.name)
174
175 @cached_property
183
194
195 @cached_property
198
199 @cached_property
210 return gen()
211
212 @staticmethod
228
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
247
248 @staticmethod
250 try:
251 return hash(kwds['name'])
252 except KeyError:
253 raise InvalidParametersError("name has to be provided for hashing")
254
257
260
263
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