Skip to content

Commit c5ce902

Browse files
amaziahubAmazia Gur
and
Amazia Gur
authored
feat: introduce mood based song generator (#380)
Co-authored-by: Amazia Gur <[email protected]>
1 parent 0ea1a29 commit c5ce902

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Mood Based Youtube Song Generator
2+
This Python script fetches a random song from YouTube based on your mood input and opens it in your default web browser.
3+
4+
## Features
5+
Accepts mood input (e.g., happy, sad, energetic) and finds related songs on YouTube.
6+
Opens the YouTube song URL in your browser.
7+
8+
## Setup
9+
10+
### 1. Install dependencies:
11+
```shell
12+
pip install -r requirements.txt
13+
```
14+
15+
### 2. Get your YouTube API key:
16+
- Follow the instructions to get your YouTube Data API key.
17+
18+
### 3. Set your API key:
19+
Replace the api_key variable in the script with your own [YouTube Data API key](https://developers.google.com/youtube/v3/getting-started)
20+
```python
21+
api_key = "YOUR_YOUTUBE_API_KEY"
22+
```
23+
24+
### 4. Run the script:
25+
```shell
26+
python random_song_generator.py
27+
```
28+
29+
## Example
30+
Input:
31+
```bash
32+
Enter your mood (e.g., happy, sad, energetic): happy
33+
```
34+
The script will fetch a song and open it in your browser.
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import random
2+
import webbrowser
3+
from googleapiclient.discovery import build
4+
5+
6+
def fetch_youtube_songs(mood):
7+
api_key = "your_youtube_api_key"
8+
youtube = build("youtube", "v3", developerKey=api_key)
9+
request = youtube.search().list(
10+
q=f"{mood} song", part="snippet", type="video", maxResults=10
11+
)
12+
response = request.execute()
13+
songs = [
14+
f"{item['snippet']['title']} - https://www.youtube.com/watch?v={item['id']['videoId']}"
15+
for item in response['items']
16+
]
17+
return songs
18+
19+
20+
def random_song_generator():
21+
mood = input("Enter your mood (e.g., happy, sad, energetic): ").lower()
22+
try:
23+
songs = fetch_youtube_songs(mood)
24+
random_song = random.choice(songs)
25+
song_title, song_url = random_song.split(" - ")
26+
webbrowser.open(song_url)
27+
print(f"Here's a song for your mood ({mood}):\n{random_song}")
28+
except Exception as e:
29+
print("Error fetching songs. Please check your API key and mood.")
30+
31+
32+
if __name__ == "__main__":
33+
random_song_generator()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-api-python-client

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ More information on contributing and the general code of conduct for discussion
9696
| Longitude & Latitude to conical coverter | [Longitude Latitude conical converter](master/Longitude%20Latitude%20conical%20converter) | Converts Longitude and Latitude to Lambert conformal conic projection. |
9797
| Mail Sender | [Mail Sender](https://github.com/DhanushNehru/Python-Scripts/tree/master/Mail%20Sender) | Sends an email. |
9898
| Merge Two Images | [Merge Two Images](https://github.com/DhanushNehru/Python-Scripts/tree/master/Merge%20Two%20Images) | Merges two images horizontally or vertically. |
99+
| Mood based youtube song generator | [Mood based youtube song generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Mood%20based%20youtube%20song%20generator) | This Python script fetches a random song from YouTube based on your mood input and opens it in your default web browser. |
99100
| Mouse mover | [Mouse mover](https://github.com/DhanushNehru/Python-Scripts/tree/master/Mouse%20Mover) | Moves your mouse every 15 seconds. |
100101
| Morse Code | [Mose Code](https://github.com/DhanushNehru/Python-Scripts/tree/master/Morse%20Code) | Encodes and decodes Morse code. |
101102
| No Screensaver | [No Screensaver](https://github.com/DhanushNehru/Python-Scripts/tree/master/No%20Screensaver) | Prevents screensaver from turning on. |

0 commit comments

Comments
 (0)