-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_api.py
120 lines (72 loc) · 3.36 KB
/
spotify_api.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
get_ipython().run_line_magic('matplotlib', 'inline')
import os
#import my_spotify_credentials as credentials
import numpy as np
import pandas as pd
import ujson
import spotipy
import spotipy.util
import seaborn as sns
# use your credentials
os.environ["SPOTIPY_CLIENT_ID"] = ''
os.environ["SPOTIPY_CLIENT_SECRET"] = ''
os.environ["SPOTIPY_REDIRECT_URI"] = ''
scope = 'user-library-read'
username = ''
token = spotipy.util.prompt_for_user_token(username, scope)
if token:
spotipy_obj = spotipy.Spotify(auth=token)
saved_tracks_resp = spotipy_obj.current_user_saved_tracks(limit=50)
else:
print('Couldn\'t get token for that username')
number_of_tracks = saved_tracks_resp['total']
print('%d tracks' % number_of_tracks)
def save_only_some_fields(track_response):
return {
'id': str(track_response['track']['id']),
'name': str(track_response['track']['name']),
'artists': [artist['name'] for artist in track_response['track']['artists']],
'duration_ms': track_response['track']['duration_ms'],
'popularity': track_response['track']['popularity'],
'added_at': track_response['added_at']
}
tracks = [save_only_some_fields(track) for track in saved_tracks_resp['items']]
while saved_tracks_resp['next']:
saved_tracks_resp = spotipy_obj.next(saved_tracks_resp)
tracks.extend([save_only_some_fields(track) for track in saved_tracks_resp['items']])
# In[2]:
tracks_df = pd.DataFrame(tracks)
pd.set_option('display.max_rows', len(tracks))
# In[3]:
#pd.reset_option('display.max_rows')
tracks_df['artists'] = tracks_df['artists'].apply(lambda artists: artists[0])
tracks_df['duration_ms'] = tracks_df['duration_ms'].apply(lambda duration: duration/1000)
tracks_df = tracks_df.rename(columns = {'duration_ms':'duration_s'})
# In[5]:
audio_features = {}
for idd in tracks_df['id'].tolist():
audio_features[idd] = spotipy_obj.audio_features(idd)[0]
tracks_df['acousticness'] = tracks_df['id'].apply(lambda idd: audio_features[idd]['acousticness'])
tracks_df['speechiness'] = tracks_df['id'].apply(lambda idd: audio_features[idd]['speechiness'])
tracks_df['key'] = tracks_df['id'].apply(lambda idd: str(audio_features[idd]['key']))
tracks_df['liveness'] = tracks_df['id'].apply(lambda idd: audio_features[idd]['liveness'])
tracks_df['instrumentalness'] = tracks_df['id'].apply(lambda idd: audio_features[idd]['instrumentalness'])
tracks_df['energy'] = tracks_df['id'].apply(lambda idd: audio_features[idd]['energy'])
tracks_df['tempo'] = tracks_df['id'].apply(lambda idd: audio_features[idd]['tempo'])
tracks_df['time_signature'] = tracks_df['id'].apply(lambda idd: audio_features[idd]['time_signature'])
tracks_df['loudness'] = tracks_df['id'].apply(lambda idd: audio_features[idd]['loudness'])
tracks_df['danceability'] = tracks_df['id'].apply(lambda idd: audio_features[idd]['danceability'])
tracks_df['valence'] = tracks_df['id'].apply(lambda idd: audio_features[idd]['valence'])
# In[7]:
# In[ ]:
class getSong(): # cnn to rnn is hooked here.
def __init__(self):
super(getSong, self).__init__()
def passs():
return tracks_df
# 'name', 'artists', 'duration_ms', 'explicit', 'danceability', 'energy',
# 'key', 'loudness', 'mode', 'speechiness', 'acousticness',
# 'instrumentalness', 'liveness', 'valence', 'tempo'