moved get_all method to crawlable mixin. added get_all to venue, location, country. minor changes.

master
Abhinav Sarkar 2009-04-07 04:57:51 +00:00
parent e6d5a21478
commit 2219c5b0ad
14 changed files with 106 additions and 61 deletions

View File

@ -7,9 +7,10 @@ __license__ = "GNU Lesser General Public License"
__package__ = "lastfm"
from lastfm.base import LastfmBase
from lastfm.mixins import cacheable, searchable, taggable
from lastfm.mixins import cacheable, searchable, taggable, crawlable
from lastfm.decorators import cached_property, top_property
@crawlable
@taggable
@searchable
@cacheable
@ -237,15 +238,14 @@ class Album(LastfmBase):
a._fill_info()
return a
@classmethod
def get_all(cls, seed_album):
@staticmethod
def _get_all(seed_album):
def gen():
for artist in Artist.get_all(seed_album.artist):
for album in artist.top_albums:
yield album
return super(Album, cls).get_all(seed_album, ['name', 'artist'],
lambda api, hsh: gen())
return (seed_album, ['name', 'artist'], lambda api, hsh: gen())
def _default_params(self, extra_params = {}):
if not (self.artist and self.name):

View File

@ -7,9 +7,10 @@ __license__ = "GNU Lesser General Public License"
__package__ = "lastfm"
from lastfm.base import LastfmBase
from lastfm.mixins import cacheable, searchable, sharable, shoutable, taggable
from lastfm.mixins import cacheable, searchable, sharable, shoutable, taggable, crawlable
from lastfm.decorators import cached_property, top_property
@crawlable
@shoutable
@sharable
@taggable
@ -370,9 +371,9 @@ class Artist(LastfmBase):
a._fill_info()
return a
@classmethod
def get_all(cls,seed_artist):
return super(Artist, cls).get_all(seed_artist, ['name'],
@staticmethod
def _get_all(seed_artist):
return (seed_artist, ['name'],
lambda api, hsh: Artist(api, **hsh).similar)
def _default_params(self, extra_params = None):

View File

@ -11,27 +11,6 @@ from lastfm.lazylist import lazylist
class LastfmBase(object):
"""Base class for all the classes in this package"""
@classmethod
def get_all(cls, seed, hash_attrs, spider_func):
if cls == LastfmBase:
raise NotImplementedError("the subclass must implement this method")
@lazylist
def gen(lst):
seen = []
api = seed._api
def hash_dict(item):
return dict((a, getattr(item, a)) for a in hash_attrs)
seen.append(hash_dict(seed))
yield seed
for hsh in seen:
for n in spider_func(api, hsh):
if hash_dict(n) not in seen:
seen.append(hash_dict(n))
yield n
return gen()
def __eq__(self, other):
raise NotImplementedError("The subclass must override this method")

View File

@ -5,6 +5,7 @@ __version__ = "0.2"
__license__ = "GNU Lesser General Public License"
__package__ = "lastfm"
from functools import reduce
from lastfm.base import LastfmBase
from lastfm.mixins import cacheable
from operator import xor

View File

@ -159,7 +159,7 @@ def async_callback(func, *args, **kwargs):
from threading import Thread
callback = None
for a in args:
if callable(a):
if hasattr(a, '__call__'):
callback = a
args = list(args)
args.remove(a)
@ -169,7 +169,7 @@ def async_callback(func, *args, **kwargs):
callback = kwargs['callback']
del kwargs['callback']
if callback is not None and callable(callback):
if callback is not None and hasattr(callback, '__call__'):
def async_call():
result = None
try:

View File

@ -7,8 +7,9 @@ __license__ = "GNU Lesser General Public License"
__package__ = "lastfm"
from lastfm.base import LastfmBase
from lastfm.mixins import cacheable, sharable, shoutable
from lastfm.mixins import cacheable, sharable, shoutable, crawlable
@crawlable
@shoutable
@sharable
@cacheable
@ -260,6 +261,7 @@ class Event(LastfmBase):
headliner = Artist(api, name = data.findtext('artists/headliner')),
venue = Venue(
api,
id = int(data.findtext('venue/url').split('/')[-1]),
name = data.findtext('venue/name'),
location = Location(
api,
@ -288,17 +290,15 @@ class Event(LastfmBase):
tag = data.findtext('tag')
)
@classmethod
def get_all(cls, seed_event):
@staticmethod
def _get_all(seed_event):
def gen():
for artist in Artist.get_all(seed_event.artists[0]):
for event in artist.events:
yield event
return super(Event, cls).get_all(seed_event, ['id'],
lambda api, hsh: gen())
return (seed_event, ['id'], lambda api, hsh: gen())
def _default_params(self, extra_params = None):
if not self.id:
raise InvalidParametersError("id has to be provided.")

View File

@ -6,8 +6,9 @@ __version__ = "0.2"
__license__ = "GNU Lesser General Public License"
__package__ = "lastfm"
from functools import reduce
from lastfm.base import LastfmBase
from lastfm.mixins import cacheable
from lastfm.mixins import cacheable, crawlable
from lastfm.decorators import cached_property, top_property, depaginate
class Geo(object):
@ -145,6 +146,7 @@ class Geo(object):
for t in data.findall('track')
]
@crawlable
@cacheable
class Location(LastfmBase):
"""A class representing a location of an event"""
@ -292,6 +294,14 @@ class Location(LastfmBase):
"""
return self.get_events()
@staticmethod
def _get_all(seed_location):
def gen():
for event in Event.get_all(seed_location.events[0]):
yield event.venue.location
return (seed_location, ['city', 'country'], lambda api, hsh: gen())
@staticmethod
def _hash_func(*args, **kwds):
try:
@ -325,6 +335,7 @@ class Location(LastfmBase):
else:
return "<lastfm.geo.Location: %s>" % self.city
@crawlable
@cacheable
class Country(LastfmBase):
"""A class representing a country."""
@ -652,6 +663,11 @@ class Country(LastfmBase):
"""
return Geo.get_events(self._api, self.name)
@staticmethod
def _get_all(seed_country):
return (seed_country, ['name'],
lambda api, hsh: (Country(api, name = c) for c in Country.ISO_CODES.itervalues()))
@staticmethod
def _hash_func(*args, **kwds):
try:

View File

@ -90,7 +90,5 @@ class Group(LastfmBase):
return "<lastfm.Group: %s>" % self.name
from lastfm.api import Api
from lastfm.error import InvalidParametersError, LastfmError
from lastfm.user import User
from lastfm.chart import (WeeklyChart, WeeklyAlbumChart,
WeeklyArtistChart, WeeklyTrackChart, WeeklyTagChart)
from lastfm.error import InvalidParametersError
from lastfm.user import User

View File

@ -11,6 +11,7 @@ from lastfm.mixins._sharable import sharable
from lastfm.mixins._shoutable import shoutable
from lastfm.mixins._taggable import taggable
from lastfm.mixins._chartable import chartable
from lastfm.mixins._crawlable import crawlable
__all__ = ['cacheable', 'searchable', 'sharable', 'shoutable', 'taggable'
'chartable']
'chartable','crawlable']

View File

@ -0,0 +1,38 @@
#!/usr/bin/env python
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
__version__ = "0.2"
__license__ = "GNU Lesser General Public License"
__package__ = "lastfm.mixins"
from lastfm.lazylist import lazylist
def crawlable(cls):
_get_all = cls._get_all
@staticmethod
def get_all(seed):
seed, hash_attrs, spider_func = _get_all(seed)
@lazylist
def gen(lst):
seen = []
api = seed._api
def hash_dict(item):
return dict((a, getattr(item, a)) for a in hash_attrs)
seen.append(hash_dict(seed))
yield seed
for hsh in seen:
for n in spider_func(api, hsh):
if hash_dict(n) not in seen:
seen.append(hash_dict(n))
yield n
return gen()
cls.get_all = get_all
delattr(cls, '_get_all')
if not hasattr(cls, '_mixins'):
cls._mixins = []
cls._mixins.append('get_all')
return cls

View File

@ -6,9 +6,10 @@ __license__ = "GNU Lesser General Public License"
__package__ = "lastfm"
from lastfm.base import LastfmBase
from lastfm.mixins import cacheable, searchable, chartable
from lastfm.mixins import cacheable, searchable, chartable, crawlable
from lastfm.decorators import cached_property, top_property
@crawlable
@chartable(['artist'])
@searchable
@cacheable
@ -193,9 +194,9 @@ class Tag(LastfmBase):
for t in data.findall('tag')
]
@classmethod
def get_all(cls, seed_tag):
return super(Tag, cls).get_all(seed_tag, ['name'],
@staticmethod
def _get_all(seed_tag):
return (seed_tag, ['name'],
lambda api, hsh: Tag(api, **hsh).similar)
def _default_params(self, extra_params = None):
@ -240,8 +241,7 @@ class Tag(LastfmBase):
from lastfm.album import Album
from lastfm.api import Api
from lastfm.artist import Artist
from lastfm.error import LastfmError, InvalidParametersError
from lastfm.error import InvalidParametersError
from lastfm.playlist import Playlist
from lastfm.stats import Stats
from lastfm.track import Track
from lastfm.chart import WeeklyChart, WeeklyArtistChart
from lastfm.track import Track

View File

@ -6,9 +6,10 @@ __license__ = "GNU Lesser General Public License"
__package__ = "lastfm"
from lastfm.base import LastfmBase
from lastfm.mixins import cacheable, searchable, sharable, taggable
from lastfm.mixins import cacheable, searchable, sharable, taggable, crawlable
from lastfm.decorators import cached_property, top_property
@crawlable
@sharable
@taggable
@searchable
@ -273,15 +274,14 @@ class Track(LastfmBase):
t._fill_info()
return t
@classmethod
def get_all(cls, seed_track):
@staticmethod
def _get_all(seed_track):
def gen():
for artist in Artist.get_all(seed_track.artist):
for track in artist.top_tracks:
yield track
return super(Track, cls).get_all(seed_track, ['name', 'artist'],
lambda api, hsh: gen())
return (seed_track, ['name', 'artist'], lambda api, hsh: gen())
def _default_params(self, extra_params = None):
if not (self.artist and self.name):

View File

@ -6,11 +6,12 @@ __license__ = "GNU Lesser General Public License"
__package__ = "lastfm"
from lastfm.base import LastfmBase
from lastfm.mixins import cacheable, shoutable, chartable
from lastfm.mixins import cacheable, shoutable, chartable, crawlable
import lastfm.playlist
from lastfm.decorators import (
cached_property, top_property, authentication_required, depaginate)
@crawlable
@chartable(['album', 'artist', 'track', 'tag'])
@shoutable
@cacheable
@ -522,9 +523,9 @@ class User(LastfmBase):
user._stats = Stats(subject = user, playcount = data.findtext('playcount'))
return user
@classmethod
def get_all(cls,seed_user):
return super(User, cls).get_all(seed_user, ['name'],
@staticmethod
def _get_all(seed_user):
return (seed_user, ['name'],
lambda api, hsh: User(api, **hsh).neighbours)
def _default_params(self, extra_params = None):

View File

@ -6,9 +6,10 @@ __license__ = "GNU Lesser General Public License"
__package__ = "lastfm"
from lastfm.base import LastfmBase
from lastfm.mixins import cacheable, searchable
from lastfm.mixins import cacheable, searchable, crawlable
from lastfm.decorators import cached_property, depaginate
@crawlable
@searchable
@cacheable
class Venue(LastfmBase):
@ -77,6 +78,15 @@ class Venue(LastfmBase):
def past_events(self):
return self.get_past_events()
@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())
def _default_params(self, extra_params = {}):
if not self.id:
raise InvalidParametersError("venue id has to be provided.")