Package lastfm :: Module event

[hide private]
[frames] | no frames]

# Source Code for Module lastfm.event

      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, Sharable, Shoutable
      9
     10 -class Event(LastfmBase, Cacheable, Sharable, Shoutable):
     11      """A class representing an event."""
     12      STATUS_ATTENDING = 0
     13      STATUS_MAYBE = 1
     14      STATUS_NOT = 2
     15
     16 -    def init(self,
     17                   api,
     18                   id = None,
     19                   title = None,
     20                   artists = None,
     21                   headliner = None,
     22                   venue = None,
     23                   start_date = None,
     24                   description = None,
     25                   image = None,
     26                   url = None,
     27                   stats = None,
     28                   tag = None,
     29                   subject = None):
     30          if not isinstance(api, Api):
     31              raise InvalidParametersError("api reference must be supplied as an argument")
     32          Sharable.init(self, api)
     33          Shoutable.init(self, api)
     34
     35          self._api = api
     36          self._id = id
     37          self._title = title
     38          self._artists = artists
     39          self._headliner = headliner
     40          self._venue = venue
     41          self._start_date = start_date
     42          self._description = description
     43          self._image = image
     44          self._url = url
     45          self._stats = stats and Stats(
     46                               subject = self,
     47                               attendance = stats.attendance,
     48                               reviews = stats.reviews
     49                              )
     50          self._tag = tag
     51          self._subject = subject
     52
     53      @property
     54 -    def id(self):
     55          """id of the event"""
     56          return self._id
     57
     58      @property
     59 -    def title(self):
     60          """title of the event"""
     61          return self._title
     62
     63      @property
     64 -    def artists(self):
     65          """artists performing in the event"""
     66          return self._artists
     67
     68      @property
     69 -    def headliner(self):
     70          """headliner artist of the event"""
     71          return self._headliner
     72
     73      @property
     74 -    def venue(self):
     75          """venue of the event"""
     76          return self._venue
     77
     78      @property
     79 -    def start_date(self):
     80          """start date of the event"""
     81          return self._start_date
     82
     83      @property
     84 -    def description(self):
     85          """description of the event"""
     86          return self._description
     87
     88      @property
     89 -    def image(self):
     90          """poster of the event"""
     91          return self._image
     92
     93      @property
     94 -    def url(self):
     95          """url of the event's page"""
     96          return self._url
     97
     98      @property
     99 -    def stats(self):
    100          """stats of the event"""
    101          return self._stats
    102
    103      @property
    104 -    def tag(self):
    105          """tags for the event"""
    106          return self._tag
    107
    108 -    def attend(self, status = STATUS_ATTENDING):
    109          if status not in [Event.STATUS_ATTENDING, Event.STATUS_MAYBE, Event.STATUS_NOT]:
    110              InvalidParametersError("status has to be 0, 1 or 2")
    111          params = self._default_params({'method': 'event.attend', 'status': status})
    112          self._api._post_data(params)
    113
    114      @staticmethod
    115 -    def get_info(api, event):
    116          params = {'method': 'event.getInfo', 'event': event}
    117          data = api._fetch_data(params).find('event')
    118          return Event.create_from_data(api, data)
    119
    120      @staticmethod
    121 -    def create_from_data(api, data):
    122          start_date = None
    123
    124          if data.findtext('startTime') is not None:
    125              start_date = datetime(*(
    126                  time.strptime(
    127                      "%s %s" % (
    128                          data.findtext('startDate').strip(),
    129                          data.findtext('startTime').strip()
    130                      ),
    131                      '%a, %d %b %Y %H:%M'
    132                  )[0:6])
    133              )
    134          else:
    135              try:
    136                  start_date = datetime(*(
    137                      time.strptime(
    138                          data.findtext('startDate').strip(),
    139                          '%a, %d %b %Y %H:%M:%S'
    140                      )[0:6])
    141                  )
    142              except ValueError:
    143                  try:
    144                      start_date = datetime(*(
    145                          time.strptime(
    146                              data.findtext('startDate').strip(),
    147                              '%a, %d %b %Y'
    148                          )[0:6])
    149                      )
    150                  except ValueError:
    151                      pass
    152
    153
    154          return Event(
    155                       api,
    156                       id = int(data.findtext('id')),
    157                       title = data.findtext('title'),
    158                       artists = [Artist(api, name = a.text) for a in data.findall('artists/artist')],
    159                       headliner = Artist(api, name = data.findtext('artists/headliner')),
    160                       venue = Venue(
    161                                     api,
    162                                     name = data.findtext('venue/name'),
    163                                     location = Location(
    164                                                         api,
    165                                                         city = data.findtext('venue/location/city'),
    166                                                         country = Country(
    167                                                              api,
    168                                                              name = data.findtext('venue/location/country')
    169                                                              ),
    170                                                         street = data.findtext('venue/location/street'),
    171                                                         postal_code = data.findtext('venue/location/postalcode'),
    172                                                         latitude = float(data.findtext(
    173                                                             'venue/location/{%s}point/{%s}lat' % ((Location.XMLNS,)*2)
    174                                                             )),
    175                                                         longitude = float(data.findtext(
    176                                                             'venue/location/{%s}point/{%s}long' % ((Location.XMLNS,)*2)
    177                                                             )),
    178                                                         #timezone = data.findtext('venue/location/timezone')
    179                                                         ),
    180                                     url = data.findtext('venue/url')
    181                                     ),
    182                       start_date = start_date,
    183                       description = data.findtext('description'),
    184                       image = dict([(i.get('size'), i.text) for i in data.findall('image')]),
    185                       url = data.findtext('url'),
    186                       stats = Stats(
    187                                     subject = int(data.findtext('id')),
    188                                     attendance = int(data.findtext('attendance')),
    189                                     reviews = int(data.findtext('reviews')),
    190                                     ),
    191                       tag = data.findtext('tag')
    192                      )
    193
    194 -    def _default_params(self, extra_params = []):
    195          if not self.id:
    196              raise InvalidParametersError("id has to be provided.")
    197          params = {'event': self.id}
    198          params.update(extra_params)
    199          return params
    200
    201      @staticmethod
    202 -    def _hash_func(*args, **kwds):
    203          try:
    204              return hash(kwds['id'])
    205          except KeyError:
    206              raise InvalidParametersError("id has to be provided for hashing")
    207
    208 -    def __hash__(self):
    209          return Event._hash_func(id = self.id)
    210
    211 -    def __eq__(self, other):
    212          return self.id == other.id
    213
    214 -    def __lt__(self, other):
    215          return self.start_date < other.start_date
    216
    217 -    def __repr__(self):
    218          return "<lastfm.Event: %s at %s on %s>" % (self.title, self.venue.name, self.start_date.strftime("%x"))
    219
    220  from datetime import datetime
    221  import time
    222
    223  from lastfm.api import Api
    224  from lastfm.artist import Artist
    225  from lastfm.error import InvalidParametersError
    226  from lastfm.geo import Location, Country
    227  from lastfm.stats import Stats
    228  from lastfm.venue import Venue
    229