Package lastfm :: Module group

[hide private]
[frames] | no frames]

# Source Code for Module lastfm.group

      1  #!/usr/bin/env python
      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
      9  from lastfm.lazylist import lazylist
     10  from lastfm.decorators import cached_property, top_property
     11
     12 -class Group(LastfmBase, Cacheable):
     13      """A class representing a group on last.fm."""
     14 -    def init(self,
     15                   api,
     16                   name = None,
     17                   **kwargs):
     18          if not isinstance(api, Api):
     19              raise InvalidParametersError("api reference must be supplied as an argument")
     20          self._api = api
     21          self._name = name
     22
     23      @property
     24 -    def name(self):
     25          return self._name
     26
     27      @cached_property
     28 -    def weekly_chart_list(self):
     29          params = self._default_params({'method': 'group.getWeeklyChartList'})
     30          data = self._api._fetch_data(params).find('weeklychartlist')
     31          return [
     32                  WeeklyChart.create_from_data(self._api, self, c)
     33                  for c in data.findall('chart')
     34                  ]
     35
     36 -    def get_weekly_album_chart(self,
     37                               start = None,
     38                               end = None):
     39          params = self._default_params({'method': 'group.getWeeklyAlbumChart'})
     40          params = WeeklyChart._check_weekly_chart_params(params, start, end)
     41          data = self._api._fetch_data(params).find('weeklyalbumchart')
     42          return WeeklyAlbumChart.create_from_data(self._api, self, data)
     43
     44      @cached_property
     45 -    def recent_weekly_album_chart(self):
     46          return self.get_weekly_album_chart()
     47
     48      @cached_property
     49 -    def weekly_album_chart_list(self):
     50          wcl = list(self.weekly_chart_list)
     51          wcl.reverse()
     52          @lazylist
     53          def gen(lst):
     54              for wc in wcl:
     55                  yield self.get_weekly_album_chart(wc.start, wc.end)
     56          return gen()
     57
     58 -    def get_weekly_artist_chart(self,
     59                               start = None,
     60                               end = None):
     61          params = self._default_params({'method': 'group.getWeeklyArtistChart'})
     62          params = WeeklyChart._check_weekly_chart_params(params, start, end)
     63          data = self._api._fetch_data(params).find('weeklyartistchart')
     64          return WeeklyArtistChart.create_from_data(self._api, self, data)
     65
     66      @cached_property
     67 -    def recent_weekly_artist_chart(self):
     68          return self.get_weekly_artist_chart()
     69
     70      @cached_property
     71 -    def weekly_artist_chart_list(self):
     72          wcl = list(self.weekly_chart_list)
     73          wcl.reverse()
     74          @lazylist
     75          def gen(lst):
     76              for wc in wcl:
     77                  yield self.get_weekly_artist_chart(wc.start, wc.end)
     78          return gen()
     79
     80 -    def get_weekly_track_chart(self,
     81                               start = None,
     82                               end = None):
     83          params = self._default_params({'method': 'group.getWeeklyTrackChart'})
     84          params = WeeklyChart._check_weekly_chart_params(params, start, end)
     85          data = self._api._fetch_data(params).find('weeklytrackchart')
     86          return WeeklyTrackChart.create_from_data(self._api, self, data)
     87
     88      @cached_property
     89 -    def recent_weekly_track_chart(self):
     90          return self.get_weekly_track_chart()
     91
     92      @cached_property
     93 -    def weekly_track_chart_list(self):
     94          wcl = list(self.weekly_chart_list)
     95          wcl.reverse()
     96          @lazylist
     97          def gen(lst):
     98              for wc in wcl:
     99                  yield self.get_weekly_track_chart(wc.start, wc.end)
    100          return gen()
    101
    102      @cached_property
    103 -    def members(self):
    104          params = self._default_params({'method': 'group.getMembers'})
    105
    106          @lazylist
    107          def gen(lst):
    108              data = self._api._fetch_data(params).find('members')
    109              total_pages = int(data.attrib['totalPages'])
    110
    111              @lazylist
    112              def gen2(lst, data):
    113                  for u in data.findall('user'):
    114                      yield User(
    115                                 self._api,
    116                                 name = u.findtext('name'),
    117                                 real_name = u.findtext('realname'),
    118                                 image = dict([(i.get('size'), i.text) for i in u.findall('image')]),
    119                                 url = u.findtext('url')
    120                                 )
    121
    122              for u in gen2(data):
    123                  yield u
    124
    125              for page in xrange(1, total_pages+1):
    126                  params.update({'page': page})
    127                  data = self._api._fetch_data(params).find('members')
    128                  for u in gen2(data):
    129                      yield u
    130          return gen()
    131
    132 -    def _default_params(self, extra_params = {}):
    133          if not self.name:
    134              raise InvalidParametersError("group has to be provided.")
    135          params = {'group': self.name}
    136          params.update(extra_params)
    137          return params
    138
    139      @staticmethod
    140 -    def _hash_func(*args, **kwds):
    141          try:
    142              return hash(kwds['name'])
    143          except KeyError:
    144              raise InvalidParametersError("name has to be provided for hashing")
    145
    146 -    def __hash__(self):
    147          return self.__class__._hash_func(name = self.name)
    148
    149 -    def __eq__(self, other):
    150          return self.name == other.name
    151
    152 -    def __lt__(self, other):
    153          return self.name < other.name
    154
    155 -    def __repr__(self):
    156          return "<lastfm.Group: %s>" % self.name
    157
    158  from lastfm.api import Api
    159  from lastfm.error import InvalidParametersError
    160  from lastfm.user import User
    161  from lastfm.weeklychart import WeeklyChart, WeeklyAlbumChart, WeeklyArtistChart, WeeklyTrackChart
    162