1
2 """Module for calling Album related last.fm web services API methods"""
3
4 __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
5 __version__ = "0.2"
6 __license__ = "GNU Lesser General Public License"
7
8 from lastfm.base import LastfmBase
9 from lastfm.mixins import Cacheable, Searchable, Taggable
10
11 -class Album(LastfmBase, Cacheable, Searchable, Taggable):
12 """A class representing an album."""
13 - def init(self,
14 api,
15 name = None,
16 artist = None,
17 id = None,
18 mbid = None,
19 url = None,
20 release_date = None,
21 image = None,
22 stats = None,
23 top_tags = None,
24 streamable = None):
25 """
26 Create an Album object by providing all the data related to it.
27
28 @param api: an instance of L{Api}
29 @type api: L{Api}
30 @param name: the album name
31 @type name: L{str}
32 @param artist: the album artist name
33 @type artist: L{Artist}
34 @param id: the album ID
35 @type id: L{str}
36 @param mbid: MBID of the album
37 @type mbid: L{str}
38 @param url: URL of the album on last.fm
39 @type url: L{str}
40 @param release_date: release date of the album
41 @type release_date: C{datetime.datetime}
42 @param image: the cover images of the album in various sizes
43 @type image: L{dict}
44 @param stats: the album statistics
45 @type stats: L{Stats}
46 @param top_tags: top tags of the album
47 @type top_tags: L{list} of L{Tag}
48 @param streamable: flag indicating if the album is streamable
49 @type streamable: L{bool}
50
51 @raise InvalidParametersError: If an instance of L{Api} is not provided as the first
52 parameter then an Exception is raised.
53 """
54 if not isinstance(api, Api):
55 raise InvalidParametersError("api reference must be supplied as an argument")
56 Taggable.init(self, api)
57 self._api = api
58 self._name = name
59 self._artist = artist
60 self._id = id
61 self._mbid = mbid
62 self._url = url
63 self._release_date = release_date
64 self._image = image
65 self._stats = stats and Stats(
66 subject = self,
67 listeners = stats.listeners,
68 playcount = stats.playcount,
69 match = stats.match,
70 rank = stats.rank
71 )
72 self._top_tags = top_tags
73 self._streamable = streamable
74
75 @property
77 """
78 name of the album
79 @rtype: L{str}
80 """
81 return self._name
82
83 @property
85 """
86 artist of the album
87 @rtype: L{Artist}
88 """
89 return self._artist
90
91 @property
93 """
94 id of the album
95 @rtype: L{int}
96 """
97 if self._id is None:
98 self._fill_info()
99 return self._id
100
101 @property
103 """
104 mbid of the album
105 @rtype: L{str}
106 """
107 if self._mbid is None:
108 self._fill_info()
109 return self._mbid
110
111 @property
113 """
114 url of the album's page
115 @rtype: L{str}
116 """
117 if self._url is None:
118 self._fill_info()
119 return self._url
120
121 @property
123 """
124 release date of the album
125 @rtype: C{datetime.datetime}
126 """
127 if self._release_date is None:
128 self._fill_info()
129 return self._release_date
130
131 @property
133 """
134 cover images of the album
135 @rtype: L{dict}
136 """
137 if self._image is None:
138 self._fill_info()
139 return self._image
140
141 @property
143 """
144 stats related to the album
145 @rtype: L{Stats}
146 """
147 if self._stats is None:
148 self._fill_info()
149 return self._stats
150
151 @property
153 """
154 is the album streamable
155 @rtype: L{bool}
156 """
157 return self._streamable
158
159 @LastfmBase.cached_property
180
181 @LastfmBase.top_property("top_tags")
183 """
184 top tag for the album
185 @rtype: L{Tag}
186 """
187 pass
188
189 @LastfmBase.cached_property
191 """
192 playlist for the album
193 @rtype: L{Playlist}
194 """
195 return Playlist.fetch(self._api, "lastfm://playlist/album/%s" % self.id)
196
197 @staticmethod
198 - def get_info(api,
199 artist = None,
200 album = None,
201 mbid = None):
202 """
203 Get the data for the album.
204
205 @param api: an instance of L{Api}
206 @type api: L{Api}
207 @param artist: the album artist name
208 @type artist: L{str} OR L{Artist}
209 @param album: the album name
210 @type album: L{str}
211 @param mbid: MBID of the album
212 @type mbid: L{str}
213
214 @return: an Album object corresponding the provided album name
215 @rtype: L{Album}
216
217 @raise lastfm.InvalidParametersError: Either album and artist parameters or
218 mbid parameter has to be provided.
219 Otherwise exception is raised.
220
221 @note: Use the L{Api.get_album} method instead of using this method directly.
222 """
223 data = Album._fetch_data(api, artist, album, mbid)
224 a = Album(
225 api,
226 name = data.findtext('name'),
227 artist = Artist(
228 api,
229 name = data.findtext('artist'),
230 ),
231 )
232 a._fill_info()
233 return a
234
241
242 @staticmethod
243 - def _fetch_data(api,
244 artist = None,
245 album = None,
246 mbid = None):
255
257 data = Album._fetch_data(self._api, self.artist.name, self.name)
258 self._id = int(data.findtext('id'))
259 self._mbid = data.findtext('mbid')
260 self._url = data.findtext('url')
261 self._release_date = data.findtext('releasedate') and data.findtext('releasedate').strip() and \
262 datetime(*(time.strptime(data.findtext('releasedate').strip(), '%d %b %Y, 00:00')[0:6]))
263 self._image = dict([(i.get('size'), i.text) for i in data.findall('image')])
264 if not self._stats:
265 self._stats = Stats(
266 subject = self,
267 listeners = int(data.findtext('listeners')),
268 playcount = int(data.findtext('playcount')),
269 )
270 self._top_tags = [
271 Tag(
272 self._api,
273 subject = self,
274 name = t.findtext('name'),
275 url = t.findtext('url')
276 )
277 for t in data.findall('toptags/tag')
278 ]
279
280 @staticmethod
294
295 @staticmethod
297 try:
298 return hash("%s%s" % (kwds['name'], hash(kwds['artist'])))
299 except KeyError:
300 raise InvalidParametersError("name and artist have to be provided for hashing")
301
304
315
318
320 return "<lastfm.Album: '%s' by %s>" % (self.name, self.artist.name)
321
322
323 from datetime import datetime
324 import time
325
326 from lastfm.api import Api
327 from lastfm.artist import Artist
328 from lastfm.error import InvalidParametersError
329 from lastfm.playlist import Playlist
330 from lastfm.stats import Stats
331 from lastfm.tag import Tag
332