ML-with-Rajibul's picture
Update MT.py
341c08b verified
import random
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
# Authenticate with Spotify API
client_id = '471e06ff0a13445095909029b18c265c'
client_secret = 'c0f56895d29f434cbeac4309d0b42d05'
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
def search_song_by_emotion(emotion):
# Define a mapping of emotions to search keywords
emotion_keywords = {
"neutral": ["raga des sarangi", "raga malkauns", "raga bhairav", "raga rageshri"],
"surprise": ["raag hameer", "raag kedar", "raga puriya"],
"fear": ["raag bilahari", "raag purvi", "raag shudh kalyan", "raag miya ki malhar"],
"sad": ["raag yaman sitar", "raga hameer","raga shyam kalyan"],
"angry": ["raag jaijaiwanti", "raag bhairavi", "raga puriya", "raag kafi"],
"happy": ["raga hamsadhwani sarod", "raga khamaj", "raga bhupali", "raga bahar"],
"disgust": ["raga khamaj", "raga bilaskhani todi", "raga suddha kalyan"]
}
# Search for playlists based on the emotion keywords
keywords = emotion_keywords.get(emotion.lower(), [])
if keywords:
keywords = random.choice(keywords)
results = sp.search(q=f"track:{keywords}", type="track", limit=1)
tracks = results["tracks"]["items"]
# Extract song previews from the playlists
if tracks:
preview_url = tracks[0]["preview_url"]
return preview_url
return None