2009-01-01 16:34:31 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
|
|
|
|
__version__ = "0.2"
|
|
|
|
__license__ = "GNU Lesser General Public License"
|
2009-03-17 20:51:40 +05:30
|
|
|
__package__ = "lastfm"
|
2009-01-01 16:34:31 +05:30
|
|
|
|
|
|
|
from lastfm.base import LastfmBase
|
2009-04-07 10:27:51 +05:30
|
|
|
from lastfm.mixins import cacheable, searchable, crawlable
|
2009-03-18 00:04:06 +05:30
|
|
|
from lastfm.decorators import cached_property, depaginate
|
2009-01-01 16:34:31 +05:30
|
|
|
|
2009-04-07 10:27:51 +05:30
|
|
|
@crawlable
|
2009-03-31 09:09:23 +05:30
|
|
|
@searchable
|
|
|
|
@cacheable
|
|
|
|
class Venue(LastfmBase):
|
2009-01-01 16:34:31 +05:30
|
|
|
"""A class representing a venue of an event"""
|
|
|
|
def init(self,
|
|
|
|
api,
|
|
|
|
id = None,
|
|
|
|
name = None,
|
|
|
|
location = None,
|
2009-03-10 11:39:11 +05:30
|
|
|
url = None,
|
|
|
|
**kwargs):
|
2009-01-01 16:34:31 +05:30
|
|
|
if not isinstance(api, Api):
|
|
|
|
raise InvalidParametersError("api reference must be supplied as an argument")
|
|
|
|
self._api = api
|
|
|
|
self._id = id
|
|
|
|
self._name = name
|
|
|
|
self._location = location
|
|
|
|
self._url = url
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
"""id of the venue"""
|
|
|
|
return self._id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""name of the venue"""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def location(self):
|
|
|
|
"""location of the event"""
|
|
|
|
return self._location
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
"""url of the event's page"""
|
|
|
|
return self._url
|
|
|
|
|
2009-03-10 11:39:11 +05:30
|
|
|
@cached_property
|
2009-01-01 16:34:31 +05:30
|
|
|
def events(self):
|
|
|
|
params = self._default_params({'method': 'venue.getEvents'})
|
|
|
|
data = self._api._fetch_data(params).find('events')
|
|
|
|
|
|
|
|
return [
|
|
|
|
Event.create_from_data(self._api, e)
|
|
|
|
for e in data.findall('event')
|
|
|
|
]
|
2009-03-18 00:04:06 +05:30
|
|
|
|
|
|
|
@depaginate
|
|
|
|
def get_past_events(self, limit = None, page = None):
|
2009-01-01 16:34:31 +05:30
|
|
|
params = self._default_params({'method': 'venue.getPastEvents'})
|
|
|
|
if limit is not None:
|
|
|
|
params.update({'limit': limit})
|
2009-03-18 00:04:06 +05:30
|
|
|
if page is not None:
|
|
|
|
params.update({'page': page})
|
2009-01-01 16:34:31 +05:30
|
|
|
|
2009-03-18 00:04:06 +05:30
|
|
|
data = self._api._fetch_data(params).find('events')
|
|
|
|
total_pages = int(data.attrib['totalPages'])
|
|
|
|
yield total_pages
|
2009-01-01 16:34:31 +05:30
|
|
|
|
2009-03-18 00:04:06 +05:30
|
|
|
for e in data.findall('event'):
|
|
|
|
yield Event.create_from_data(self._api, e)
|
2009-01-01 16:34:31 +05:30
|
|
|
|
2009-03-10 11:39:11 +05:30
|
|
|
@cached_property
|
2009-01-01 16:34:31 +05:30
|
|
|
def past_events(self):
|
|
|
|
return self.get_past_events()
|
|
|
|
|
2009-04-07 10:27:51 +05:30
|
|
|
@staticmethod
|
|
|
|
def _get_all(seed_venue):
|
|
|
|
def gen():
|
|
|
|
for event in Event.get_all(seed_venue.past_events[0]):
|
|
|
|
yield event.venue
|
|
|
|
|
|
|
|
return (seed_venue, ['id'], lambda api, hsh: gen())
|
|
|
|
|
|
|
|
|
2009-01-01 16:34:31 +05:30
|
|
|
def _default_params(self, extra_params = {}):
|
|
|
|
if not self.id:
|
|
|
|
raise InvalidParametersError("venue id has to be provided.")
|
|
|
|
params = {'venue': self.id}
|
|
|
|
params.update(extra_params)
|
|
|
|
return params
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _search_yield_func(api, venue):
|
2009-03-18 00:04:06 +05:30
|
|
|
latitude = venue.findtext('location/{%s}point/{%s}lat' % ((Location.XMLNS,)*2))
|
|
|
|
longitude = venue.findtext('location/{%s}point/{%s}long' % ((Location.XMLNS,)*2))
|
|
|
|
|
2009-01-01 16:34:31 +05:30
|
|
|
return Venue(
|
|
|
|
api,
|
|
|
|
id = int(venue.findtext('id')),
|
|
|
|
name = venue.findtext('name'),
|
|
|
|
location = Location(
|
|
|
|
api,
|
|
|
|
city = venue.findtext('location/city'),
|
|
|
|
country = Country(
|
|
|
|
api,
|
|
|
|
name = venue.findtext('location/country')
|
|
|
|
),
|
|
|
|
street = venue.findtext('location/street'),
|
|
|
|
postal_code = venue.findtext('location/postalcode'),
|
2009-03-18 00:04:06 +05:30
|
|
|
latitude = (latitude.strip()!= '') and float(latitude) or None,
|
|
|
|
longitude = (longitude.strip()!= '') and float(longitude) or None,
|
|
|
|
),
|
2009-01-01 16:34:31 +05:30
|
|
|
url = venue.findtext('url')
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _hash_func(*args, **kwds):
|
|
|
|
try:
|
|
|
|
return hash(kwds['url'])
|
|
|
|
except KeyError:
|
|
|
|
raise InvalidParametersError("url has to be provided for hashing")
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return self.__class__._hash_func(url = self.url)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.url == other.url
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
return self.name < other.name
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<lastfm.geo.Venue: %s, %s>" % (self.name, self.location.city)
|
|
|
|
|
|
|
|
from lastfm.api import Api
|
|
|
|
from lastfm.event import Event
|
|
|
|
from lastfm.geo import Location, Country
|
|
|
|
from lastfm.error import InvalidParametersError
|