creazione primi endpoint

This commit is contained in:
AnouarElKihal 2025-08-02 05:04:53 +02:00
parent 51f507811c
commit 564a2a4b9a
2 changed files with 34 additions and 2 deletions

View file

@ -49,9 +49,13 @@ class SessionData():
except requests.exceptions.RequestException as e:
print(f"Errore durante il refresh del token: {e}")
raise e
@property
def access_tokens(self):
if self.nearly_expired():
self.refresh()
return self.__access_token

View file

@ -11,7 +11,7 @@ SPOTIFY_CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRET")
SPOTIFY_REDIRECT_URI = os.getenv("HOST") + "/music/callback"
SPOTIFY_AUTH_URL = "https://accounts.spotify.com/authorize"
SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token"
SPOTIFY_PLAY_URL = "https://api.spotify.com/v1/me/player/play"
SPOTIFY_PLAY_URL = "https://api.spotify.com/v1/"
# Step 1: Redirect user to Spotify login
@music_router.get("/login")
@ -48,4 +48,32 @@ def callback(code: str):
@music_router.get("/search")
def search(query: str):
pass
try:
url = SPOTIFY_PLAY_URL + f"search?q={query}&type={["track"]}"
header = {
"Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens,
"Content-Type": "application/json"
}
return requests.get(url, headers=header)
except Exception as e:
print(e)
@music_router.get("/play")
def play(song_id: str):
url = SPOTIFY_PLAY_URL + "me/player/play"
body = { "uris": [f"spotify:track:{song_id}"] }
header = {
"Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens,
"Content-Type": "application/json"
}
requests.put(url, data=body, headers=header)
@music_router.get("/current-song")
def current_song():
url = SPOTIFY_PLAY_URL + f"me/player/currently-playing?marketing=IT"
header = {
"Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens,
"Content-Type": "application/json"
}
return requests.get(url, headers=header)