sjw commited on
Commit
7587b95
·
1 Parent(s): 452dd85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -71
app.py CHANGED
@@ -1,63 +1,10 @@
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 gradio as gr
57
  import spotipy
58
  from spotipy.oauth2 import SpotifyOAuth
 
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',
@@ -65,15 +12,13 @@ scope= ['user-library-read',
65
  'playlist-modify-public',
66
  'user-top-read']
67
 
68
- def spotify_auth(client_id, client_secret, code=None, spotify_state=None):
69
- if spotify_state is None:
70
- spotify_state = {'sp': None, 'device_id': None}
71
 
 
72
  auth_manager = SpotifyOAuth(client_id=client_id,
73
  client_secret=client_secret,
74
  redirect_uri=redirect_uri,
75
  scope=scope)
76
- if code and isinstance(code, str): # Check if code is a string
77
  parsed_url = urlparse(code)
78
  code = parse_qs(parsed_url.query)['code'][0]
79
  auth_manager.get_access_token(code=code, as_dict=False)
@@ -85,19 +30,13 @@ def spotify_auth(client_id, client_secret, code=None, spotify_state=None):
85
  track_uri = results['tracks']['items'][0]['uri']
86
  sp.start_playback(device_id=device_id, uris=[track_uri])
87
 
88
- spotify_state['sp'] = sp
89
- spotify_state['device_id'] = device_id
90
-
91
- print(f"Spotify object: {sp}") # Print the Spotify object
92
- print(f"Device ID: {device_id}") # Print the device ID
93
-
94
- return f"Account switched successfully.\n\n{devices}", spotify_state
95
  else:
96
  auth_url = auth_manager.get_authorize_url()
97
- return f"Please authorize the app by clicking [here]({auth_url}) and then paste the code below.", spotify_state
 
98
 
99
  with gr.Blocks() as demo:
100
- spotify_state = gr.State({'sp': None, 'device_id': None})
101
 
102
  with gr.Row():
103
  client_id = gr.Textbox(label="Client ID")
@@ -109,6 +48,6 @@ with gr.Blocks() as demo:
109
  auth_button = gr.Button("Authorize")
110
  auth_result = gr.Textbox()
111
 
112
- link_button.click(spotify_auth, inputs=[client_id, client_secret, spotify_state], outputs=[link_result, spotify_state])
113
- auth_button.click(spotify_auth, inputs=[client_id, client_secret, link, spotify_state], outputs=[auth_result, spotify_state])
114
  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',
 
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)
 
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")
 
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()