-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPlaylistData.py
96 lines (81 loc) · 3.38 KB
/
getPlaylistData.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
import argparse
import pprint
import sys
import os
import subprocess
import json
import spotipy
import spotipy.util as util
import pandas as pd
import time
from spotipy.oauth2 import SpotifyClientCredentials
client_credentials_manager = SpotifyClientCredentials(
client_id='f3b2b3cf8a1a4c57910c954ddba18073', client_secret='72c9355a26a74196bd305ae6ea08fddf')
def show_tracks(tracks):
for i, item in enumerate(tracks['items']):
track = item['track']
print (" %d %s %s" % (i, track['artists'][0]['name'],track['name']))
def get_track_features(track_id,sp):
if track_id is None:
return None
else:
features = sp.audio_features([track_id])
return features
def get_features(tracks,sp):
tracks_with_features=[]
for track in tracks:
features = get_track_features(track['id'],sp)
print (track['name'])
if not features:
print("passing track %s" % track['name'])
pass
else:
f = features[0]
tracks_with_features.append(dict(
name=track['name'],
artist=track['artist'],
id=track['id'],
danceability=f['danceability'],
energy=f['energy'],
loudness=f['loudness'],
speechiness=f['speechiness'],
acousticness=f['acousticness'],
tempo=f['tempo'],
liveness=f['liveness'],
valence=f['valence']
))
# time.sleep(0.1)
# print(tracks_with_features[0])
return tracks_with_features
def get_tracks_from_playlists(username, sp):
playlists = sp.user_playlists(username)
trackList = []
for playlist in playlists['items']:
if playlist['owner']['id'] == username:
print (playlist['name'],' no. of tracks: ',playlist['tracks']['total'])
results = sp.user_playlist(username, playlist['id'],fields="tracks,next")
tracks = results['tracks']
for i, item in enumerate(tracks['items']):
track = item['track']
trackList.append(dict(name=track['name'], id=track['id'], artist=track['artists'][0]['name']))
# print(trackList[0])
return trackList
def write_to_csv(track_features):
df = pd.DataFrame(track_features)
df.drop_duplicates(subset=['name','artist'])
print ('Total tracks in data set', len(df))
df.to_csv('mySongsDataset.csv',index=False)
def main(username):
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
print ("Getting user tracks from playlists")
tracks = get_tracks_from_playlists(username, sp)
print ("Getting track audio features")
tracks_with_features = get_features(tracks,sp)
print ("Storing into csv")
write_to_csv(tracks_with_features)
if __name__ == '__main__':
print ('Starting...')
parser = argparse.ArgumentParser(description='this sript will grab user playlists')
parser.add_argument('--username', help='username')
args = parser.parse_args()
main('4n7rfymwt8mv5ge5c0cgeciwe')