File size: 2,730 Bytes
5889992
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from spotipy import Spotify
from spotipy.oauth2 import SpotifyClientCredentials
from typing import Dict, List
from src.music.config.settings import SpotifyConfig, logger
from src.music.models.data_models import RecommendationParameters

class SpotifyClient:
    """Handles Spotify authentication and API operations"""

    def __init__(self, config: SpotifyConfig):
        self.config = config
        self.config.validate()
        self.authenticate()

    def authenticate(self):
        """Authenticate and set up the Spotify client"""
        self.client_credentials_manager = SpotifyClientCredentials(
            client_id=self.config.client_id,
            client_secret=self.config.client_secret
        )
        self.client = Spotify(client_credentials_manager=self.client_credentials_manager)

    def get_recommendations(self, params: RecommendationParameters) -> List[Dict]:
        """Get track recommendations from Spotify"""
        try:
            response = self.client.recommendations(
                seed_genres=params.seed_genres,
                target_features=params.target_features,
                limit=params.limit,
                market=params.market
            )
            return response['tracks']
        except Exception as e:
            logger.error(f"Recommendation failed: {str(e)}")
            raise

    def refresh_authentication(self):
        """Refresh authentication and reinitialize the client"""
        try:
            self.authenticate()
        except Exception as e:
            logger.error(f"Failed to refresh authentication: {str(e)}")
            raise

    def get_available_genres(self) -> List[str]:
        """Get available genre seeds from Spotify"""
        try:
            self.refresh_authentication()  # Ensure fresh authentication
            response = self.client.recommendation_genre_seeds()
            logger.info(f"Available genres: {response}")
            return response.get('genres', [])
        except Exception as e:
            logger.error(f"Failed to fetch genres: {str(e)}")
            return []

    def get_audio_features(self, track_uri: str) -> Dict:
        """Get audio features for a track"""
        try:
            return self.client.audio_features(track_uri)[0] or {}
        except Exception as e:
            logger.error(f"Failed to fetch audio features: {str(e)}")
            return {}


# Usage:
spotify_client = SpotifyClient(config=SpotifyConfig())
params = RecommendationParameters(
    seed_genres=["pop", "rock"], 
    target_features={"danceability": 0.7, "energy": 0.6},
    limit=10,
    market="US"
)

print(params)  # Debugging
recommendations = spotify_client.get_recommendations(params)
print(recommendations)