-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_song_analysis.py
181 lines (145 loc) · 5.41 KB
/
get_song_analysis.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
import os
import csv
import random
# Remove existing audio_analysis.csv file
def clear_audio_analysis_file():
try:
os.remove('audio_analysis.csv')
except FileNotFoundError:
print("File not found")
# Load pre-saved spotify category csv file
def get_song_categories():
file1 = open('categories.csv', 'r')
category_file = csv.reader(file1)
category_list = []
for row in category_file:
category_list.append({"name": row[0], "id": row[1]})
return category_list
# Get a random category
def get_random_category(category_list):
random_index = random.randint(0, len(category_list) - 1)
chosen_cateogry = category_list[random_index]
return chosen_cateogry
# Get a category playlist
def get_category_playlist(spotifyObj, category_id):
category_playlists = spotifyObj.category_playlists(category_id=category_id, limit=1, offset=0)
playlist = category_playlists["playlists"]["items"][0]
return (playlist["name"], playlist["id"])
# Get a songs in the playlist
def get_playlist_songs(spotifyObj, playlist_id):
track_list = []
playlist_songs = spotifyObj.playlist_tracks(playlist_id, fields=None, limit=3, offset=0, additional_types=('track', ))
for song in playlist_songs['items']:
track_list.append({
"track_id": song['track']['id'],
"track_name": song['track']['name'],
"artist_name": song['track']['artists'][0]['name']
}
)
return track_list
# Get audio features of a track
def get_audio_analysis(spotifyObj, track_id):
audio_analysis = spotifyObj.audio_analysis(track_id)
return audio_analysis
# Map pitch class to key (scale)
def get_key(note):
scale = ""
if note == 0:
scale = "C"
elif note == 1:
scale = "C#"
elif note == 2:
scale = "D"
elif note == 3:
scale = "D#"
elif note == 4:
scale = "E"
elif note == 5:
scale = "F"
elif note == 6:
scale = "F#"
elif note == 7:
scale = "G"
elif note == 8:
scale = "G#"
elif note == 9:
scale = "A"
elif note == 10:
scale = "A#"
elif note == 11:
scale = "B"
return scale
# Map mode to major or minor
def get_mode(mode):
return "Major" if mode == 1 else "Minor"
# Save audio features of the tracks
def save_audio_analysis(song, audio_analysis):
# Check whether file already exists
file_exists = os.path.isfile('audio_analysis.csv')
# Open file in append mode and initiate headers
with open ('audio_analysis.csv', 'a') as csvfile:
headers = [
"track_id",
"track_name",
"artist_name",
"timestamp",
"analysis_time",
"num_samples",
"duration",
"loudness",
"tempo",
"tempo_confidence",
"key",
"key_confidence",
"mode",
"mode_confidence"
]
# Create a csv writer object
csv_writer = csv.DictWriter(csvfile, delimiter=',', lineterminator='\n',fieldnames=headers)
# Write the header if the file doesn't exist
if not file_exists:
csv_writer.writeheader() # file doesn't exist yet, write a header
# Write the data
csv_writer.writerow(
{
"track_id": song["track_id"],
"track_name": song["track_name"],
"artist_name": song["artist_name"],
"timestamp": audio_analysis["meta"]["timestamp"],
"analysis_time": audio_analysis["meta"]["analysis_time"],
"num_samples": audio_analysis["track"]["num_samples"],
"duration": audio_analysis["track"]["duration"],
"loudness": audio_analysis["track"]["loudness"],
"tempo": audio_analysis["track"]["tempo"],
"tempo_confidence": audio_analysis["track"]["tempo_confidence"],
"key": get_key(audio_analysis["track"]["key"]),
"key_confidence": audio_analysis["track"]["key_confidence"],
"mode": get_mode(audio_analysis["track"]["mode"]),
"mode_confidence": audio_analysis["track"]["mode_confidence"]
}
)
# Close the file
csvfile.close()
def get_song_analytics(spotipyObj):
# Clear existing audio_analysis.csv file
clear_audio_analysis_file()
# Get song categories from csv file
category_list = get_song_categories()
# Get a random category
# Order of dict: Category Name, Category ID
chosen_category = get_random_category(category_list)
# Get a category playlist
# Order of tuple: Playlist Name, Playlist ID
category_playlist = get_category_playlist(spotipyObj, chosen_category["id"].strip())
# Get a songs in the playlist
# Order of dict: Artist, Song Name, Song ID
playlist_songs = get_playlist_songs(spotipyObj, category_playlist[1])
# print("Playlist Songs: ", playlist_songs)
# Save audio features of per track
for song in playlist_songs:
audio_analysis = get_audio_analysis(spotipyObj, song["track_id"])
save_audio_analysis(song, audio_analysis)
print("\n============== CATEGORY AND PLAYLIST ================")
print("Chosen Category: ", chosen_category["name"].strip())
print("Chosen Playlist: ", category_playlist[0])
print("=====================================================")