-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetSongData.py
68 lines (52 loc) · 1.94 KB
/
GetSongData.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
'''
Pull data from Spotify's API on a playlist (currently Top 500 of 2022) then
write data into a JSON file for future use
'''
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import json
# authentication
with open("client_secrets.json") as f:
data = json.load(f)
cid = data["client_id"]
secret = data["client_secret"]
client_cred_manager = SpotifyClientCredentials(client_id = cid, \
client_secret = secret)
sp = spotipy.Spotify(client_credentials_manager = client_cred_manager)
# URL to playlist - can be replaced with any playlist
# currently using the top 500 songs of 2022
playlist_url = "https://open.spotify.com/playlist/64QxysD2w5x5EMLgcoT7fa"
uri = playlist_url.split("/")[-1].split("?")[0]
song_data = []
i = 0
# can only pull 100 songs at once, so loop through 5 times
for o in range(5):
for track in sp.playlist_tracks(uri, limit = 100, offset = o * 100)["items"]:
song = {}
# URI
track_uri = track["track"]["uri"]
song["URI"] = (track_uri)
# track name
track_name = track["track"]["name"]
song["name"] = (track_name)
# artist info
artist_uri = track["track"]["artists"][0]["uri"]
artist_info = sp.artist(artist_uri)
artist_name = track["track"]["artists"][0]["name"]
artist_genres = artist_info["genres"]
song["artist"] = (artist_name)
song["genre"] = (artist_genres)
# audio features
features = sp.audio_features(track_uri)[0]
song["tempo"] = (features["tempo"])
song["energy"] = (features["energy"])
song["key"] = (features["key"])
song["danceability"] = (features["danceability"])
song["liveness"] = (features["liveness"])
song["acousticness"] = (features["acousticness"])
song_data.append(song)
print(i)
i += 1
# write song data to json file
with open("data2.json", "w") as outfile:
json.dump(song_data, outfile)