diff --git a/beetsplug/discogs.py b/beetsplug/discogs.py index a8b02f349..ac0f516ff 100644 --- a/beetsplug/discogs.py +++ b/beetsplug/discogs.py @@ -32,6 +32,7 @@ import time import json import socket import httplib +import os # Silence spurious INFO log lines generated by urllib3. @@ -78,6 +79,12 @@ class DiscogsPlugin(BeetsPlugin): self.discogs_client = Client(USER_AGENT, c_key, c_secret, token, secret) + def reset_auth(self): + """Delete toke file & redo the auth steps. + """ + os.remove(self._tokenfile()) + self.setup() + def _tokenfile(self): """Get the path to the JSON file for storing the OAuth token. """ @@ -130,7 +137,11 @@ class DiscogsPlugin(BeetsPlugin): return self.get_albums(query) except DiscogsAPIError as e: self._log.debug(u'API Error: {0} (query: {1})', e, query) - return [] + if e.status_code == 401: + self.reset_auth() + return self.candidates(items, artist, album, va_likely) + else: + return [] except CONNECTION_ERRORS as e: self._log.debug(u'HTTP Connection Error: {0}', e) return [] @@ -156,8 +167,11 @@ class DiscogsPlugin(BeetsPlugin): try: getattr(result, 'title') except DiscogsAPIError as e: - if e.message != '404 Not Found': + if e.status_code != 404: self._log.debug(u'API Error: {0} (query: {1})', e, result._uri) + if e.status_code == 401: + self.reset_auth() + return self.album_for_id(album_id) return None except CONNECTION_ERRORS as e: self._log.debug(u'HTTP Connection Error: {0}', e)