mirror of
https://github.com/beetbox/beets.git
synced 2026-02-22 07:14:24 +01:00
add request_finished function, rename wait_for_rate_limiter to request_start, add doc and changelog
This commit is contained in:
parent
77fd5ee548
commit
9bc3898951
2 changed files with 17 additions and 10 deletions
|
|
@ -73,6 +73,7 @@ class DiscogsPlugin(BeetsPlugin):
|
|||
# Try using a configured user token (bypassing OAuth login).
|
||||
user_token = self.config['user_token'].as_str()
|
||||
if user_token:
|
||||
# rate limit for authenticated users is 60 per minute
|
||||
self.rate_limit_per_minute = 60
|
||||
self.discogs_client = Client(USER_AGENT, user_token=user_token)
|
||||
return
|
||||
|
|
@ -95,16 +96,22 @@ class DiscogsPlugin(BeetsPlugin):
|
|||
seconds_between_requests = 60 / self.rate_limit_per_minute
|
||||
seconds_since_last_request = time.time() - self.last_request_timestamp
|
||||
seconds_to_wait = seconds_between_requests - seconds_since_last_request
|
||||
if seconds_to_wait > 0:
|
||||
return seconds_to_wait
|
||||
return 0
|
||||
return seconds_to_wait
|
||||
|
||||
def wait_for_rate_limiter(self):
|
||||
def request_start(self):
|
||||
"""wait for rate limit if needed
|
||||
"""
|
||||
time_to_next_request = self._time_to_next_request()
|
||||
if time_to_next_request > 0:
|
||||
self._log.debug('hit rate limit, waiting for {0} seconds', time_to_next_request)
|
||||
self._log.debug('hit rate limit, waiting for {0} seconds',
|
||||
time_to_next_request)
|
||||
time.sleep(time_to_next_request)
|
||||
|
||||
def request_finished(self):
|
||||
"""update timestamp for rate limiting
|
||||
"""
|
||||
self.last_request_timestamp = time.time()
|
||||
|
||||
def reset_auth(self):
|
||||
"""Delete token file & redo the auth steps.
|
||||
"""
|
||||
|
|
@ -224,11 +231,10 @@ class DiscogsPlugin(BeetsPlugin):
|
|||
# can also negate an otherwise positive result.
|
||||
query = re.sub(br'(?i)\b(CD|disc)\s*\d+', b'', query)
|
||||
|
||||
self.wait_for_rate_limiter()
|
||||
self.request_start()
|
||||
try:
|
||||
releases = self.discogs_client.search(query,
|
||||
type='release').page(1)
|
||||
self.last_request_timestamp = time.time()
|
||||
|
||||
except CONNECTION_ERRORS:
|
||||
self._log.debug(u"Communication error while searching for {0!r}",
|
||||
|
|
@ -244,10 +250,10 @@ class DiscogsPlugin(BeetsPlugin):
|
|||
self._log.debug(u'Searching for master release {0}', master_id)
|
||||
result = Master(self.discogs_client, {'id': master_id})
|
||||
|
||||
self.wait_for_rate_limiter()
|
||||
self.request_start()
|
||||
try:
|
||||
year = result.fetch('year')
|
||||
self.last_request_timestamp = time.time()
|
||||
self.request_finished()
|
||||
return year
|
||||
except DiscogsAPIError as e:
|
||||
if e.status_code != 404:
|
||||
|
|
|
|||
|
|
@ -65,7 +65,8 @@ New features:
|
|||
:bug:`3123`
|
||||
* :doc:`/plugins/ipfs`: The plugin now supports a ``nocopy`` option which passes that flag to ipfs.
|
||||
Thanks to :user:`wildthyme`.
|
||||
|
||||
* :doc:`/plugins/discogs`: The plugin has rate limiting for the discogs API now.
|
||||
:bug:`3081`
|
||||
|
||||
Changes:
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue