Update app.py
Browse files
app.py
CHANGED
|
@@ -52,37 +52,54 @@
|
|
| 52 |
# auth_button.click(spotify_auth, inputs=[client_id, client_secret, link], outputs=auth_result)
|
| 53 |
# demo.launch()
|
| 54 |
|
|
|
|
|
|
|
|
|
|
| 55 |
import gradio as gr
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# auth_button.click(spotify_auth, inputs=[client_id, client_secret, link], outputs=auth_result)
|
| 53 |
# demo.launch()
|
| 54 |
|
| 55 |
+
import os
|
| 56 |
+
import spotipy
|
| 57 |
+
from spotipy.oauth2 import SpotifyOAuth
|
| 58 |
import gradio as gr
|
| 59 |
+
from urllib.parse import urlparse, parse_qs
|
| 60 |
|
| 61 |
+
redirect_uri = "https://huggingface.co/sjw"
|
| 62 |
+
scope= ['user-library-read',
|
| 63 |
+
'user-read-playback-state',
|
| 64 |
+
'user-modify-playback-state',
|
| 65 |
+
'playlist-modify-public',
|
| 66 |
+
'user-top-read']
|
| 67 |
+
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
used_tokens_var = gr.State([])
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
client_id = gr.Textbox(label="Client ID")
|
| 73 |
+
client_secret = gr.Textbox(label="Client Secret")
|
| 74 |
+
link_button = gr.Button("Get Link")
|
| 75 |
+
link_result = gr.Markdown()
|
| 76 |
+
|
| 77 |
+
link = gr.Textbox(label="Paste Link")
|
| 78 |
+
auth_button = gr.Button("Authorize")
|
| 79 |
+
auth_result = gr.Textbox()
|
| 80 |
+
|
| 81 |
+
def spotify_auth(client_id, client_secret, used_tokens, code=None):
|
| 82 |
+
auth_manager = SpotifyOAuth(client_id=client_id,
|
| 83 |
+
client_secret=client_secret,
|
| 84 |
+
redirect_uri=redirect_uri,
|
| 85 |
+
scope=scope)
|
| 86 |
+
if code:
|
| 87 |
+
parsed_url = urlparse(code)
|
| 88 |
+
code = parse_qs(parsed_url.query)['code'][0]
|
| 89 |
+
|
| 90 |
+
# EACH USER GETS A UNIQUE CODE
|
| 91 |
+
token = auth_manager.get_access_token(code=code, as_dict=False)
|
| 92 |
+
used_tokens.append(token)
|
| 93 |
+
dictionary = {
|
| 94 |
+
used_tokens_var: used_tokens,
|
| 95 |
+
}
|
| 96 |
+
print(dictionary)
|
| 97 |
+
return token
|
| 98 |
+
else:
|
| 99 |
+
auth_url = auth_manager.get_authorize_url()
|
| 100 |
+
return f"Please authorize the app by clicking [here]({auth_url}) and then paste the code below."
|
| 101 |
+
|
| 102 |
+
link_button.click(spotify_auth, inputs=[client_id, client_secret, used_tokens_var], outputs=link_result)
|
| 103 |
+
auth_button.click(spotify_auth, inputs=[client_id, client_secret, used_tokens_var, link], outputs=auth_result)
|
| 104 |
+
|
| 105 |
+
demo.launch()
|