File size: 734 Bytes
2a89678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pymongo import MongoClient
import certifi
import os

# MongoDB connection string
MONGO_URI = os.getenv("MONGO_URI")
if not MONGO_URI:
    raise ValueError("MONGO_URI environment variable is not set")

# Connect using certifi's CA bundle
client = MongoClient(MONGO_URI, tlsCAFile=certifi.where())

# Access database and collection
db = client["moodify_db"]
collection = db["songs_by_emotion"]

# Function to fetch songs by emotion
def get_songs_by_emotion(emotion):
    results = collection.find({"emotion": emotion})
    return list(results)

# Main
if __name__ == "__main__":
    emotion = input("Enter an emotion (e.g. happy, sad, angry): ")
    songs = get_songs_by_emotion(emotion)
    for song in songs:
        print(song)