Spaces:
Runtime error
Runtime error
Update app_utils.py
Browse files- app_utils.py +52 -16
app_utils.py
CHANGED
@@ -4,15 +4,20 @@ import os
|
|
4 |
import pickle
|
5 |
import spotipy
|
6 |
import spotipy.util as sp_util
|
|
|
7 |
import requests
|
|
|
8 |
|
9 |
dir_path = os.path.dirname(os.path.realpath(__file__))
|
10 |
|
11 |
# current mess: https://github.com/plamere/spotipy/issues/632
|
12 |
-
def centered_button(func, text, n_columns=7, args=None):
|
13 |
columns = st.columns(np.ones(n_columns))
|
14 |
with columns[n_columns//2]:
|
15 |
-
|
|
|
|
|
|
|
16 |
|
17 |
# get credentials
|
18 |
def setup_credentials():
|
@@ -25,7 +30,7 @@ def setup_credentials():
|
|
25 |
|
26 |
os.environ['SPOTIPY_CLIENT_ID'] = client_info['client_id']
|
27 |
os.environ['SPOTIPY_CLIENT_SECRET'] = client_info['client_secret']
|
28 |
-
os.environ['SPOTIPY_REDIRECT_URI'] = 'http://localhost:
|
29 |
return client_info
|
30 |
|
31 |
relevant_audio_features = ["danceability", "energy", "loudness", "mode", "valence", "tempo"]
|
@@ -38,33 +43,41 @@ def get_client():
|
|
38 |
user_id = sp.me()['id']
|
39 |
return sp, user_id
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
def new_get_client(session):
|
43 |
scope = "playlist-modify-public"
|
44 |
-
print('Here3')
|
45 |
|
46 |
cache_handler = StreamlitCacheHandler(session)
|
47 |
auth_manager = spotipy.oauth2.SpotifyOAuth(scope=scope,
|
48 |
cache_handler=cache_handler,
|
49 |
show_dialog=True)
|
50 |
-
print('Here4')
|
51 |
sp, user_id = None, None
|
52 |
|
53 |
-
if requests.get("code"):
|
54 |
-
# Step 2. Being redirected from Spotify auth page
|
55 |
-
auth_manager.get_access_token(requests.get("code"))
|
56 |
-
print('Here7')
|
57 |
-
sp = spotipy.Spotify(auth_manager=auth_manager)
|
58 |
-
user_id = sp.me()['id']
|
59 |
-
|
60 |
-
|
61 |
if not auth_manager.validate_token(cache_handler.get_cached_token()):
|
62 |
-
print('Here6')
|
63 |
# Step 1. Display sign in link when no token
|
64 |
auth_url = auth_manager.get_authorize_url()
|
65 |
-
|
66 |
-
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
|
70 |
def extract_uris_from_links(links, url_type):
|
@@ -131,6 +144,29 @@ def aggregate_genres(genres, legit_genres, verbose=False):
|
|
131 |
genres_output[best_match] = genres[glabel]
|
132 |
return genres_output
|
133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
|
136 |
class StreamlitCacheHandler(spotipy.cache_handler.CacheHandler):
|
|
|
4 |
import pickle
|
5 |
import spotipy
|
6 |
import spotipy.util as sp_util
|
7 |
+
from streamlit import button
|
8 |
import requests
|
9 |
+
from bokeh.models.widgets import Div
|
10 |
|
11 |
dir_path = os.path.dirname(os.path.realpath(__file__))
|
12 |
|
13 |
# current mess: https://github.com/plamere/spotipy/issues/632
|
14 |
+
def centered_button(func, text, n_columns=7, disabled=False, args=None):
|
15 |
columns = st.columns(np.ones(n_columns))
|
16 |
with columns[n_columns//2]:
|
17 |
+
if 'button' in str(func):
|
18 |
+
return func(text, disabled=disabled)
|
19 |
+
else:
|
20 |
+
return func(text)
|
21 |
|
22 |
# get credentials
|
23 |
def setup_credentials():
|
|
|
30 |
|
31 |
os.environ['SPOTIPY_CLIENT_ID'] = client_info['client_id']
|
32 |
os.environ['SPOTIPY_CLIENT_SECRET'] = client_info['client_secret']
|
33 |
+
os.environ['SPOTIPY_REDIRECT_URI'] = 'http://localhost:8501/'
|
34 |
return client_info
|
35 |
|
36 |
relevant_audio_features = ["danceability", "energy", "loudness", "mode", "valence", "tempo"]
|
|
|
43 |
user_id = sp.me()['id']
|
44 |
return sp, user_id
|
45 |
|
46 |
+
def add_button(url, text):
|
47 |
+
st.write(f'''
|
48 |
+
<center>
|
49 |
+
<a target="_self" style='color:black;' href="{url}">
|
50 |
+
<button class='css-1cpxqw2'>
|
51 |
+
{text}
|
52 |
+
</button>
|
53 |
+
</a></center>
|
54 |
+
''',
|
55 |
+
unsafe_allow_html=True
|
56 |
+
)
|
57 |
|
58 |
def new_get_client(session):
|
59 |
scope = "playlist-modify-public"
|
|
|
60 |
|
61 |
cache_handler = StreamlitCacheHandler(session)
|
62 |
auth_manager = spotipy.oauth2.SpotifyOAuth(scope=scope,
|
63 |
cache_handler=cache_handler,
|
64 |
show_dialog=True)
|
|
|
65 |
sp, user_id = None, None
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
if not auth_manager.validate_token(cache_handler.get_cached_token()):
|
|
|
68 |
# Step 1. Display sign in link when no token
|
69 |
auth_url = auth_manager.get_authorize_url()
|
70 |
+
if 'code' not in st.experimental_get_query_params():
|
71 |
+
add_button(auth_url, 'Log in')
|
72 |
|
73 |
+
# st.markdown(f'<a href="{auth_url}" target="_self">Click here to log in</a>', unsafe_allow_html=True)
|
74 |
+
# Step 2. Being redirected from Spotify auth page
|
75 |
+
if 'code' in st.experimental_get_query_params():
|
76 |
+
auth_manager.get_access_token(st.experimental_get_query_params()['code'])
|
77 |
+
sp = spotipy.Spotify(auth_manager=auth_manager)
|
78 |
+
user_id = sp.me()['id']
|
79 |
+
|
80 |
+
return sp, user_id
|
81 |
|
82 |
|
83 |
def extract_uris_from_links(links, url_type):
|
|
|
144 |
genres_output[best_match] = genres[glabel]
|
145 |
return genres_output
|
146 |
|
147 |
+
def get_all_playlists_uris_from_users(sp, user_ids):
|
148 |
+
all_uris = []
|
149 |
+
all_names = []
|
150 |
+
for user_id in user_ids:
|
151 |
+
offset = 0
|
152 |
+
done = False
|
153 |
+
all_uris = []
|
154 |
+
playlist_names = []
|
155 |
+
while not done:
|
156 |
+
playlist_list = sp.user_playlists(user_id, offset=offset, limit=50)
|
157 |
+
these_names = [p['name'] for p in playlist_list['items']]
|
158 |
+
these_uris = [p['uri'] for p in playlist_list['items']]
|
159 |
+
for name, uri in zip(these_names, these_uris):
|
160 |
+
if uri not in all_uris:
|
161 |
+
all_uris.append(uri)
|
162 |
+
all_names.append(user_id + '/' + name)
|
163 |
+
if len(playlist_list['items']) < offset:
|
164 |
+
done = True
|
165 |
+
else:
|
166 |
+
offset += 50
|
167 |
+
return all_uris, all_names
|
168 |
+
|
169 |
+
|
170 |
|
171 |
|
172 |
class StreamlitCacheHandler(spotipy.cache_handler.CacheHandler):
|