carterwward
commited on
Commit
•
374f6c2
1
Parent(s):
efc4522
update cache
Browse files- app.py +0 -9
- src/spotipy_utils.py +9 -10
app.py
CHANGED
@@ -31,15 +31,6 @@ app.add_middleware(SessionMiddleware, secret_key=FAST_API_KEY)
|
|
31 |
@app.get('/', response_class=HTMLResponse)
|
32 |
async def homepage(request: SRequest):
|
33 |
auth_manager = get_auth_manager() # Need auth manager to generate Spotify API authentication url.
|
34 |
-
url = str(request.url)
|
35 |
-
code = auth_manager.parse_response_code(url)
|
36 |
-
|
37 |
-
if code != url:
|
38 |
-
token_info = auth_manager.get_access_token(code)
|
39 |
-
request.session['token'] = token_info['access_token']
|
40 |
-
return RedirectResponse("/gradio")
|
41 |
-
else:
|
42 |
-
request.session['token'] = None
|
43 |
|
44 |
auth_url = auth_manager.get_authorize_url()
|
45 |
return "<a href='" + auth_url + "'>Login to Spotify</a>"
|
|
|
31 |
@app.get('/', response_class=HTMLResponse)
|
32 |
async def homepage(request: SRequest):
|
33 |
auth_manager = get_auth_manager() # Need auth manager to generate Spotify API authentication url.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
auth_url = auth_manager.get_authorize_url()
|
36 |
return "<a href='" + auth_url + "'>Login to Spotify</a>"
|
src/spotipy_utils.py
CHANGED
@@ -114,21 +114,20 @@ def add_tracks_to_playlist(playlist_name:str, tracks:List, request: gr.Request)
|
|
114 |
Returns:
|
115 |
bool: Flag indicating whether or not the playlist was added to the library succesfully.
|
116 |
"""
|
117 |
-
|
118 |
-
code = auth_manager.parse_response_code(request.headers.get("referer"))
|
119 |
-
old_code = request.request.session.get('code')
|
120 |
|
121 |
-
if
|
122 |
-
|
123 |
-
user_client = spotipy.Spotify(token)
|
124 |
-
else:
|
125 |
code = auth_manager.parse_response_code(request.headers.get("referer"))
|
126 |
token_info = auth_manager.get_access_token(code)
|
127 |
token = token_info['access_token']
|
128 |
-
request.request.session["code"]
|
129 |
-
request.request.session["token"]
|
|
|
|
|
|
|
|
|
130 |
|
131 |
-
user_client = spotipy.Spotify(token)
|
132 |
|
133 |
username = user_client.current_user()["id"]
|
134 |
|
|
|
114 |
Returns:
|
115 |
bool: Flag indicating whether or not the playlist was added to the library succesfully.
|
116 |
"""
|
117 |
+
token = request.request.session.get('token')
|
|
|
|
|
118 |
|
119 |
+
if not token and "referer" in request.request.headers:
|
120 |
+
auth_manager = get_auth_manager()
|
|
|
|
|
121 |
code = auth_manager.parse_response_code(request.headers.get("referer"))
|
122 |
token_info = auth_manager.get_access_token(code)
|
123 |
token = token_info['access_token']
|
124 |
+
del request.request.session["code"]
|
125 |
+
del request.request.session["token"]
|
126 |
+
else:
|
127 |
+
return False
|
128 |
+
|
129 |
+
user_client = spotipy.Spotify(token)
|
130 |
|
|
|
131 |
|
132 |
username = user_client.current_user()["id"]
|
133 |
|