Skip to content

Commit

Permalink
narrowcasting: Ignore intermittent ConnectionErrors when Spotify cann…
Browse files Browse the repository at this point in the history
…ot be reached
  • Loading branch information
Kurocon committed Sep 2, 2024
1 parent 9555ca3 commit 6f36f1e
Showing 1 changed file with 54 additions and 25 deletions.
79 changes: 54 additions & 25 deletions amelie/narrowcasting/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import requests
from django.conf import settings
from django.http import JsonResponse, Http404
Expand Down Expand Up @@ -115,13 +117,16 @@ def room_spotify_callback(request):
return_url = reverse('narrowcasting:room_spotify_callback')
return_url = request.build_absolute_uri(return_url)

res = requests.post("https://accounts.spotify.com/api/token", data={
"grant_type": "authorization_code",
"code": auth_code,
"redirect_uri": return_url,
"client_id": settings.SPOTIFY_CLIENT_ID,
"client_secret": settings.SPOTIFY_CLIENT_SECRET
})
try:
res = requests.post("https://accounts.spotify.com/api/token", data={
"grant_type": "authorization_code",
"code": auth_code,
"redirect_uri": return_url,
"client_id": settings.SPOTIFY_CLIENT_ID,
"client_secret": settings.SPOTIFY_CLIENT_SECRET
})
except ConnectionError as e:
raise ValueError(_("ConnectionError while refreshing access token:") + f" {e}")

data = res.json()
try:
Expand All @@ -137,20 +142,23 @@ def room_spotify_callback(request):


def _spotify_refresh_token(association):
res = requests.post("https://accounts.spotify.com/api/token", data={
"grant_type": "refresh_token",
"refresh_token": association.refresh_token,
"client_id": settings.SPOTIFY_CLIENT_ID,
"client_secret": settings.SPOTIFY_CLIENT_SECRET
})
try:
res = requests.post("https://accounts.spotify.com/api/token", data={
"grant_type": "refresh_token",
"refresh_token": association.refresh_token,
"client_id": settings.SPOTIFY_CLIENT_ID,
"client_secret": settings.SPOTIFY_CLIENT_SECRET
})
except ConnectionError as e:
raise ValueError(_("ConnectionError while refreshing access token:") + f" {e}")

data = res.json()

if 'error' in data:
raise ValueError(data['error_description'])
elif res.status_code != 200:
raise ValueError(f"Status code: {res.status_code}")

try:
association.access_token = data['access_token']
association.save()
Expand All @@ -175,10 +183,17 @@ def room_spotify_now_playing(request):
except SpotifyAssociation.DoesNotExist:
raise Http404(_("No association for this identifier"))

res = requests.get("https://api.spotify.com/v1/me/player",
params={"market": "from_token"},
headers={"Authorization": "Bearer {}".format(assoc.access_token)}
)
try:
res = requests.get("https://api.spotify.com/v1/me/player",
params={"market": "from_token"},
headers={"Authorization": "Bearer {}".format(assoc.access_token)}
)
except ConnectionError as e:
log = logging.getLogger("amelie.narrowcasting.views.room_spotify_now_playing")
log.warning(f"ConnectionError while retrieving player info: {e}")
# Return empty response
return JsonResponse({})

if res.status_code == 200:
data = res.json()
data['error'] = False
Expand Down Expand Up @@ -211,9 +226,16 @@ def room_spotify_pause(request):
except SpotifyAssociation.DoesNotExist:
raise Http404(_("No association for this identifier"))

res = requests.put("https://api.spotify.com/v1/me/player/pause",
headers={"Authorization": "Bearer {}".format(assoc.access_token)}
)
try:
res = requests.put("https://api.spotify.com/v1/me/player/pause",
headers={"Authorization": "Bearer {}".format(assoc.access_token)}
)
except ConnectionError as e:
log = logging.getLogger("amelie.narrowcasting.views.room_spotify_now_playing")
log.warning(f"ConnectionError while pausing Spotify player: {e}")
# Return empty response
return JsonResponse({})

if res.status_code == 200:
data = res.json()
data['error'] = False
Expand Down Expand Up @@ -246,9 +268,16 @@ def room_spotify_play(request):
except SpotifyAssociation.DoesNotExist:
raise Http404(_("No association for this identifier"))

res = requests.put("https://api.spotify.com/v1/me/player/play",
headers={"Authorization": "Bearer {}".format(assoc.access_token)}
)
try:
res = requests.put("https://api.spotify.com/v1/me/player/play",
headers={"Authorization": "Bearer {}".format(assoc.access_token)}
)
except ConnectionError as e:
log = logging.getLogger("amelie.narrowcasting.views.room_spotify_now_playing")
log.warning(f"ConnectionError while unpausing Spotify player: {e}")
# Return empty response
return JsonResponse({})

if res.status_code == 200:
data = res.json()
data['error'] = False
Expand Down

0 comments on commit 6f36f1e

Please sign in to comment.