sjw commited on
Commit
accfd5b
·
1 Parent(s): 8f82d81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -5
app.py CHANGED
@@ -1,3 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import spotipy
3
  from spotipy.oauth2 import SpotifyOAuth
@@ -13,7 +67,7 @@ scope= ['user-library-read',
13
  'user-top-read']
14
 
15
 
16
- def spotify_auth(client_id, client_secret, code=None):
17
  auth_manager = SpotifyOAuth(client_id=client_id,
18
  client_secret=client_secret,
19
  redirect_uri=redirect_uri,
@@ -30,10 +84,14 @@ def spotify_auth(client_id, client_secret, code=None):
30
  track_uri = results['tracks']['items'][0]['uri']
31
  sp.start_playback(device_id=device_id, uris=[track_uri])
32
 
33
- return f"Account switched successfully.\n\n{devices}"
 
 
 
 
34
  else:
35
  auth_url = auth_manager.get_authorize_url()
36
- return f"Please authorize the app by clicking [here]({auth_url}) and then paste the code below."
37
 
38
 
39
  with gr.Blocks() as demo:
@@ -48,6 +106,6 @@ with gr.Blocks() as demo:
48
  auth_button = gr.Button("Authorize")
49
  auth_result = gr.Textbox()
50
 
51
- link_button.click(spotify_auth, inputs=[client_id, client_secret], outputs=link_result)
52
- auth_button.click(spotify_auth, inputs=[client_id, client_secret, link], outputs=auth_result)
53
  demo.launch()
 
1
+ # import os
2
+ # import spotipy
3
+ # from spotipy.oauth2 import SpotifyOAuth
4
+ # import gradio as gr
5
+ # from urllib.parse import urlparse, parse_qs
6
+
7
+
8
+ # redirect_uri = "https://huggingface.co/sjw"
9
+ # scope= ['user-library-read',
10
+ # 'user-read-playback-state',
11
+ # 'user-modify-playback-state',
12
+ # 'playlist-modify-public',
13
+ # 'user-top-read']
14
+
15
+
16
+ # def spotify_auth(client_id, client_secret, code=None):
17
+ # auth_manager = SpotifyOAuth(client_id=client_id,
18
+ # client_secret=client_secret,
19
+ # redirect_uri=redirect_uri,
20
+ # scope=scope)
21
+ # if code:
22
+ # parsed_url = urlparse(code)
23
+ # code = parse_qs(parsed_url.query)['code'][0]
24
+ # auth_manager.get_access_token(code=code, as_dict=False)
25
+ # sp = spotipy.Spotify(auth_manager=auth_manager)
26
+ # devices = sp.devices()
27
+ # device_id = devices['devices'][0]['id']
28
+
29
+ # results = sp.search(q="No Pole", type='track')
30
+ # track_uri = results['tracks']['items'][0]['uri']
31
+ # sp.start_playback(device_id=device_id, uris=[track_uri])
32
+
33
+ # return f"Account switched successfully.\n\n{devices}"
34
+ # else:
35
+ # auth_url = auth_manager.get_authorize_url()
36
+ # return f"Please authorize the app by clicking [here]({auth_url}) and then paste the code below."
37
+
38
+
39
+ # with gr.Blocks() as demo:
40
+
41
+ # with gr.Row():
42
+ # client_id = gr.Textbox(label="Client ID")
43
+ # client_secret = gr.Textbox(label="Client Secret")
44
+ # link_button = gr.Button("Get Link")
45
+ # link_result = gr.Markdown()
46
+
47
+ # link = gr.Textbox(label="Paste Link")
48
+ # auth_button = gr.Button("Authorize")
49
+ # auth_result = gr.Textbox()
50
+
51
+ # link_button.click(spotify_auth, inputs=[client_id, client_secret], outputs=link_result)
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
 
67
  'user-top-read']
68
 
69
 
70
+ def spotify_auth(client_id, client_secret, code=None, state=gr.State()):
71
  auth_manager = SpotifyOAuth(client_id=client_id,
72
  client_secret=client_secret,
73
  redirect_uri=redirect_uri,
 
84
  track_uri = results['tracks']['items'][0]['uri']
85
  sp.start_playback(device_id=device_id, uris=[track_uri])
86
 
87
+ # Update the state object
88
+ state = auth_manager.get_access_token(as_dict=False)
89
+ print(state)
90
+
91
+ return f"Account switched successfully.\n\n{devices}", state
92
  else:
93
  auth_url = auth_manager.get_authorize_url()
94
+ return f"Please authorize the app by clicking [here]({auth_url}) and then paste the code below.", state
95
 
96
 
97
  with gr.Blocks() as demo:
 
106
  auth_button = gr.Button("Authorize")
107
  auth_result = gr.Textbox()
108
 
109
+ link_button.click(spotify_auth, inputs=[client_id, client_secret, gr.State()], outputs=[link_result, gr.State()])
110
+ auth_button.click(spotify_auth, inputs=[client_id, client_secret, link, gr.State()], outputs=[auth_result, gr.State()])
111
  demo.launch()