Package lastfm :: Module venue

[hide private]
[frames] | no frames]

# Source Code for Module lastfm.venue

      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, Searchable
      9  from lastfm.lazylist import lazylist
     10  from lastfm.decorators import cached_property
     11
     12 -class Venue(LastfmBase, Cacheable, Searchable):
     13      """A class representing a venue of an event"""
     14 -    def init(self,
     15               api,
     16               id = None,
     17               name = None,
     18               location = None,
     19               url = None,
     20               **kwargs):
     21          if not isinstance(api, Api):
     22              raise InvalidParametersError("api reference must be supplied as an argument")
     23          self._api = api
     24          self._id = id
     25          self._name = name
     26          self._location = location
     27          self._url = url
     28
     29      @property
     30 -    def id(self):
     31          """id of the venue"""
     32          return self._id
     33
     34      @property
     35 -    def name(self):
     36          """name of the venue"""
     37          return self._name
     38
     39      @property
     40 -    def location(self):
     41          """location of the event"""
     42          return self._location
     43
     44      @property
     45 -    def url(self):
     46          """url of the event's page"""
     47          return self._url
     48
     49      @cached_property
     50 -    def events(self):
     51          params = self._default_params({'method': 'venue.getEvents'})
     52          data = self._api._fetch_data(params).find('events')
     53
     54          return [
     55                  Event.create_from_data(self._api, e)
     56                  for e in data.findall('event')
     57                  ]
     58
     59 -    def get_past_events(self,
     60                        limit = None):
     61          params = self._default_params({'method': 'venue.getPastEvents'})
     62          if limit is not None:
     63              params.update({'limit': limit})
     64
     65          @lazylist
     66          def gen(lst):
     67              data = self._api._fetch_data(params).find('events')
     68              total_pages = int(data.attrib['totalPages'])
     69
     70              @lazylist
     71              def gen2(lst, data):
     72                  for e in data.findall('event'):
     73                      yield Event.create_from_data(self._api, e)
     74
     75              for e in gen2(data):
     76                  yield e
     77
     78              for page in xrange(2, total_pages+1):
     79                  params.update({'page': page})
     80                  data = self._api._fetch_data(params).find('events')
     81                  for e in gen2(data):
     82                      yield e
     83          return gen()
     84
     85      @cached_property
     86 -    def past_events(self):
     87          return self.get_past_events()
     88
     89 -    def _default_params(self, extra_params = {}):
     90          if not self.id:
     91              raise InvalidParametersError("venue id has to be provided.")
     92          params = {'venue': self.id}
     93          params.update(extra_params)
     94          return params
     95
     96      @staticmethod
     97 -    def _search_yield_func(api, venue):
     98          return Venue(
     99                       api,
    100                       id = int(venue.findtext('id')),
    101                       name = venue.findtext('name'),
    102                       location = Location(
    103                                           api,
    104                                           city = venue.findtext('location/city'),
    105                                           country = Country(
    106                                              api,
    107                                              name = venue.findtext('location/country')
    108                                              ),
    109                                           street = venue.findtext('location/street'),
    110                                           postal_code = venue.findtext('location/postalcode'),
    111                                           latitude = float(venue.findtext(
    112                                               'location/{%s}point/{%s}lat' % ((Location.XMLNS,)*2)
    113                                               )),
    114                                           longitude = float(venue.findtext(
    115                                               'location/{%s}point/{%s}long' % ((Location.XMLNS,)*2)
    116                                               )),
    117                                           ),
    118                       url = venue.findtext('url')
    119                       )
    120
    121      @staticmethod
    122 -    def _hash_func(*args, **kwds):
    123          try:
    124              return hash(kwds['url'])
    125          except KeyError:
    126              raise InvalidParametersError("url has to be provided for hashing")
    127
    128 -    def __hash__(self):
    129          return self.__class__._hash_func(url = self.url)
    130
    131 -    def __eq__(self, other):
    132          return self.url == other.url
    133
    134 -    def __lt__(self, other):
    135          return self.name < other.name
    136
    137 -    def __repr__(self):
    138          return "<lastfm.geo.Venue: %s, %s>" % (self.name, self.location.city)
    139
    140  from lastfm.api import Api
    141  from lastfm.event import Event
    142  from lastfm.geo import Location, Country
    143  from lastfm.error import InvalidParametersError
    144