Update app.py
Browse files
app.py
CHANGED
|
@@ -82,17 +82,67 @@ load_dotenv()
|
|
| 82 |
# demo.launch()
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
import spotipy
|
| 86 |
from spotipy.oauth2 import SpotifyOAuth
|
| 87 |
import gradio as gr
|
| 88 |
-
import
|
| 89 |
-
import
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
client_id = os.getenv('SPOTIPY_CLIENT_ID')
|
| 94 |
-
client_secret = os.getenv('SPOTIPY_CLIENT_SECRET')
|
| 95 |
-
redirect_uri = os.getenv('SPOTIPY_REDIRECT_URI')
|
| 96 |
|
| 97 |
scope= ['user-library-read',
|
| 98 |
'user-read-playback-state',
|
|
@@ -100,30 +150,43 @@ scope= ['user-library-read',
|
|
| 100 |
'playlist-modify-public',
|
| 101 |
'user-top-read']
|
| 102 |
|
| 103 |
-
|
|
|
|
| 104 |
auth_manager = SpotifyOAuth(client_id=client_id,
|
| 105 |
client_secret=client_secret,
|
| 106 |
redirect_uri=redirect_uri,
|
| 107 |
scope=scope)
|
| 108 |
if code:
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
sp = spotipy.Spotify(auth_manager=auth_manager)
|
| 111 |
devices = sp.devices()
|
| 112 |
device_id = devices['devices'][0]['id']
|
|
|
|
| 113 |
results = sp.search(q="No Pole", type='track')
|
| 114 |
track_uri = results['tracks']['items'][0]['uri']
|
| 115 |
sp.start_playback(device_id=device_id, uris=[track_uri])
|
| 116 |
-
|
|
|
|
| 117 |
else:
|
| 118 |
auth_url = auth_manager.get_authorize_url()
|
| 119 |
-
return f
|
|
|
|
| 120 |
|
| 121 |
def gradio_interface():
|
| 122 |
interface = gr.Interface(fn=spotify_auth,
|
| 123 |
-
inputs=[
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
interface.launch()
|
| 128 |
-
|
| 129 |
gradio_interface()
|
|
|
|
| 82 |
# demo.launch()
|
| 83 |
|
| 84 |
|
| 85 |
+
# import spotipy
|
| 86 |
+
# from spotipy.oauth2 import SpotifyOAuth
|
| 87 |
+
# import gradio as gr
|
| 88 |
+
# import webbrowser
|
| 89 |
+
# import os
|
| 90 |
+
# from dotenv import load_dotenv
|
| 91 |
+
# load_dotenv()
|
| 92 |
+
|
| 93 |
+
# client_id = os.getenv('SPOTIPY_CLIENT_ID')
|
| 94 |
+
# client_secret = os.getenv('SPOTIPY_CLIENT_SECRET')
|
| 95 |
+
# redirect_uri = os.getenv('SPOTIPY_REDIRECT_URI')
|
| 96 |
+
|
| 97 |
+
# scope= ['user-library-read',
|
| 98 |
+
# 'user-read-playback-state',
|
| 99 |
+
# 'user-modify-playback-state',
|
| 100 |
+
# 'playlist-modify-public',
|
| 101 |
+
# 'user-top-read']
|
| 102 |
+
|
| 103 |
+
# def spotify_auth(code=None):
|
| 104 |
+
# auth_manager = SpotifyOAuth(client_id=client_id,
|
| 105 |
+
# client_secret=client_secret,
|
| 106 |
+
# redirect_uri=redirect_uri,
|
| 107 |
+
# scope=scope)
|
| 108 |
+
# if code:
|
| 109 |
+
# token_info = auth_manager.get_access_token(code=code)
|
| 110 |
+
# sp = spotipy.Spotify(auth_manager=auth_manager)
|
| 111 |
+
# devices = sp.devices()
|
| 112 |
+
# device_id = devices['devices'][0]['id']
|
| 113 |
+
# results = sp.search(q="No Pole", type='track')
|
| 114 |
+
# track_uri = results['tracks']['items'][0]['uri']
|
| 115 |
+
# sp.start_playback(device_id=device_id, uris=[track_uri])
|
| 116 |
+
# return f"Account switched successfully.\n{devices}", None
|
| 117 |
+
# else:
|
| 118 |
+
# auth_url = auth_manager.get_authorize_url()
|
| 119 |
+
# return f"Please authorize the app by clicking [here]({auth_url}) and then paste the code below.", auth_url
|
| 120 |
+
|
| 121 |
+
# def gradio_interface():
|
| 122 |
+
# interface = gr.Interface(fn=spotify_auth,
|
| 123 |
+
# inputs=["text"],
|
| 124 |
+
# outputs=["markdown", "text"],
|
| 125 |
+
# live=False,
|
| 126 |
+
# description="Authorize and paste the code here:")
|
| 127 |
+
# interface.launch()
|
| 128 |
+
|
| 129 |
+
# gradio_interface()
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
import os
|
| 133 |
import spotipy
|
| 134 |
from spotipy.oauth2 import SpotifyOAuth
|
| 135 |
import gradio as gr
|
| 136 |
+
from urllib.parse import urlparse, parse_qs
|
| 137 |
+
#from dotenv import load_dotenv
|
| 138 |
+
#load_dotenv()
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
# client_id = os.getenv('SPOTIPY_CLIENT_ID')
|
| 142 |
+
# client_secret = os.getenv('SPOTIPY_CLIENT_SECRET')
|
| 143 |
+
#redirect_uri = os.getenv('SPOTIPY_REDIRECT_URI')
|
| 144 |
+
redirect_uri = "https://huggingface.co/sjw"
|
| 145 |
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
scope= ['user-library-read',
|
| 148 |
'user-read-playback-state',
|
|
|
|
| 150 |
'playlist-modify-public',
|
| 151 |
'user-top-read']
|
| 152 |
|
| 153 |
+
|
| 154 |
+
def spotify_auth(client_id, client_secret, code=None):
|
| 155 |
auth_manager = SpotifyOAuth(client_id=client_id,
|
| 156 |
client_secret=client_secret,
|
| 157 |
redirect_uri=redirect_uri,
|
| 158 |
scope=scope)
|
| 159 |
if code:
|
| 160 |
+
parsed_url = urlparse(code)
|
| 161 |
+
code = parse_qs(parsed_url.query)['code'][0]
|
| 162 |
+
auth_manager.get_access_token(code=code, as_dict=False)
|
| 163 |
+
#auth_manager.get_cached_token() # if .cache exists
|
| 164 |
+
|
| 165 |
sp = spotipy.Spotify(auth_manager=auth_manager)
|
| 166 |
devices = sp.devices()
|
| 167 |
device_id = devices['devices'][0]['id']
|
| 168 |
+
|
| 169 |
results = sp.search(q="No Pole", type='track')
|
| 170 |
track_uri = results['tracks']['items'][0]['uri']
|
| 171 |
sp.start_playback(device_id=device_id, uris=[track_uri])
|
| 172 |
+
|
| 173 |
+
return "Login Successful"
|
| 174 |
else:
|
| 175 |
auth_url = auth_manager.get_authorize_url()
|
| 176 |
+
return f'<p>Please authorize the app by clicking <a href="{auth_url}">here</a> and then paste the code below.</p>'
|
| 177 |
+
|
| 178 |
|
| 179 |
def gradio_interface():
|
| 180 |
interface = gr.Interface(fn=spotify_auth,
|
| 181 |
+
inputs=[
|
| 182 |
+
gr.Textbox(label="Client ID"),
|
| 183 |
+
gr.Textbox(label="Client Secret"),
|
| 184 |
+
gr.Textbox(label="Code")
|
| 185 |
+
],
|
| 186 |
+
outputs=["markdown"],
|
| 187 |
+
description="Authorize and paste the code here:",
|
| 188 |
+
allow_flagging="never",
|
| 189 |
+
theme="finlaymacklon/boxy_violet"
|
| 190 |
+
)
|
| 191 |
interface.launch()
|
|
|
|
| 192 |
gradio_interface()
|