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
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):
30
31 @property
33 """name of the tag"""
34 return self._name
35
36 @property
38 """url of the tag's page"""
39 return self._url
40
41 @property
43 """is the tag streamable"""
44 return self._streamable
45
46 @property
49
50 @LastfmBase.cached_property
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")
68 """most similar tag to this tag"""
69 pass
70
71 @LastfmBase.cached_property
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")
102 """top album for the tag"""
103 pass
104
105 @LastfmBase.cached_property
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")
130 """top artist for the tag"""
131 pass
132
133 @LastfmBase.cached_property
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")
165 """top track for the tag"""
166 pass
167
168 @LastfmBase.cached_property
170 return Playlist.fetch(self._api,
171 "lastfm://playlist/tag/%s/freetracks" % self.name)
172
173 @LastfmBase.cached_property
181
192
193 @LastfmBase.cached_property
196
197 @LastfmBase.cached_property
208 return gen()
209
210 @staticmethod
226
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
245
246 @staticmethod
248 try:
249 return hash(kwds['name'])
250 except KeyError:
251 raise InvalidParametersError("name has to be provided for hashing")
252
255
258
261
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