|
| 1 | +import spotipy |
| 2 | +from spotipy.oauth2 import SpotifyClientCredentials |
| 3 | +import random |
| 4 | + |
| 5 | +# Authentication - Replace with your credentials |
| 6 | +CLIENT_ID = 'your_spotify_client_id' |
| 7 | +CLIENT_SECRET = 'your_spotify_client_secret' |
| 8 | + |
| 9 | +# Connect to Spotify API |
| 10 | +auth_manager = SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET) |
| 11 | +sp = spotipy.Spotify(auth_manager=auth_manager) |
| 12 | + |
| 13 | +# Dictionary to map emotions to musical characteristics |
| 14 | +emotion_to_genre = { |
| 15 | + 'happy': ['pop', 'dance', 'indie'], |
| 16 | + 'sad': ['acoustic', 'blues', 'piano'], |
| 17 | + 'angry': ['metal', 'rock', 'punk'], |
| 18 | + 'relaxed': ['ambient', 'chill', 'classical'], |
| 19 | + 'energetic': ['electronic', 'hip-hop', 'funk'], |
| 20 | + 'anxious': ['ambient', 'classical', 'jazz'], |
| 21 | + 'cheerful': ['pop', 'indie', 'reggae'], |
| 22 | + 'stressed': ['jazz', 'chill', 'lo-fi'], |
| 23 | + 'dreamy': ['dream-pop', 'ambient', 'shoegaze'], |
| 24 | + 'excited': ['dance', 'electronic', 'pop'], |
| 25 | + 'bored': ['alternative', 'indie', 'chill'], |
| 26 | + 'nostalgic': ['classic rock', 'folk', 'retro'], |
| 27 | + 'hopeful': ['pop', 'inspirational', 'uplifting'], |
| 28 | + 'content': ['soft rock', 'acoustic', 'country'], |
| 29 | + 'romantic': ['pop', 'r&b', 'soul'], |
| 30 | +} |
| 31 | + |
| 32 | +# Function to recommend tracks based on emotion |
| 33 | +def recommend_tracks(emotion): |
| 34 | + genres = emotion_to_genre.get(emotion.lower(), ['pop']) # Default to 'pop' if emotion is not found |
| 35 | + selected_genre = random.choice(genres) |
| 36 | + |
| 37 | + # Fetch recommendations from Spotify |
| 38 | + results = sp.recommendations(seed_genres=[selected_genre], limit=10) # Increase limit for more results |
| 39 | + tracks = results['tracks'] |
| 40 | + |
| 41 | + playlist = [] |
| 42 | + for track in tracks: |
| 43 | + track_info = { |
| 44 | + 'name': track['name'], |
| 45 | + 'artist': track['artists'][0]['name'], |
| 46 | + 'url': track['external_urls']['spotify'] |
| 47 | + } |
| 48 | + playlist.append(track_info) |
| 49 | + |
| 50 | + return playlist, genres |
| 51 | + |
| 52 | +# Function to get maximum length of strings in a list of dictionaries |
| 53 | +def get_max_lengths(playlist): |
| 54 | + max_name_length = max(len(song['name']) for song in playlist) if playlist else 0 |
| 55 | + max_artist_length = max(len(song['artist']) for song in playlist) if playlist else 0 |
| 56 | + max_url_length = max(len(song['url']) for song in playlist) if playlist else 0 |
| 57 | + return max_name_length, max_artist_length, max_url_length |
| 58 | + |
| 59 | +# Main loop for user input |
| 60 | +while True: |
| 61 | + emotion = input("\nEnter your emotion (happy, sad, angry, relaxed, energetic, anxious, cheerful, stressed, dreamy, excited, bored, nostalgic, hopeful, content, romantic) or type 'exit' to quit: ").strip().lower() |
| 62 | + |
| 63 | + if emotion == 'exit': |
| 64 | + print("Goodbye my Love!!!") |
| 65 | + break |
| 66 | + |
| 67 | + # Get playlist based on the emotion |
| 68 | + playlist, genres = recommend_tracks(emotion) |
| 69 | + |
| 70 | + # Get maximum lengths for formatting |
| 71 | + max_name_length, max_artist_length, max_url_length = get_max_lengths(playlist) |
| 72 | + |
| 73 | + # Set a minimum width for columns to ensure proper alignment |
| 74 | + min_name_length = max(20, max_name_length) |
| 75 | + min_artist_length = max(15, max_artist_length) |
| 76 | + min_url_length = 35 # Fixed width for URL |
| 77 | + |
| 78 | + # Display the recommended playlist |
| 79 | + if emotion not in emotion_to_genre: |
| 80 | + print("The emotion you entered is NOT in the list, so we will show you pop music instead.") |
| 81 | + |
| 82 | + print(f"Here are some songs for your '{emotion}' mood:" if emotion in emotion_to_genre else "Here are some pop songs for you:") |
| 83 | + print(f"STT | Song Name{' ' * (min_name_length - 9)} | Artist{' ' * (min_artist_length - 6)} | URL") |
| 84 | + print("-" * (11 + max_name_length + max_artist_length + max_url_length)) |
| 85 | + |
| 86 | + for idx, song in enumerate(playlist, 1): |
| 87 | + print(f"{idx:<3} | {song['name']:<{min_name_length}} | {song['artist']:<{min_artist_length}} | {song['url']}") |
0 commit comments