-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspl_test.py
63 lines (51 loc) · 2.08 KB
/
spl_test.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
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import pandas as pd
import time
client_id = '3309ad26a1394013879d2d284d6ee963'
client_secret = '5aaaae91f4264ed9871d4fd38a1ad8f6'
client_credentials_manager = SpotifyClientCredentials(client_id, client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
def getTrackIDs(user, playlist_id):
ids = []
playlist = sp.user_playlist(user, playlist_id)
for item in playlist['tracks']['items']:
track = item['track']
ids.append(track['id'])
return ids
def getTrackFeatures(id):
meta = sp.track(id)
features = sp.audio_features(id)
# meta
name = meta['name']
album = meta['album']['name']
artist = meta['album']['artists'][0]['name']
release_date = meta['album']['release_date']
length = meta['duration_ms']
popularity = meta['popularity']
# features
acousticness = features[0]['acousticness']
danceability = features[0]['danceability']
energy = features[0]['energy']
instrumentalness = features[0]['instrumentalness']
liveness = features[0]['liveness']
loudness = features[0]['loudness']
speechiness = features[0]['speechiness']
tempo = features[0]['tempo']
time_signature = features[0]['time_signature']
track = [name, album, artist, release_date, length, popularity, danceability, acousticness, danceability, energy, instrumentalness, liveness, loudness, speechiness, tempo, time_signature]
return track
if __name__ == "__main__":
# get track DIs and check for it
ids = getTrackIDs('angelicadietzel', '4R0BZVh27NUJhHGLNitU08')
print(len(ids))
print(ids)
# loop over track ids
tracks = []
for i in range(len(ids)):
time.sleep(.5)
track = getTrackFeatures(ids[i])
tracks.append(track)
# create dataset
df = pd.DataFrame(tracks, columns = ['name', 'album', 'artist', 'release_date', 'length', 'popularity', 'danceability', 'acousticness', 'danceability', 'energy', 'instrumentalness', 'liveness', 'loudness', 'speechiness', 'tempo', 'time_signature'])
df.to_csv("spotify.csv", sep = ',')