fixed add track to queue
This commit is contained in:
parent
dec89ef9de
commit
f3c868a050
3 changed files with 21 additions and 17 deletions
|
@ -52,7 +52,7 @@ class SessionData():
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def access_tokens(self):
|
def access_token(self):
|
||||||
if self.nearly_expired():
|
if self.nearly_expired():
|
||||||
self.refresh()
|
self.refresh()
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,7 @@ class QueueItem(BaseModel):
|
||||||
voti: int = 0
|
voti: int = 0
|
||||||
|
|
||||||
class QueueItemCreate(BaseModel):
|
class QueueItemCreate(BaseModel):
|
||||||
|
id: str
|
||||||
titolo: str
|
titolo: str
|
||||||
coverUrl: str
|
coverUrl: str
|
||||||
color: str
|
color: str
|
||||||
|
@ -42,12 +43,8 @@ class VoteUpdate(BaseModel):
|
||||||
async def add_item(item: QueueItemCreate):
|
async def add_item(item: QueueItemCreate):
|
||||||
"""POST: Aggiunge un nuovo item alla queue"""
|
"""POST: Aggiunge un nuovo item alla queue"""
|
||||||
try:
|
try:
|
||||||
# Genera un ID univoco
|
|
||||||
import uuid
|
|
||||||
item_id = str(uuid.uuid4())
|
|
||||||
|
|
||||||
new_item = {
|
new_item = {
|
||||||
"id": item_id,
|
"id": item.id,
|
||||||
"titolo": item.titolo,
|
"titolo": item.titolo,
|
||||||
"coverUrl": item.coverUrl,
|
"coverUrl": item.coverUrl,
|
||||||
"color": item.color,
|
"color": item.color,
|
||||||
|
|
|
@ -7,6 +7,8 @@ from auth.session import SessionData, SessionManager
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from fastapi.responses import RedirectResponse
|
from fastapi.responses import RedirectResponse
|
||||||
|
|
||||||
|
from backend.endpoints.queue import delete_item
|
||||||
|
|
||||||
music_router = APIRouter(prefix="/music")
|
music_router = APIRouter(prefix="/music")
|
||||||
|
|
||||||
SPOTIFY_CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID")
|
SPOTIFY_CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID")
|
||||||
|
@ -46,6 +48,9 @@ def callback(code: str):
|
||||||
refresh_token = token_info.get("refresh_token")
|
refresh_token = token_info.get("refresh_token")
|
||||||
expires_in = token_info.get("expires_in")
|
expires_in = token_info.get("expires_in")
|
||||||
|
|
||||||
|
if any(token is None for token in [access_token, refresh_token, expires_in]):
|
||||||
|
return {"error": "Failed to obtain access token from Spotify."}
|
||||||
|
|
||||||
# Salva access_token in sessione o database per usi futuri
|
# Salva access_token in sessione o database per usi futuri
|
||||||
old_session = SessionManager.instance().get_current_session()
|
old_session = SessionManager.instance().get_current_session()
|
||||||
SessionManager.instance().set_session(SessionData(access_token, refresh_token, expires_in))
|
SessionManager.instance().set_session(SessionData(access_token, refresh_token, expires_in))
|
||||||
|
@ -53,7 +58,7 @@ def callback(code: str):
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"changed": session != old_session,
|
"changed": session != old_session,
|
||||||
"access_token": session.access_tokens,
|
"access_token": session.access_token,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -62,7 +67,7 @@ def search(query: str):
|
||||||
try:
|
try:
|
||||||
url = SPOTIFY_PLAY_URL + f"search?q={query}&type=track"
|
url = SPOTIFY_PLAY_URL + f"search?q={query}&type=track"
|
||||||
header = {
|
header = {
|
||||||
"Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens,
|
"Authorization": "Bearer " + SessionManager.instance().get_current_session().access_token,
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
response = requests.get(url, headers=header)
|
response = requests.get(url, headers=header)
|
||||||
|
@ -81,7 +86,7 @@ def play(song_id: str):
|
||||||
"position_ms": 0
|
"position_ms": 0
|
||||||
}
|
}
|
||||||
header = {
|
header = {
|
||||||
"Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens,
|
"Authorization": "Bearer " + SessionManager.instance().get_current_session().access_token,
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
print(body)
|
print(body)
|
||||||
|
@ -97,7 +102,7 @@ def current_song():
|
||||||
try:
|
try:
|
||||||
url = SPOTIFY_PLAY_URL + f"me/player/currently-playing?marketing=IT"
|
url = SPOTIFY_PLAY_URL + f"me/player/currently-playing?marketing=IT"
|
||||||
header = {
|
header = {
|
||||||
"Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens,
|
"Authorization": "Bearer " + SessionManager.instance().get_current_session().access_token,
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
return requests.get(url, headers=header).json()
|
return requests.get(url, headers=header).json()
|
||||||
|
@ -105,19 +110,21 @@ def current_song():
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
@music_router.get("/add_queue_track")
|
@music_router.get("/add_queue_track")
|
||||||
def add_queue_track(song_id: str):
|
async def add_queue_track(song_id: str):
|
||||||
try:
|
try:
|
||||||
params = {
|
params = {
|
||||||
"uri": f"spotify:track:{song_id}"
|
"uri": f"spotify:track:{song_id}"
|
||||||
}
|
}
|
||||||
url_encoded_params = urllib.parse.urlencode(params)
|
|
||||||
url = f"{SPOTIFY_AUTH_URL}/me/player/queue{url_encoded_params}"
|
url = f"{SPOTIFY_PLAY_URL}me/player/queue"
|
||||||
|
|
||||||
header = {
|
header = {
|
||||||
"Authorization": "Bearer " + SessionManager.instance().get_current_session().access_tokens,
|
"Authorization": "Bearer " + SessionManager.instance().get_current_session().access_token,
|
||||||
"Content-Type": "application/json"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return requests.post(url, headers=header).json()
|
return requests.post(url, params=params, headers=header).json()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
print(e)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue