def authenticate(self, c_key, c_secret): # Get the link for the OAuth page. auth_client = Client(USER_AGENT, c_key, c_secret) try: _, _, url = auth_client.get_authorize_url() except CONNECTION_ERRORS as e: self._log.debug(u'connection error: {0}', e) raise beets.ui.UserError(u'communication with Discogs failed') beets.ui.print_(u"To authenticate with Discogs, visit:") beets.ui.print_(url) # Ask for the code and validate it. code = beets.ui.input_(u"Enter the code:") try: token, secret = auth_client.get_access_token(code) except DiscogsAPIError: raise beets.ui.UserError(u'Discogs authorization failed') except CONNECTION_ERRORS as e: self._log.debug(u'connection error: {0}', e) raise beets.ui.UserError(u'Discogs token request failed') # Save the token for later use. self._log.debug(u'Discogs token {0}, secret {1}', token, secret) with open(self._tokenfile(), 'w') as f: json.dump({'token': token, 'secret': secret}, f) return token, secret
def authenticate(self, c_key, c_secret): # Get the link for the OAuth page. auth_client = Client(USER_AGENT, c_key, c_secret) _, _, url = auth_client.get_authorize_url() beets.ui.print_("To authenticate with Discogs, visit:") beets.ui.print_(url) # Ask for the code and validate it. code = beets.ui.input_("Enter the code:") try: token, secret = auth_client.get_access_token(code) except DiscogsAPIError: raise beets.ui.UserError('Discogs authorization failed') # Save the token for later use. log.debug('Discogs token {0}, secret {1}'.format(token, secret)) with open(self._tokenfile(), 'w') as f: json.dump({'token': token, 'secret': secret}, f) return token, secret
class Discogs: """ OAuth authentication wrapper for discogs API with 'search' method to search for cover images. """ def __init__(self): self.consumer_key = 'pDGCsGkOvnpeoypQDyMp' self.consumer_secret = 'MXrkZgMMofvlTBVMWwGIYIgdfjXWXhvM' if self.is_authenticated(): from access_tokens import access_token, access_secret self.discogs = Client(USER_AGENT, consumer_key=self.consumer_key, consumer_secret=self.consumer_secret, token=access_token, secret=access_secret) # If user does not have access token and secret we need to use OAuth else: self.discogs = Client(USER_AGENT) self.get_request_token() def get_request_token(self): self.discogs.set_consumer_key(self.consumer_key, self.consumer_secret) token, secret, url = self.discogs.get_authorize_url() auth = False while not auth: print('in order to access your access images from discogs you') print('need to verify your Discogs account using OAuth.') print(f'please visit {url} and accept the authentication request') oauth_code = input('Verification code: ') try: token, secret = self.discogs.get_access_token(oauth_code) except Exception as e: print(f'Unable to authenticate, please try again. error="{e}"') continue if token: auth = True with open('access_tokens.py', 'w') as f: f.write('access token: {token}') f.write('access secret: {secret}') def is_authenticated(self): """ Returns True if token exists in local file """ if os.path.isfile('access_tokens.py'): return True def search(self, params): """ takes search params as param, col dict and returns image & image id """ url = 'https://api.discogs.com/database/search?' for key, value in params.items(): url += key + '=' + value + '&' url += 'key=' + self.consumer_key url += '&secret=' + self.consumer_secret response = requests.get(url).text data = json.loads(response) try: item = data['results'][0] except IndexError: print(f"Nothing found for {params['artist']}") return None time.sleep(1.1) cover_url = item['cover_image'] cover_id = str(item['id']) response = requests.get(cover_url, stream=True) if response.status_code == 200: print(f"Downloaded image for {params['artist']}, id:{cover_id}") return response, cover_id else: print(f"Error getting {params['artist']}: {response.status_code}")