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: str
32 @param artist: the album artist name
33 @type artist: L{Artist}
34 @param id: the album ID
35 @type id: str
36 @param mbid: MBID of the album
37 @type mbid: str
38 @param url: URL of the album on last.fm
39 @type url: str
40 @param release_date: release date of the album
41 @type release_date: datetime.datetime
42 @param image: the cover images of the album in various sizes
43 @type image: 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: list of L{Tag}
48 @param streamable: flag indicating if the album is streamable
49 @type streamable: 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 """name of the album"""
78 return self._name
79
80 @property
82 """artist of the album"""
83 return self._artist
84
85 @property
87 """id of the album"""
88 if self._id is None:
89 self._fill_info()
90 return self._id
91
92 @property
94 """mbid of the album"""
95 if self._mbid is None:
96 self._fill_info()
97 return self._mbid
98
99 @property
101 """url of the album's page"""
102 if self._url is None:
103 self._fill_info()
104 return self._url
105
106 @property
108 """release date of the album"""
109 if self._release_date is None:
110 self._fill_info()
111 return self._release_date
112
113 @property
115 """cover images of the album"""
116 if self._image is None:
117 self._fill_info()
118 return self._image
119
120 @property
122 """stats related to the album"""
123 if self._stats is None:
124 self._fill_info()
125 return self._stats
126
127 @property
129 """is the album streamable"""
130 return self._streamable
131
132 @LastfmBase.cached_property
150
151 @LastfmBase.top_property("top_tags")
153 """top tag for the album"""
154 pass
155
156 @LastfmBase.cached_property
158 """playlist for the album"""
159 return Playlist.fetch(self._api, "lastfm://playlist/album/%s" % self.id)
160
161 @staticmethod
162 - def get_info(api,
163 artist = None,
164 album = None,
165 mbid = None):
166 """
167 Get the data for the album.
168
169 @param api: an instance of L{Api}
170 @type api: L{Api}
171 @param artist: the album artist name
172 @type artist: str OR L{Artist}
173 @param album: the album name
174 @type album: str
175 @param mbid: MBID of the album
176 @type mbid: str
177
178 @return: an Album object corresponding the provided album name
179 @rtype: L{Album}
180
181 @raise lastfm.InvalidParametersError: Either album and artist parameters or
182 mbid parameter has to be provided.
183 Otherwise exception is raised.
184
185 @note: Use the L{Api.get_album} method instead of using this method directly.
186 """
187 data = Album._fetch_data(api, artist, album, mbid)
188 a = Album(
189 api,
190 name = data.findtext('name'),
191 artist = Artist(
192 api,
193 name = data.findtext('artist'),
194 ),
195 )
196 a._fill_info()
197 return a
198
205
206 @staticmethod
207 - def _fetch_data(api,
208 artist = None,
209 album = None,
210 mbid = None):
219
221 data = Album._fetch_data(self._api, self.artist.name, self.name)
222 self._id = int(data.findtext('id'))
223 self._mbid = data.findtext('mbid')
224 self._url = data.findtext('url')
225 self._release_date = data.findtext('releasedate') and data.findtext('releasedate').strip() and \
226 datetime(*(time.strptime(data.findtext('releasedate').strip(), '%d %b %Y, 00:00')[0:6]))
227 self._image = dict([(i.get('size'), i.text) for i in data.findall('image')])
228 if not self._stats:
229 self._stats = Stats(
230 subject = self,
231 listeners = int(data.findtext('listeners')),
232 playcount = int(data.findtext('playcount')),
233 )
234 self._top_tags = [
235 Tag(
236 self._api,
237 subject = self,
238 name = t.findtext('name'),
239 url = t.findtext('url')
240 )
241 for t in data.findall('toptags/tag')
242 ]
243
244 @staticmethod
258
259 @staticmethod
261 try:
262 return hash("%s%s" % (kwds['name'], hash(kwds['artist'])))
263 except KeyError:
264 raise InvalidParametersError("name and artist have to be provided for hashing")
265
268
279
282
284 return "<lastfm.Album: '%s' by %s>" % (self.name, self.artist.name)
285
286
287 from datetime import datetime
288 import time
289
290 from lastfm.api import Api
291 from lastfm.artist import Artist
292 from lastfm.error import InvalidParametersError
293 from lastfm.playlist import Playlist
294 from lastfm.stats import Stats
295 from lastfm.tag import Tag
296