forked from suryanshsk/Python-Voice-Assistant-Suryanshsk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
song_quiz.py
81 lines (66 loc) · 2.75 KB
/
song_quiz.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import requests
from bs4 import BeautifulSoup
import speech_recognition as sr
import pyttsx3
import random
# Initialize the text-to-speech engine
engine = pyttsx3.init()
# Function to speak a message
def speak(message):
engine.say(message)
engine.runAndWait()
# Function to listen for voice input
def listen():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = recognizer.listen(source)
try:
# Recognize the speech using Google Web Speech API
text = recognizer.recognize_google(audio)
print(f"You said: {text}")
return text.lower()
except sr.UnknownValueError:
print("Sorry, I didn't catch that. Could you say it again?")
speak("Sorry, I didn't catch that. Could you say it again?")
return listen() # Retry listening
except sr.RequestError:
print("I can't connect to the service. Please check your internet connection.")
speak("I can't connect to the service. Please check your internet connection.")
return ""
# Function to scrape song data
def scrape_songs():
url = "https://www.billboard.com/charts/hot-100/" # Billboard Hot 100 as an example
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
songs = []
for item in soup.select(".o-chart-results-list-row-container"):
title = item.select_one(".o-chart-results-list-row-container h3").get_text(strip=True)
artist = item.select_one(".o-chart-results-list-row-container h3 + span").get_text(strip=True)
songs.append({"title": title, "artist": artist})
return songs
def play_quiz(songs):
score = 0
total_questions = len(songs)
speak("Welcome to the song quiz! I'm excited to see how much you know about music.")
for song in songs:
question = f"Alright! Who is the artist of the song '{song['title']}'?"
speak(question)
print(question)
answer = listen()
if answer == song['artist'].lower():
score += 1
speak("That's correct! You're really good at this.")
else:
speak(f"Oops! The correct answer is {song['artist']}. Don't worry, let's keep going!")
speak(f"Great job! You scored {score} out of {total_questions}.")
if score == total_questions:
speak("Perfect score! You're a music genius!")
elif score > total_questions / 2:
speak("Not bad! You really know your stuff!")
else:
speak("Keep practicing, and you'll get better!")
if __name__ == "__main__":
songs = scrape_songs()
random.shuffle(songs) # Shuffle songs for randomness
play_quiz(songs[:5]) # Limit to the first 5 songs for the quiz