Fixed Spotify auth if not in use, fix GH-87

This commit is contained in:
krateng 2021-11-21 17:14:22 +01:00
parent 2101223440
commit 898dd9735c
2 changed files with 25 additions and 25 deletions

View File

@ -5,7 +5,7 @@ author = {
"email":"maloja@dev.krateng.ch", "email":"maloja@dev.krateng.ch",
"github": "krateng" "github": "krateng"
} }
version = 2,12,16 version = 2,12,17
versionstr = ".".join(str(n) for n in version) versionstr = ".".join(str(n) for n in version)
links = { links = {
"pypi":"malojaserver", "pypi":"malojaserver",

View File

@ -24,27 +24,27 @@ class Spotify(MetadataInterface):
def authorize(self): def authorize(self):
keys = { if self.active_metadata():
"url":"https://accounts.spotify.com/api/token",
"method":"POST", try:
"headers":{ keys = {
"Authorization":"Basic " + b64(utf(self.settings["apiid"] + ":" + self.settings["secret"])).decode("utf-8") "url":"https://accounts.spotify.com/api/token",
}, "method":"POST",
"data":bytes(urllib.parse.urlencode({"grant_type":"client_credentials"}),encoding="utf-8") "headers":{
} "Authorization":"Basic " + b64(utf(self.settings["apiid"] + ":" + self.settings["secret"])).decode("utf-8")
try: },
req = urllib.request.Request(**keys) "data":bytes(urllib.parse.urlencode({"grant_type":"client_credentials"}),encoding="utf-8")
response = urllib.request.urlopen(req) }
responsedata = json.loads(response.read()) req = urllib.request.Request(**keys)
if "error" in responsedata: response = urllib.request.urlopen(req)
log("Error authenticating with Spotify: " + responsedata['error_description']) responsedata = json.loads(response.read())
expire = 3600 if "error" in responsedata:
else: log("Error authenticating with Spotify: " + responsedata['error_description'])
expire = responsedata.get("expires_in",3600) expire = 3600
self.settings["token"] = responsedata["access_token"] else:
log("Successfully authenticated with Spotify") expire = responsedata.get("expires_in",3600)
except Exception as e: self.settings["token"] = responsedata["access_token"]
log("Error while authenticating with Spotify: " + repr(e)) log("Successfully authenticated with Spotify")
expire = 1200 Timer(expire,self.authorize).start()
Timer(expire,self.authorize).start() except Exception as e:
return True log("Error while authenticating with Spotify: " + repr(e))