2008-07-10 22:25:05 +05:30
|
|
|
#!/usr/bin/env python
|
2009-03-17 20:51:40 +05:30
|
|
|
"""Module for calling Group related last.fm web services API methods"""
|
2008-07-10 22:25:05 +05:30
|
|
|
|
2008-08-25 17:14:48 +05:30
|
|
|
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
|
2008-09-02 22:06:16 +05:30
|
|
|
__version__ = "0.2"
|
2008-07-10 22:25:05 +05:30
|
|
|
__license__ = "GNU Lesser General Public License"
|
2009-03-17 20:51:40 +05:30
|
|
|
__package__ = "lastfm"
|
2008-07-10 22:25:05 +05:30
|
|
|
|
2008-12-31 11:00:23 +05:30
|
|
|
from lastfm.base import LastfmBase
|
2009-04-18 10:41:22 +05:30
|
|
|
from lastfm.mixin import mixin, chartable
|
2009-03-18 09:55:04 +05:30
|
|
|
from lastfm.decorators import cached_property, depaginate
|
2008-07-15 00:38:39 +05:30
|
|
|
|
2009-04-18 10:41:22 +05:30
|
|
|
@chartable('album', 'artist', 'track', 'tag')
|
|
|
|
@mixin("cacheable", "property_adder")
|
2009-03-31 09:09:23 +05:30
|
|
|
class Group(LastfmBase):
|
2008-07-10 22:25:05 +05:30
|
|
|
"""A class representing a group on last.fm."""
|
2009-04-18 10:41:22 +05:30
|
|
|
|
|
|
|
class Meta(object):
|
|
|
|
properties = ["name"]
|
|
|
|
|
|
|
|
def init(self, api, **kwargs):
|
2009-03-17 20:51:40 +05:30
|
|
|
"""
|
|
|
|
Create a Group object by providing all the data related to it.
|
|
|
|
|
|
|
|
@param api: an instance of L{Api}
|
|
|
|
@type api: L{Api}
|
|
|
|
@param name: name of the group on last.fm
|
|
|
|
@type name: L{str}
|
|
|
|
|
|
|
|
@raise InvalidParametersError: If an instance of L{Api} is not provided as the first
|
|
|
|
parameter then an Exception is raised.
|
|
|
|
"""
|
2008-07-16 18:36:42 +05:30
|
|
|
if not isinstance(api, Api):
|
2008-12-30 15:57:13 +05:30
|
|
|
raise InvalidParametersError("api reference must be supplied as an argument")
|
2009-03-31 09:09:23 +05:30
|
|
|
|
2008-12-30 19:21:04 +05:30
|
|
|
self._api = api
|
2009-04-18 10:41:22 +05:30
|
|
|
super(Group, self).init(**kwargs)
|
2008-07-10 22:25:05 +05:30
|
|
|
|
2009-03-10 11:39:11 +05:30
|
|
|
@cached_property
|
2009-03-18 00:04:06 +05:30
|
|
|
@depaginate
|
|
|
|
def members(self, page = None):
|
2009-03-17 20:51:40 +05:30
|
|
|
"""
|
|
|
|
members of the group
|
|
|
|
@rtype: L{lazylist} of L{User}
|
|
|
|
"""
|
2009-01-01 01:39:06 +05:30
|
|
|
params = self._default_params({'method': 'group.getMembers'})
|
2009-03-18 00:04:06 +05:30
|
|
|
if page is not None:
|
|
|
|
params.update({'page': page})
|
|
|
|
data = self._api._fetch_data(params).find('members')
|
|
|
|
total_pages = int(data.attrib['totalPages'])
|
|
|
|
yield total_pages
|
|
|
|
for u in data.findall('user'):
|
|
|
|
yield User(
|
|
|
|
self._api,
|
|
|
|
name = u.findtext('name'),
|
|
|
|
real_name = u.findtext('realname'),
|
|
|
|
image = dict([(i.get('size'), i.text) for i in u.findall('image')]),
|
|
|
|
url = u.findtext('url')
|
|
|
|
)
|
2009-01-01 01:39:06 +05:30
|
|
|
|
2009-03-18 08:49:57 +05:30
|
|
|
def _default_params(self, extra_params = None):
|
2008-12-30 19:21:04 +05:30
|
|
|
if not self.name:
|
|
|
|
raise InvalidParametersError("group has to be provided.")
|
|
|
|
params = {'group': self.name}
|
2009-03-18 08:49:57 +05:30
|
|
|
if extra_params is not None:
|
|
|
|
params.update(extra_params)
|
2008-12-30 19:21:04 +05:30
|
|
|
return params
|
|
|
|
|
2008-07-15 00:38:39 +05:30
|
|
|
@staticmethod
|
2008-12-30 19:21:04 +05:30
|
|
|
def _hash_func(*args, **kwds):
|
2008-07-15 00:38:39 +05:30
|
|
|
try:
|
|
|
|
return hash(kwds['name'])
|
|
|
|
except KeyError:
|
2008-12-30 15:57:13 +05:30
|
|
|
raise InvalidParametersError("name has to be provided for hashing")
|
2008-08-27 19:03:00 +05:30
|
|
|
|
2008-07-15 00:38:39 +05:30
|
|
|
def __hash__(self):
|
2008-12-30 19:21:04 +05:30
|
|
|
return self.__class__._hash_func(name = self.name)
|
2008-08-27 19:03:00 +05:30
|
|
|
|
2008-07-15 00:38:39 +05:30
|
|
|
def __eq__(self, other):
|
|
|
|
return self.name == other.name
|
2008-08-27 19:03:00 +05:30
|
|
|
|
2008-07-15 00:38:39 +05:30
|
|
|
def __lt__(self, other):
|
|
|
|
return self.name < other.name
|
2008-08-27 19:03:00 +05:30
|
|
|
|
2008-07-15 00:38:39 +05:30
|
|
|
def __repr__(self):
|
|
|
|
return "<lastfm.Group: %s>" % self.name
|
2008-07-15 17:29:49 +05:30
|
|
|
|
2008-12-31 11:00:23 +05:30
|
|
|
from lastfm.api import Api
|
2009-04-07 10:27:51 +05:30
|
|
|
from lastfm.error import InvalidParametersError
|
|
|
|
from lastfm.user import User
|