sjw commited on
Commit
64e9ea9
·
1 Parent(s): 85a22e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -7
app.py CHANGED
@@ -1,10 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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',
@@ -12,8 +66,10 @@ scope= ['user-library-read',
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,
@@ -30,14 +86,16 @@ 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:
40
  gr.Markdown("...")
 
41
 
42
  with gr.Tab("Spotify Authentication"):
43
  with gr.Row():
@@ -50,6 +108,6 @@ with gr.Blocks() as demo:
50
  auth_button = gr.Button("Authorize")
51
  auth_result = gr.Textbox()
52
 
53
- link_button.click(spotify_auth, inputs=[client_id, client_secret], outputs=link_result)
54
- auth_button.click(spotify_auth, inputs=[client_id, client_secret, link], outputs=auth_result)
55
  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
+
56
  import os
57
  import spotipy
58
  from spotipy.oauth2 import SpotifyOAuth
59
  import gradio as gr
60
  from urllib.parse import urlparse, parse_qs
61
 
 
62
  redirect_uri = "https://huggingface.co/sjw"
63
  scope= ['user-library-read',
64
  'user-read-playback-state',
 
66
  'playlist-modify-public',
67
  'user-top-read']
68
 
69
+ def spotify_auth(client_id, client_secret, code=None, auth_state=None):
70
+ if auth_state is None:
71
+ auth_state = {}
72
 
 
73
  auth_manager = SpotifyOAuth(client_id=client_id,
74
  client_secret=client_secret,
75
  redirect_uri=redirect_uri,
 
86
  track_uri = results['tracks']['items'][0]['uri']
87
  sp.start_playback(device_id=device_id, uris=[track_uri])
88
 
89
+ auth_state['devices'] = devices
90
+ return f"Account switched successfully.\n\n{devices}", auth_state
91
  else:
92
  auth_url = auth_manager.get_authorize_url()
93
+ auth_state['auth_url'] = auth_url
94
+ return f"Please authorize the app by clicking [here]({auth_url}) and then paste the code below.", auth_state
95
 
96
  with gr.Blocks() as demo:
97
  gr.Markdown("...")
98
+ auth_state = gr.State({})
99
 
100
  with gr.Tab("Spotify Authentication"):
101
  with gr.Row():
 
108
  auth_button = gr.Button("Authorize")
109
  auth_result = gr.Textbox()
110
 
111
+ link_button.click(spotify_auth, inputs=[client_id, client_secret, auth_state], outputs=[link_result, auth_state])
112
+ auth_button.click(spotify_auth, inputs=[client_id, client_secret, link, auth_state], outputs=[auth_result, auth_state])
113
  demo.launch()