mirror of
https://github.com/bnair123/MusicAnalyser.git
synced 2026-02-25 11:46:07 +00:00
fix: ReccoBeats audio features and OpenAI narrative generation
- Add reccobeats_id column to Track model for API mapping - Fix ReccoBeats batch size limit (max 40 IDs per request) - Extract spotify_id from href field in ReccoBeats responses - Fix OpenAI API: remove unsupported temperature param, increase max_completion_tokens to 4000 - Add playlist/user management methods to spotify_client for future auto-playlist feature
This commit is contained in:
@@ -107,3 +107,104 @@ class SpotifyClient:
|
||||
print(f"Error fetching currently playing: {response.text}")
|
||||
return None
|
||||
return response.json()
|
||||
|
||||
async def get_current_user_id(self) -> str:
|
||||
token = await self.get_access_token()
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(
|
||||
f"{SPOTIFY_API_BASE}/me",
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
if response.status_code != 200:
|
||||
raise Exception(f"Failed to get user profile: {response.text}")
|
||||
return response.json().get("id")
|
||||
|
||||
async def create_playlist(
|
||||
self, user_id: str, name: str, description: str = "", public: bool = False
|
||||
) -> Dict[str, Any]:
|
||||
token = await self.get_access_token()
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(
|
||||
f"{SPOTIFY_API_BASE}/users/{user_id}/playlists",
|
||||
headers={
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
json={"name": name, "description": description, "public": public},
|
||||
)
|
||||
if response.status_code not in [200, 201]:
|
||||
raise Exception(f"Failed to create playlist: {response.text}")
|
||||
return response.json()
|
||||
|
||||
async def update_playlist_details(
|
||||
self, playlist_id: str, name: str = None, description: str = None
|
||||
) -> bool:
|
||||
token = await self.get_access_token()
|
||||
data = {}
|
||||
if name:
|
||||
data["name"] = name
|
||||
if description:
|
||||
data["description"] = description
|
||||
|
||||
if not data:
|
||||
return True
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.put(
|
||||
f"{SPOTIFY_API_BASE}/playlists/{playlist_id}",
|
||||
headers={
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
json=data,
|
||||
)
|
||||
return response.status_code == 200
|
||||
|
||||
async def replace_playlist_tracks(
|
||||
self, playlist_id: str, track_uris: List[str]
|
||||
) -> bool:
|
||||
token = await self.get_access_token()
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.put(
|
||||
f"{SPOTIFY_API_BASE}/playlists/{playlist_id}/tracks",
|
||||
headers={
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
json={"uris": track_uris[:100]},
|
||||
)
|
||||
return response.status_code in [200, 201]
|
||||
|
||||
async def get_recommendations(
|
||||
self,
|
||||
seed_tracks: List[str] = None,
|
||||
seed_artists: List[str] = None,
|
||||
seed_genres: List[str] = None,
|
||||
limit: int = 20,
|
||||
target_energy: float = None,
|
||||
target_valence: float = None,
|
||||
) -> List[Dict[str, Any]]:
|
||||
token = await self.get_access_token()
|
||||
params = {"limit": limit}
|
||||
|
||||
if seed_tracks:
|
||||
params["seed_tracks"] = ",".join(seed_tracks[:5])
|
||||
if seed_artists:
|
||||
params["seed_artists"] = ",".join(seed_artists[:5])
|
||||
if seed_genres:
|
||||
params["seed_genres"] = ",".join(seed_genres[:5])
|
||||
if target_energy is not None:
|
||||
params["target_energy"] = target_energy
|
||||
if target_valence is not None:
|
||||
params["target_valence"] = target_valence
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(
|
||||
f"{SPOTIFY_API_BASE}/recommendations",
|
||||
params=params,
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
if response.status_code != 200:
|
||||
print(f"Error fetching recommendations: {response.text}")
|
||||
return []
|
||||
return response.json().get("tracks", [])
|
||||
|
||||
Reference in New Issue
Block a user