Auto re-auth for discogs plugin upon error 401

This goes in the direction of #1299 and #1305.
This commit is contained in:
Bruno Cauet 2015-02-18 13:30:06 +01:00
parent 7bcd3a383b
commit 457afdc55d

View file

@ -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)