-
Notifications
You must be signed in to change notification settings - Fork 0
/
MusicQuizAPI.py
182 lines (158 loc) · 5.46 KB
/
MusicQuizAPI.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import requests # Importing requests library for making HTTP requests
import json # Importing json library for JSON manipulation
import random # Importing random library for random sampling
# Initialize variables
artist_list = ""
id_artist = []
block = 1
artists_info = []
artist_info = {}
artists_list_id = {}
id_artist_quest = []
answer = ""
# Insert here the artists you are interested in
to_be_searched_artists = [
"Elio e le storie tese",
"Queen",
"Eminem",
"Al Bano",
"Mina",
"Ghali",
"Muse",
"The Beatles",
"The Rolling Stones",
"Michael Jackson",
"I Prevail",
"Oasis",
"Pink Floyd",
"Billie Eilish",
"Drake",
"Peanetty",
"Naska",
"Daft Punk",
]
# Deezer API URLs
DEEZER_SEARCH_ARTIST_API = "https://api.deezer.com/search/artist"
DEEZER_SEARCH_ARTIST_INFO = "https://api.deezer.com/artist/"
def get_artists_ids(to_be_searched_artists):
"""
Get the Deezer IDs for the given list of artists.
Parameters:
- to_be_searched_artists: List of artists to search for
Returns:
- artists_list_id: Dictionary containing artists and their corresponding Deezer IDs
"""
results = {}
for artist in to_be_searched_artists:
r = requests.get(
DEEZER_SEARCH_ARTIST_API, params={"q": artist, "strict": "on"}
).json()
if len(r["data"]) == 0:
print("No match found for '", artist, "'")
elif len(r["data"]) > 1:
print(
"Multiple matches found for '",
artist,
"'. I selected the most popular match. Checkout their pic:",
r["data"][0]["picture_big"],
)
artists_list_id[artist] = r["data"][0]["id"]
else:
print("Exactly one match found for '", artist, "'")
artists_list_id[artist] = r["data"][0]["id"]
json.dumps(artists_list_id)
return artists_list_id
def select_random_id(artists_list_id):
"""
Select random Deezer IDs from the given list of artists.
Parameters:
- artists_list_id: Dictionary containing artists and their corresponding Deezer IDs
Returns:
- id_artist_quest: List containing randomly selected Deezer IDs
- input_artists: List containing names of artists corresponding to the selected IDs
"""
input_artists = random.sample(list(artists_list_id.keys()), k=2)
id_artist_quest = [int(artists_list_id[artist]) for artist in input_artists]
return id_artist_quest, input_artists
def get_artist_info(id_artist_quest):
"""
Get information about artists from Deezer using their IDs.
Parameters:
- id_artist_quest: List containing Deezer IDs of the artists to get information about
Returns:
- artists_info: List containing information about each artist
"""
artists_info = [] # Initialize as an empty list
for id in id_artist_quest:
quest_url = "{0}{1}".format(DEEZER_SEARCH_ARTIST_INFO, id)
artist_info = requests.get(quest_url).json()
artists_info.append(artist_info)
return artists_info
def quest(artists_info, block):
"""
Quiz function to compare the number of albums between two randomly selected artists.
Parameters:
- artists_info: List containing information about two artists
- block: Flag to control the execution of the quiz
Returns:
- block: Updated flag after completing the quiz
"""
answer = input(
"Which artist between {0} (1) and {1} (2) has more albums? ".format(
artists_info[0]["name"], artists_info[1]["name"]
)
).lower()
try:
if answer not in ["1", "2", "stop"]:
raise ValueError
if answer == "stop":
block = 0
raise NameError
if answer == "1" and artists_info[0]["nb_album"] > artists_info[1]["nb_album"]:
print(
"Correct! {0} has {1} albums, while {2} has {3} albums".format(
artists_info[0]["name"],
artists_info[0]["nb_album"],
artists_info[1]["name"],
artists_info[1]["nb_album"],
)
)
elif (
answer == "2" and artists_info[0]["nb_album"] < artists_info[1]["nb_album"]
):
print(
"Correct! {0} has {1} albums, while {2} has {3} albums".format(
artists_info[1]["name"],
artists_info[1]["nb_album"],
artists_info[0]["name"],
artists_info[0]["nb_album"],
)
)
else:
print(
"Wrong! {0} has {1} albums, while {2} has {3} albums".format(
artists_info[1]["name"],
artists_info[1]["nb_album"],
artists_info[0]["name"],
artists_info[0]["nb_album"],
)
)
except ValueError:
print("Please insert 1, 2, or 'stop' to close the program")
except NameError:
print("Thanks for playing")
block = 0
return block
def main(block):
"""
Main function to run the quiz.
Parameters:
- block: Flag to control the execution of the quiz
"""
artists_list_id = get_artists_ids(to_be_searched_artists)
while block == 1:
id_artist_quest, input_artists = select_random_id(artists_list_id)
artists_info = get_artist_info(id_artist_quest)
block = quest(artists_info, block)
artists_info = []
main(block)