minor changes

master
Abhinav Sarkar 2009-03-27 05:48:34 +00:00
parent 5a4296fa73
commit 36b2aea1fd
4 changed files with 16 additions and 13 deletions

View File

@ -13,7 +13,7 @@ __package__ = "lastfm"
import sys import sys
import os import os
sys.path.append(os.path.dirname(os.path.dirname(__file__))) sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from lastfm.album import Album from lastfm.album import Album
from lastfm.api import Api from lastfm.api import Api

View File

@ -64,6 +64,7 @@ class Api(object):
self._no_cache = no_cache self._no_cache = no_cache
self._debug = debug self._debug = debug
self._last_fetch_time = datetime.now() self._last_fetch_time = datetime.now()
self._lock = Lock()
@property @property
def api_key(self): def api_key(self):
@ -643,13 +644,14 @@ class Api(object):
return urllib.urlencode([(k, self._encode(parameters[k])) for k in keys if parameters[k] is not None]) return urllib.urlencode([(k, self._encode(parameters[k])) for k in keys if parameters[k] is not None])
def _read_url_data(self, opener, url, data = None): def _read_url_data(self, opener, url, data = None):
now = datetime.now() with self._lock:
delta = now - self._last_fetch_time now = datetime.now()
delta = delta.seconds + float(delta.microseconds)/1000000 delta = now - self._last_fetch_time
if delta < Api.FETCH_INTERVAL: delta = delta.seconds + float(delta.microseconds)/1000000
time.sleep(Api.FETCH_INTERVAL - delta) if delta < Api.FETCH_INTERVAL:
url_data = opener.open(url, data).read() time.sleep(Api.FETCH_INTERVAL - delta)
self._last_fetch_time = datetime.now() url_data = opener.open(url, data).read()
self._last_fetch_time = datetime.now()
return url_data return url_data
def _fetch_url(self, def _fetch_url(self,
@ -765,6 +767,7 @@ class Api(object):
return "<lastfm.Api: %s>" % self._api_key return "<lastfm.Api: %s>" % self._api_key
from datetime import datetime from datetime import datetime
from threading import Lock
import sys import sys
import time import time
import urllib import urllib

View File

@ -60,7 +60,7 @@ def cached_property(func):
return property(fget = wrapper, doc = func.__doc__) return property(fget = wrapper, doc = func.__doc__)
@decorator @decorator
def authenticate(func, *args, **kwargs): def authentication_required(func, *args, **kwargs):
""" """
A decorator to check if the current user is authenticated or not. Used only A decorator to check if the current user is authenticated or not. Used only
on the functions that need authentication. If not authenticated then an on the functions that need authentication. If not authenticated then an

View File

@ -6,14 +6,14 @@ __license__ = "GNU Lesser General Public License"
__package__ = "lastfm.mixins" __package__ = "lastfm.mixins"
from lastfm.safelist import SafeList from lastfm.safelist import SafeList
from lastfm.decorators import cached_property, authenticate from lastfm.decorators import cached_property, authentication_required
class Taggable(object): class Taggable(object):
def init(self, api): def init(self, api):
self._api = api self._api = api
@cached_property @cached_property
@authenticate @authentication_required
def tags(self): def tags(self):
from lastfm.tag import Tag from lastfm.tag import Tag
params = self._default_params({'method': '%s.getTags' % self.__class__.__name__.lower()}) params = self._default_params({'method': '%s.getTags' % self.__class__.__name__.lower()})
@ -28,7 +28,7 @@ class Taggable(object):
], ],
self.add_tags, self.remove_tag) self.add_tags, self.remove_tag)
@authenticate @authentication_required
def add_tags(self, tags): def add_tags(self, tags):
from lastfm.tag import Tag from lastfm.tag import Tag
while(len(tags) > 10): while(len(tags) > 10):
@ -52,7 +52,7 @@ class Taggable(object):
self._api._post_data(params) self._api._post_data(params)
self._tags = None self._tags = None
@authenticate @authentication_required
def remove_tag(self, tag): def remove_tag(self, tag):
from lastfm.tag import Tag from lastfm.tag import Tag
if isinstance(tag, Tag): if isinstance(tag, Tag):