Correctly compare current time and token expire time and actually cache the

token (spotify.py)
This commit is contained in:
jesopo 2019-01-29 22:46:00 +00:00
parent d178938463
commit 9491a44295

View file

@ -13,7 +13,7 @@ class Module(ModuleManager.BaseModule):
self._token_expires = None self._token_expires = None
def _get_token(self): def _get_token(self):
if self._token and (self._token_expires+10) < time.time(): if self._token and time.time() < (self._token_expires+10):
return self._token return self._token
else: else:
client_id = self.bot.config["spotify-client-id"] client_id = self.bot.config["spotify-client-id"]
@ -25,8 +25,11 @@ class Module(ModuleManager.BaseModule):
headers={"Authorization": "Basic %s" % bearer}, headers={"Authorization": "Basic %s" % bearer},
post_data={"grant_type": "client_credentials"}, post_data={"grant_type": "client_credentials"},
json=True) json=True)
self._token_expire = time.time()+page.data["expires_in"]
return page.data["access_token"] token = page.data["access_token"]
self._token = token
self._token_expires = time.time()+page.data["expires_in"]
return token
@utils.hook("received.command.spotify", min_args=1) @utils.hook("received.command.spotify", min_args=1)
def spotify(self, event): def spotify(self, event):