fixed play endpoint

This commit is contained in:
Matteo Baldi 2025-08-02 05:58:43 +02:00
parent 5214665a58
commit 01b7fde48e

View file

@ -1,8 +1,9 @@
from fastapi import APIRouter, Request, Depends
from fastapi.responses import RedirectResponse
import requests
import os import os
import requests
from auth.session import SessionData, SessionManager from auth.session import SessionData, SessionManager
from fastapi import APIRouter
from fastapi.responses import RedirectResponse
music_router = APIRouter(prefix="/music") music_router = APIRouter(prefix="/music")
@ -13,6 +14,7 @@ SPOTIFY_AUTH_URL = "https://accounts.spotify.com/authorize"
SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token" SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token"
SPOTIFY_PLAY_URL = "https://api.spotify.com/v1/" SPOTIFY_PLAY_URL = "https://api.spotify.com/v1/"
# Step 1: Redirect user to Spotify login # Step 1: Redirect user to Spotify login
@music_router.get("/login") @music_router.get("/login")
def login(): def login():
@ -25,6 +27,7 @@ def login():
) )
return RedirectResponse(url) return RedirectResponse(url)
# Step 2: Callback to get access token # Step 2: Callback to get access token
@music_router.get("/callback") @music_router.get("/callback")
def callback(code: str): def callback(code: str):
@ -46,6 +49,7 @@ def callback(code: str):
return {} return {}
@music_router.get("/search") @music_router.get("/search")
def search(query: str): def search(query: str):
try: try:
@ -60,20 +64,27 @@ def search(query: str):
except Exception as e: except Exception as e:
print(e) print(e)
@music_router.get("/play") @music_router.get("/play")
def play(song_id: str): def play(song_id: str):
try: try:
url = SPOTIFY_PLAY_URL + "me/player/play" url = SPOTIFY_PLAY_URL + "me/player/play"
body = { "uris": [f"spotify:track:{song_id}"] } body = {
"uris": [f"spotify:track:{song_id}"],
"position_ms": 0
}
header = { header = {
"Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens, "Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens,
"Content-Type": "application/json" "Content-Type": "application/json"
} }
response = requests.put(url, data=body, headers=header) print(body)
response = requests.put(url,json=body, headers=header)
return response.json() return response.json()
except Exception as e: except Exception as e:
print(e) print(e)
@music_router.get("/current-song") @music_router.get("/current-song")
def current_song(): def current_song():
try: try: