From 898dd9735c8812661b3c08ead4b2b5ff288b70fb Mon Sep 17 00:00:00 2001 From: krateng Date: Sun, 21 Nov 2021 17:14:22 +0100 Subject: [PATCH] Fixed Spotify auth if not in use, fix GH-87 --- maloja/__pkginfo__.py | 2 +- maloja/thirdparty/spotify.py | 48 ++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/maloja/__pkginfo__.py b/maloja/__pkginfo__.py index b2ffa67..9a61208 100644 --- a/maloja/__pkginfo__.py +++ b/maloja/__pkginfo__.py @@ -5,7 +5,7 @@ author = { "email":"maloja@dev.krateng.ch", "github": "krateng" } -version = 2,12,16 +version = 2,12,17 versionstr = ".".join(str(n) for n in version) links = { "pypi":"malojaserver", diff --git a/maloja/thirdparty/spotify.py b/maloja/thirdparty/spotify.py index 0d7f951..8d50284 100644 --- a/maloja/thirdparty/spotify.py +++ b/maloja/thirdparty/spotify.py @@ -24,27 +24,27 @@ class Spotify(MetadataInterface): def authorize(self): - keys = { - "url":"https://accounts.spotify.com/api/token", - "method":"POST", - "headers":{ - "Authorization":"Basic " + b64(utf(self.settings["apiid"] + ":" + self.settings["secret"])).decode("utf-8") - }, - "data":bytes(urllib.parse.urlencode({"grant_type":"client_credentials"}),encoding="utf-8") - } - try: - req = urllib.request.Request(**keys) - response = urllib.request.urlopen(req) - responsedata = json.loads(response.read()) - if "error" in responsedata: - log("Error authenticating with Spotify: " + responsedata['error_description']) - expire = 3600 - else: - expire = responsedata.get("expires_in",3600) - self.settings["token"] = responsedata["access_token"] - log("Successfully authenticated with Spotify") - except Exception as e: - log("Error while authenticating with Spotify: " + repr(e)) - expire = 1200 - Timer(expire,self.authorize).start() - return True + if self.active_metadata(): + + try: + keys = { + "url":"https://accounts.spotify.com/api/token", + "method":"POST", + "headers":{ + "Authorization":"Basic " + b64(utf(self.settings["apiid"] + ":" + self.settings["secret"])).decode("utf-8") + }, + "data":bytes(urllib.parse.urlencode({"grant_type":"client_credentials"}),encoding="utf-8") + } + req = urllib.request.Request(**keys) + response = urllib.request.urlopen(req) + responsedata = json.loads(response.read()) + if "error" in responsedata: + log("Error authenticating with Spotify: " + responsedata['error_description']) + expire = 3600 + else: + expire = responsedata.get("expires_in",3600) + self.settings["token"] = responsedata["access_token"] + log("Successfully authenticated with Spotify") + Timer(expire,self.authorize).start() + except Exception as e: + log("Error while authenticating with Spotify: " + repr(e))