Halim Bouayad commited on
Commit
feb4627
·
1 Parent(s): 1b3f3fc

first commit

Browse files
Files changed (4) hide show
  1. env_variables.py +13 -0
  2. main_ss.py +101 -0
  3. requirements.txt +6 -0
  4. ss_functions.py +270 -0
env_variables.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Sat Jul 23 10:38:49 2022
5
+
6
+ @author: halimbouayad
7
+ """
8
+ from datetime import datetime
9
+ import os
10
+ os.environ['SPOTIPY_CLIENT_ID'] = "fc99466410414fbb9aaafbfe76ab5a1d"
11
+ os.environ['SPOTIPY_CLIENT_SECRET'] = "83577093a17d4db3989a074b85b7905a"
12
+ os.environ['SPOTIPY_REDIRECT_URI'] = "http://www.google.com:1234"
13
+ #os.environ['SPOTIPY_REDIRECT_URI'] = "https://halimbox-spotify-shuffler-main-ss-p7ac1f.streamlitapp.com:1234"
main_ss.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Mon Jul 4 10:48:49 2022
5
+
6
+ @author: halimbouayad
7
+ """
8
+
9
+ import pandas as pd
10
+ import numpy as np
11
+
12
+
13
+ from datetime import datetime
14
+ import os
15
+
16
+ # #load data
17
+
18
+ # dir = os.getcwd()
19
+ # path = os.path.join(dir, 'data.csv')
20
+
21
+ # try:
22
+ # df=pd.read_csv(path)
23
+ # df_continent=pd.DataFrame(df.groupby('continent').total_deaths.sum())
24
+ # except:
25
+ # print('Error has occured')
26
+
27
+
28
+
29
+
30
+ # ################################################
31
+
32
+ import spotipy
33
+ from spotipy.oauth2 import SpotifyOAuth
34
+ import json
35
+ import spotipy.util as util
36
+ from spotipy.oauth2 import SpotifyClientCredentials
37
+ import requests
38
+ from requests import *
39
+ import pandas as pd
40
+ import streamlit as st
41
+
42
+
43
+ # Custom imports
44
+ import ss_functions
45
+ from ss_functions import *
46
+ import env_variables
47
+ username = "hyder14"
48
+
49
+
50
+
51
+
52
+
53
+
54
+ st.title('Spotify Playlist Shuffler')
55
+
56
+ st.text('The goal of this project is to gain better control of your playlists\n while exploring the features of the Spotify Web API.')
57
+
58
+
59
+
60
+ with st.expander('Create a playlist'):
61
+
62
+
63
+ st.header('Create a playlist')
64
+ st.write('Hello, *World!* :sunglasses:')
65
+
66
+ scope = "playlist-modify-public"
67
+
68
+
69
+ token = util.prompt_for_user_token(username,scope,client_id=os.environ['SPOTIPY_CLIENT_ID'],client_secret=os.environ['SPOTIPY_CLIENT_SECRET'],redirect_uri=os.environ['SPOTIPY_REDIRECT_URI'])
70
+ spotifyObject = spotipy.Spotify(auth=token)
71
+
72
+
73
+ #create the playlist
74
+ playlist_name = st.text_input('Enter a playlist name = ')
75
+ playlist_description = st.text_input('Enter a playlist description = ')
76
+
77
+ if st.button('Create playlist!'):
78
+ token = util.prompt_for_user_token(username,scope,client_id=SPOTIPY_CLIENT_ID,client_secret=SPOTIPY_CLIENT_SECRET,redirect_uri=SPOTIPY_REDIRECT_URI)
79
+ spotifyObject = spotipy.Spotify(auth=token)
80
+ spotifyObject.user_playlist_create(user=username, name=playlist_name, public=True, description=playlist_description)
81
+
82
+
83
+
84
+ with st.expander('List of current playlists'):
85
+
86
+ st.header("List of playlist")
87
+
88
+ sp=connect(scope='user-library_read', username=username)
89
+
90
+ df, test=get_data(sp, username)
91
+ st.dataframe(df)
92
+
93
+ st.text(test)
94
+
95
+
96
+ with st.expander('What do your playlists look like?'):
97
+ if isinstance(df, pd.DataFrame):
98
+ EDA(df)
99
+
100
+
101
+
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ numpy==1.23.0
2
+ pandas==1.4.3
3
+ plotly==5.9.0
4
+ requests==2.28.1
5
+ spotipy==2.20.0
6
+ streamlit==1.10.0
ss_functions.py ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Sat Jul 23 10:15:02 2022
5
+
6
+ @author: halimbouayad
7
+ """
8
+ import spotipy
9
+ from spotipy.oauth2 import SpotifyOAuth
10
+ import json
11
+ import spotipy.util as util
12
+ from spotipy.oauth2 import SpotifyClientCredentials
13
+ import requests
14
+ from requests import *
15
+ import pandas as pd
16
+ import streamlit as st
17
+ import numpy as np
18
+ import time
19
+
20
+ def connect(scope, username):
21
+ scope = "user-library-read"
22
+ token = SpotifyOAuth(scope=scope, username=username)
23
+ spotifyObject = spotipy.Spotify(auth_manager=token)
24
+ return spotifyObject
25
+
26
+
27
+ def get_features(a):
28
+
29
+ feature_names = [f for f in a[0]]
30
+ features = []
31
+
32
+ for i in range(len(a)):
33
+
34
+ features.append([a[i][f] for f in a[i]])
35
+ # features=list(np.pad(features, (0, max_length), 'constant'))
36
+
37
+ #print(len(features))
38
+ return features, feature_names
39
+
40
+
41
+
42
+ def get_artist(liste):
43
+ for i, artists in enumerate(liste):
44
+ if i ==0:
45
+ artist=artists['name']
46
+ else:
47
+ artist=artist+", "+artists['name']
48
+ return artist
49
+
50
+ @st.cache
51
+ def get_data(sp, username):
52
+ playlists = sp.current_user_playlists(limit=50)
53
+
54
+ tr_names=[]
55
+ pl_urls=[]
56
+ pl_names=[]
57
+ #tracks=[]
58
+ #links=[]
59
+ date_added=[]
60
+ popularity=[]
61
+ ids=[]
62
+ artist_list=[]
63
+ #features=[]
64
+ features=pd.DataFrame()
65
+ df=pd.DataFrame()
66
+
67
+ while playlists:
68
+
69
+
70
+
71
+ for i, playlist in enumerate(playlists['items']):
72
+ #print("%4d %s %s" % (i + 1 + playlists['offset'], playlist['uri'], playlist['name']))
73
+ # urls.append(playlist['uri'])
74
+ # names.append(playlist['name'])
75
+ # tracks.append(playlist['tracks']['total'])
76
+ # links.append(playlist['external_urls']['spotify'])
77
+ # ids.append(playlist['id'])
78
+ #print(playlist)
79
+ #st.text(last)
80
+ print(playlist)
81
+ playlist_content=sp.playlist(playlist['uri'])
82
+ for i,trax in enumerate(playlist_content['tracks']['items']):
83
+
84
+ #Track name
85
+ tr_names.append(trax['track']['name'])
86
+
87
+ #Track ID
88
+ ids.append(trax['track']['id'])
89
+
90
+ date_added.append(trax['added_at'])
91
+
92
+ popularity.append(trax['track']['popularity'])
93
+
94
+ #Playlist Name
95
+ pl_names.append(playlist['name'])
96
+
97
+ #Track URI
98
+ pl_urls.append(playlist['uri'])
99
+
100
+ artist_list.append(get_artist(trax['track']['artists']))
101
+
102
+ test='All done!'
103
+
104
+ if playlists['next']:
105
+ playlists = sp.next(playlists)
106
+ else:
107
+ playlists = None
108
+
109
+ #audio_analysis
110
+
111
+
112
+ df['playlist']=pl_names
113
+ df['track']=tr_names
114
+ df['artist']=artist_list
115
+ df['playlist_url']=pl_urls
116
+ df['track_id']=ids
117
+ df['date_added']=date_added
118
+ df['popularity']=popularity
119
+
120
+ #Audio Features
121
+
122
+ # start=0
123
+
124
+ # for i in range(int(np.ceil(len(df)/50))):
125
+ # end=max(start+50, len(df))
126
+
127
+
128
+
129
+ # print('test='+str(i))
130
+ # print(feat)
131
+
132
+ #API only handles batch of 100 of ids
133
+ for start in range(0, len(df), 100):
134
+
135
+ if start+100>len(df):
136
+ end=len(df)
137
+ else:
138
+ end=start+100
139
+
140
+
141
+
142
+ audio_feat=sp.audio_features(df.track_id[start:end])
143
+
144
+ ##print('audio feat=======')
145
+ #print(audio_feat)
146
+
147
+ #print('feat=======')
148
+ feat, feat_names = get_features(audio_feat)
149
+ print('-----feat')
150
+ print(feat)
151
+ print('-----features')
152
+ print(features)
153
+ features=pd.concat([features, pd.DataFrame(feat)], axis=0)
154
+ print('-----features')
155
+ print(features)
156
+
157
+ #print(len(features))
158
+
159
+
160
+ features.columns=feat_names
161
+
162
+ df=pd.concat([df.reset_index(), features.reset_index()], axis=1)
163
+
164
+
165
+
166
+ df=df.drop(columns=['index', 'index'])
167
+
168
+ #df.to_pickle('./spotify.pkl')
169
+ return df, test
170
+
171
+ # def playlist_details(sp, df, username):
172
+
173
+
174
+
175
+ # selected=st.selectbox('Please select to zoom', df.name)
176
+ # uri=df[df.name==selected].reset_index()['url'][0]
177
+ # #df_tracks=pd.DataFrame(playlist_content)
178
+
179
+ # # Connection to Spotipy
180
+ # #sp=connect(scope='user-library_read', username=username)
181
+ # playlist_content=sp.playlist(uri)
182
+ # names=[]
183
+ # releasedays=[]
184
+ # ids=[]
185
+
186
+ # for i,trax in enumerate(playlist_content['tracks']['items']):
187
+
188
+ # names.append(trax['track']['name'])
189
+ # #releasedays.append(trax['track']['added_at'])
190
+ # ids.append(trax['track']['id'])
191
+
192
+ # pl=pd.DataFrame()
193
+ # pl['name']=names
194
+ # #pl['release_date']=releasedays
195
+ # pl['id']=ids
196
+ # st.dataframe(pl)
197
+ # return df
198
+
199
+ def EDA(df):
200
+ st.header("EDA")
201
+ #Key Metrics
202
+ f_value=st.selectbox('Select a playlist', df.playlist.unique())
203
+ dff=df[df.playlist==f_value]
204
+
205
+ cols=['danceability','energy','acousticness','tempo','loudness','valence']
206
+ for col in cols[:2]:
207
+ temp=dff.describe().loc['mean',:][col]
208
+ st.metric(col, '{:.1%}'.format(temp))
209
+
210
+ st.text('Top songs from that album:')
211
+ st.dataframe(dff)
212
+
213
+ selected=st.selectbox('By playlist (click to select other filter)', df.columns)
214
+ st.bar_chart(df[df.playlist==f_value].groupby(selected).agg({'danceability':'mean'}))
215
+
216
+
217
+
218
+
219
+
220
+ #if st.button('View playlist!'):
221
+
222
+ # def playlist_overview(sp, username):
223
+ # # scope = "user-library-read"
224
+ # # token = SpotifyOAuth(scope=scope, username=username)
225
+ # # spotifyObject = spotipy.Spotify(auth_manager=token)
226
+
227
+
228
+ # playlists = sp.current_user_playlists(limit=50)
229
+ # print('=================')
230
+ # #response = requests.get("http://localhost:1234", timeout=10)
231
+ # #print(response.json())
232
+ # print('=================')
233
+ # df=pd.DataFrame()
234
+
235
+ # urls=[]
236
+ # names=[]
237
+ # tracks=[]
238
+ # links=[]
239
+ # ids=[]
240
+
241
+ # while playlists:
242
+
243
+ # for i, playlist in enumerate(playlists['items']):
244
+ # #print("%4d %s %s" % (i + 1 + playlists['offset'], playlist['uri'], playlist['name']))
245
+ # urls.append(playlist['uri'])
246
+ # names.append(playlist['name'])
247
+ # tracks.append(playlist['tracks']['total'])
248
+ # links.append(playlist['external_urls']['spotify'])
249
+ # ids.append(playlist['id'])
250
+ # #print(playlist)
251
+ # #st.text(last)
252
+
253
+
254
+
255
+ # if playlists['next']:
256
+ # playlists = sp.next(playlists)
257
+ # else:
258
+ # playlists = None
259
+
260
+ # #audio_analysis
261
+
262
+ # df['name']=names
263
+ # df['tracks']=tracks
264
+ # df['url']=urls
265
+ # df['id']=ids
266
+ # st.dataframe(df)
267
+
268
+ #[a[0][i] for i in a[0]]
269
+ # 1seFIg83Eac87g1hmtBRjG
270
+ # 5ZwRTJD1bTACtIuW5Iibnu