File size: 1,550 Bytes
9910ecc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
341c08b
9910ecc
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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