diff --git a/lastfm/__init__.py b/lastfm/__init__.py index edabba8..bb84046 100644 --- a/lastfm/__init__.py +++ b/lastfm/__init__.py @@ -13,7 +13,7 @@ __package__ = "lastfm" import sys 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.api import Api diff --git a/lastfm/api.py b/lastfm/api.py index bec7b84..ffb6177 100644 --- a/lastfm/api.py +++ b/lastfm/api.py @@ -64,6 +64,7 @@ class Api(object): self._no_cache = no_cache self._debug = debug self._last_fetch_time = datetime.now() + self._lock = Lock() @property 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]) def _read_url_data(self, opener, url, data = None): - now = datetime.now() - delta = now - self._last_fetch_time - delta = delta.seconds + float(delta.microseconds)/1000000 - if delta < Api.FETCH_INTERVAL: - time.sleep(Api.FETCH_INTERVAL - delta) - url_data = opener.open(url, data).read() - self._last_fetch_time = datetime.now() + with self._lock: + now = datetime.now() + delta = now - self._last_fetch_time + delta = delta.seconds + float(delta.microseconds)/1000000 + if delta < Api.FETCH_INTERVAL: + time.sleep(Api.FETCH_INTERVAL - delta) + url_data = opener.open(url, data).read() + self._last_fetch_time = datetime.now() return url_data def _fetch_url(self, @@ -765,6 +767,7 @@ class Api(object): return "" % self._api_key from datetime import datetime +from threading import Lock import sys import time import urllib diff --git a/lastfm/decorators.py b/lastfm/decorators.py index 2997586..9da9aa3 100644 --- a/lastfm/decorators.py +++ b/lastfm/decorators.py @@ -60,7 +60,7 @@ def cached_property(func): return property(fget = wrapper, doc = func.__doc__) @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 on the functions that need authentication. If not authenticated then an diff --git a/lastfm/mixins/taggable.py b/lastfm/mixins/taggable.py index f23a313..a8aab89 100644 --- a/lastfm/mixins/taggable.py +++ b/lastfm/mixins/taggable.py @@ -6,14 +6,14 @@ __license__ = "GNU Lesser General Public License" __package__ = "lastfm.mixins" from lastfm.safelist import SafeList -from lastfm.decorators import cached_property, authenticate +from lastfm.decorators import cached_property, authentication_required class Taggable(object): def init(self, api): self._api = api @cached_property - @authenticate + @authentication_required def tags(self): from lastfm.tag import Tag params = self._default_params({'method': '%s.getTags' % self.__class__.__name__.lower()}) @@ -28,7 +28,7 @@ class Taggable(object): ], self.add_tags, self.remove_tag) - @authenticate + @authentication_required def add_tags(self, tags): from lastfm.tag import Tag while(len(tags) > 10): @@ -52,7 +52,7 @@ class Taggable(object): self._api._post_data(params) self._tags = None - @authenticate + @authentication_required def remove_tag(self, tag): from lastfm.tag import Tag if isinstance(tag, Tag):