Skip to content

Commit

Permalink
fix potentially ugly death from spotify.
Browse files Browse the repository at this point in the history
  • Loading branch information
itsTheFae committed Jan 31, 2024
1 parent f8e394d commit 9be091a
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions musicbot/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,15 +588,26 @@ async def _get_token(self) -> str:

if self.guest_mode:
token = await self._request_guest_token()
if token is None:
if not token:
raise SpotifyError(
"Failed to get a guest token from Spotify, please try specifying client id and client secret"
)
self._token = {
"access_token": token["accessToken"],
"expires_at": int(token["accessTokenExpirationTimestampMs"]) / 1000,
}
log.debug("Created a new Guest Mode access token.")
try:
self._token = {
"access_token": token["accessToken"],
"expires_at": int(token["accessTokenExpirationTimestampMs"]) / 1000,
}
log.debug("Created a new Guest Mode access token.")
except KeyError as e:
self._token = None
raise SpotifyError(
f"API response did not contain the expected data. Missing: {str(e)}"
) from e
except (ValueError, TypeError) as e:
self._token = None
raise SpotifyError(
f"API response contained unexpected data. {str(e)}"
) from e
else:
token = await self._request_token()
if token is None:
Expand Down Expand Up @@ -629,8 +640,11 @@ async def _request_guest_token(self) -> Dict[str, Any]:
try:
async with self.aiosession.get(self.WEB_TOKEN_URL) as r:
if r.status != 200:
# Note: when status == 429 we could check for "Retry*" headers.
# however, this isn't an API endpoint, so we don't get Retry data.
# TODO: check if changing UA string would work around 429.
raise SpotifyError(
f"Response status is not OK: [{r.status}] {r.reason}"
f"API response status is not OK: [{r.status}] {r.reason}"
)

data = await r.json() # type: Dict[str, Any]
Expand All @@ -643,6 +657,9 @@ async def _request_guest_token(self) -> Dict[str, Any]:
aiohttp.ContentTypeError,
JSONDecodeError,
SpotifyError,
):
log.exception("Failed to get Spotify Guest Token.")
) as e:
if log.getEffectiveLevel() <= logging.DEBUG:
log.exception("Failed to get Spotify Guest Token.")
else:
log.error("Failed to get Guest Token due to: %s", str(e))
return {}

0 comments on commit 9be091a

Please sign in to comment.