Skip to content

Commit

Permalink
Merge pull request #4118 from maffo999/master
Browse files Browse the repository at this point in the history
Fix for 4108
  • Loading branch information
sampsyo committed Oct 22, 2021
2 parents 41fc611 + ad4fb61 commit b116eb5
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 28 deletions.
27 changes: 0 additions & 27 deletions beetsplug/discogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ def __init__(self):
self.config['user_token'].redact = True
self.discogs_client = None
self.register_listener('import_begin', self.setup)
self.rate_limit_per_minute = 25
self.last_request_timestamp = 0

def setup(self, session=None):
"""Create the `discogs_client` field. Authenticate if necessary.
Expand All @@ -77,7 +75,6 @@ def setup(self, session=None):
if user_token:
# The rate limit for authenticated users goes up to 60
# requests per minute.
self.rate_limit_per_minute = 60
self.discogs_client = Client(USER_AGENT, user_token=user_token)
return

Expand All @@ -95,26 +92,6 @@ def setup(self, session=None):
self.discogs_client = Client(USER_AGENT, c_key, c_secret,
token, secret)

def _time_to_next_request(self):
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
return seconds_to_wait

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)
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.
"""
Expand Down Expand Up @@ -266,11 +243,9 @@ def get_albums(self, query):
# can also negate an otherwise positive result.
query = re.sub(r'(?i)\b(CD|disc)\s*\d+', '', query)

self.request_start()
try:
releases = self.discogs_client.search(query,
type='release').page(1)
self.request_finished()

except CONNECTION_ERRORS:
self._log.debug("Communication error while searching for {0!r}",
Expand All @@ -286,10 +261,8 @@ def get_master_year(self, master_id):
self._log.debug('Searching for master release {0}', master_id)
result = Master(self.discogs_client, {'id': master_id})

self.request_start()
try:
year = result.fetch('year')
self.request_finished()
return year
except DiscogsAPIError as e:
if e.status_code != 404:
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Bug fixes:
* :doc:`/plugins/discogs`: Adapt regex to new URL format .
:bug: `4080`

* :doc:`/plugins/discogs`: Remove requests ratel imit code from plugin in favor of discogs library built-in capability
:bug: `4108`

1.5.0 (August 19, 2021)
-----------------------

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def build_manpages():
'embyupdate': ['requests'],
'chroma': ['pyacoustid'],
'gmusic': ['gmusicapi'],
'discogs': ['python3-discogs-client'],
'discogs': ['python3-discogs-client>=2.3.10'],
'beatport': ['requests-oauthlib>=0.6.1'],
'kodiupdate': ['requests'],
'lastgenre': ['pylast'],
Expand Down

0 comments on commit b116eb5

Please sign in to comment.