python-lastfm/src/geo.py

310 lines
9.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.1"
__license__ = "GNU Lesser General Public License"
2008-07-15 00:38:39 +05:30
from base import LastfmBase
class Geo(object):
"""A class representing an geographic location."""
2008-07-10 19:34:42 +05:30
@staticmethod
def getEvents(api,
location,
distance = None,
page = None):
params = {'method': 'geo.getevents', 'location': location}
if distance is not None:
params.update({'distance': distance})
if page is not None:
params.update({'page': page})
2008-07-25 16:29:48 +05:30
data = api._fetchData(params).find('events')
return SearchResult(
type = 'event',
searchTerms = data.attrib['location'],
startPage = int(data.attrib['page']),
totalResults = int(data.attrib['total']),
itemsPerPage = int(math.ceil(float(data.attrib['total']))/float(data.attrib['totalpages'])),
matches = [
Event.createFromData(api, e)
for e in data.findall('event')
]
)
2008-07-10 19:34:42 +05:30
@staticmethod
def getTopArtists(api, country):
params = {'method': 'geo.gettopartists', 'country': country}
2008-07-25 16:29:48 +05:30
data = api._fetchData(params).find('topartists')
return [
Artist(
api,
name = a.findtext('name'),
mbid = a.findtext('mbid'),
stats = Stats(
subject = a.findtext('name'),
rank = int(a.attrib['rank']),
playcount = int(a.findtext('playcount'))
),
url = 'http://' + a.findtext('url'),
image = {'large': a.findtext('image')}
)
for a in data.findall('artist')
]
2008-07-10 19:34:42 +05:30
@staticmethod
def getTopTracks(api, country):
params = {'method': 'geo.gettoptracks', 'country': country}
2008-07-25 16:29:48 +05:30
data = api._fetchData(params).find('toptracks')
return [
Track(
api,
name = t.findtext('name'),
mbid = t.findtext('mbid'),
artist = Artist(
api,
name = t.findtext('artist/name'),
mbid = t.findtext('artist/mbid'),
url = t.findtext('artist/url')
),
stats = Stats(
subject = t.findtext('name'),
rank = int(t.attrib['rank']),
playcount = int(t.findtext('playcount'))
),
streamable = (t.findtext('streamable') == '1'),
fullTrack = (t.find('streamable').attrib['fulltrack'] == '1'),
url = 'http://' + t.findtext('url'),
image = {'large': t.findtext('image')}
)
for t in data.findall('track')
]
2008-07-15 00:38:39 +05:30
class Venue(LastfmBase):
"""A class representing a venue of an event"""
2008-07-15 00:38:39 +05:30
def init(self,
name = None,
location = None,
url = None):
self.__name = name
2008-07-10 19:34:42 +05:30
self.__location = location
self.__url = url
@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
2008-07-15 00:38:39 +05:30
@staticmethod
def hashFunc(*args, **kwds):
try:
return hash(kwds['url'])
except KeyError:
raise LastfmError("url has to be provided for hashing")
2008-07-15 00:38:39 +05:30
def __hash__(self):
return self.__class__.hashFunc(url = self.url)
def __eq__(self, other):
2008-07-15 00:38:39 +05:30
return self.url == other.url
2008-07-15 00:38:39 +05:30
def __lt__(self, other):
return self.name < other.name
2008-07-15 00:38:39 +05:30
def __repr__(self):
return "<lastfm.geo.Venue: %s, %s>" % (self.name, self.location.city)
2008-07-15 00:38:39 +05:30
class Location(LastfmBase):
"""A class representing a location of an event"""
xmlns = "http://www.w3.org/2003/01/geo/wgs84_pos#"
2008-07-15 00:38:39 +05:30
def init(self,
2008-07-10 19:34:42 +05:30
api,
name = None,
city = None,
country = None,
street = None,
postalCode = None,
latitude = None,
longitude = None,
timezone = None):
2008-07-16 18:36:42 +05:30
if not isinstance(api, Api):
raise LastfmError("api reference must be supplied as an argument")
2008-07-10 19:34:42 +05:30
self.__api = api
self.__name = name
self.__city = city
self.__country = country
self.__street = street
self.__postalCode = postalCode
self.__latitude = latitude
self.__longitude = longitude
self.__timezone = timezone
self.__events = None
@property
def name(self):
"""name of the location"""
2008-07-15 17:29:49 +05:30
return self.__name
@property
def city(self):
"""city in which the location is situated"""
return self.__city
@property
def country(self):
"""country in which the location is situated"""
return self.__country
@property
def street(self):
"""street in which the location is situated"""
return self.__street
@property
def postalCode(self):
"""postal code of the location"""
return self.__postalCode
@property
def latitude(self):
"""latitude of the location"""
return self.__latitude
@property
def longitude(self):
"""longitude of the location"""
return self.__longitude
@property
def timezone(self):
"""timezone in which the location is situated"""
return self.__timezone
2008-07-10 19:34:42 +05:30
def getEvents(self,
distance = None,
page = None):
return Geo.getEvents(self.__api, self.name, distance, page).matches
@property
def events(self):
"""events taking place at/around the location"""
if self.__events is None:
self.__events = self.getEvents()
return self.__events
2008-07-15 00:38:39 +05:30
@staticmethod
def hashFunc(*args, **kwds):
try:
2008-07-15 17:29:49 +05:30
return hash("latlong%s%s" % (kwds['latitude'], kwds['longitude']))
2008-07-15 00:38:39 +05:30
except KeyError:
2008-07-15 17:29:49 +05:30
try:
return hash("name%s" % kwds['name'])
except KeyError:
raise LastfmError("either latitude and longitude or name has to be provided for hashing")
2008-07-15 00:38:39 +05:30
def __hash__(self):
2008-07-15 17:29:49 +05:30
if not self.name:
return self.__class__.hashFunc(
latitude = self.latitude,
longitude = self.longitude)
else:
return self.__class__.hashFunc(name = self.name)
2008-07-10 19:34:42 +05:30
def __eq__(self, other):
return self.latitude == other.latitude and self.longitude == other.longitude
2008-07-15 00:38:39 +05:30
def __lt__(self, other):
if self.country != other.country:
return self.country < other.country
else:
return self.city < other.city
2008-07-15 00:38:39 +05:30
def __repr__(self):
2008-07-15 17:29:49 +05:30
if self.name is None:
return "<lastfm.geo.Location: (%s, %s)>" % (self.latitude, self.longitude)
else:
return "<lastfm.geo.Location: %s>" % self.name
2008-07-15 00:38:39 +05:30
class Country(LastfmBase):
2008-07-10 19:34:42 +05:30
"""A class representing a country."""
2008-07-15 00:38:39 +05:30
def init(self,
2008-07-10 19:34:42 +05:30
api,
name = None):
2008-07-16 18:36:42 +05:30
if not isinstance(api, Api):
raise LastfmError("api reference must be supplied as an argument")
2008-07-10 19:34:42 +05:30
self.__api = api
self.__name = name
self.__topArtists = None
self.__topTracks = None
2008-07-10 19:34:42 +05:30
@property
def name(self):
"""name of the country"""
2008-07-10 19:34:42 +05:30
return self.__name
@property
def topArtists(self):
"""top artists of the country"""
if self.__topArtists is None:
self.__topArtists = Geo.getTopArtists(self.__api, self.name)
return self.__topArtists
@LastfmBase.topProperty("topArtists")
def topArtist(self):
"""top artist of the country"""
pass
@property
def topTracks(self):
"""top tracks of the country"""
if self.__topTracks is None:
self.__topTracks = Geo.getTopTracks(self.__api, self.name)
return self.__topTracks
@LastfmBase.topProperty("topTracks")
def topTrack(self):
"""top track of the country"""
pass
2008-07-15 00:38:39 +05:30
@staticmethod
def hashFunc(*args, **kwds):
try:
return hash(kwds['name'])
except KeyError:
raise LastfmError("name has to be provided for hashing")
2008-07-15 00:38:39 +05:30
def __hash__(self):
return self.__class__.hashFunc(name = self.name)
def __eq__(self, other):
2008-07-10 22:23:43 +05:30
return self.name == other.name
2008-07-15 00:38:39 +05:30
def __lt__(self, other):
return self.name < other.name
2008-07-15 00:38:39 +05:30
def __repr__(self):
return "<lastfm.geo.Country: %s>" % self.name
import math
2008-07-16 18:36:42 +05:30
from api import Api
2008-07-10 22:23:43 +05:30
from artist import Artist
from error import LastfmError
from event import Event
from search import SearchResult
from stats import Stats
from track import Track