Update app.py
Browse files
app.py
CHANGED
|
@@ -1,134 +1,3 @@
|
|
| 1 |
-
import spotipy
|
| 2 |
-
from spotipy.oauth2 import SpotifyOAuth
|
| 3 |
-
from spotipy.exceptions import SpotifyException
|
| 4 |
-
from spotipy.oauth2 import SpotifyClientCredentials
|
| 5 |
-
import os
|
| 6 |
-
import gradio as gr
|
| 7 |
-
from dotenv import load_dotenv
|
| 8 |
-
load_dotenv()
|
| 9 |
-
|
| 10 |
-
# scope= ['user-library-read',
|
| 11 |
-
# 'user-read-playback-state',
|
| 12 |
-
# 'user-modify-playback-state',
|
| 13 |
-
# 'playlist-modify-public',
|
| 14 |
-
# 'user-top-read'
|
| 15 |
-
# ]
|
| 16 |
-
|
| 17 |
-
# def test():
|
| 18 |
-
# sp = spotipy.Spotify(
|
| 19 |
-
# client_credentials_manager=SpotifyClientCredentials(),
|
| 20 |
-
# auth_manager=SpotifyOAuth(scope=scope)
|
| 21 |
-
# )
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# devices = sp.devices()
|
| 25 |
-
# device_id = devices['devices'][0]['id']
|
| 26 |
-
# return sp, devices
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# # client_id = os.getenv('SPOTIPY_CLIENT_ID')
|
| 30 |
-
# # client_secret = os.getenv('SPOTIPY_CLIENT_SECRET')
|
| 31 |
-
# # redirect_uri = os.getenv('SPOTIPY_REDIRECT_URI')
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# # scope= ['user-library-read',
|
| 35 |
-
# # 'user-read-playback-state',
|
| 36 |
-
# # 'user-modify-playback-state',
|
| 37 |
-
# # 'playlist-modify-public',
|
| 38 |
-
# # 'user-top-read']
|
| 39 |
-
|
| 40 |
-
# # refresh_token = "AQDVP_0CiwjPj1qshcRvASZNL1X93UgI7aDMgAyipI6g6_aTK6_-zRobqKALYbZrZkskBPpdyCwhQU7__2uP5dN7XEsNCepkj_etkH3RvDq4_TqVsl-xBskXjb2vap8Gf-U"
|
| 41 |
-
# # def switch_account():
|
| 42 |
-
# # new_auth_manager = SpotifyOAuth(client_id=client_id,
|
| 43 |
-
# # client_secret=client_secret,
|
| 44 |
-
# # redirect_uri=redirect_uri,
|
| 45 |
-
# # scope=scope) # Pass the refresh token here
|
| 46 |
-
# # new_sp = spotipy.Spotify(auth_manager=new_auth_manager)
|
| 47 |
-
# # global sp
|
| 48 |
-
# # sp = new_sp
|
| 49 |
-
# # devices = sp.devices()
|
| 50 |
-
# # print(devices)
|
| 51 |
-
# # # global device_id
|
| 52 |
-
# # # device_id = devices['devices'][0]['id']
|
| 53 |
-
# # return f"Account switched successfully. \n{new_auth_manager} \n{new_sp} \n{devices}"
|
| 54 |
-
|
| 55 |
-
# def find_track_by_name(track_name):
|
| 56 |
-
# """
|
| 57 |
-
# Searches track by name to get uri
|
| 58 |
-
# """
|
| 59 |
-
# results = sp.search(q=track_name, type='track')
|
| 60 |
-
# track_uri = results['tracks']['items'][0]['uri']
|
| 61 |
-
# return track_uri
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# def play_track_by_name(track_name):
|
| 65 |
-
# """
|
| 66 |
-
# Plays track given its name
|
| 67 |
-
# For less popular tracks, do 'play {track} {artist}'
|
| 68 |
-
# """
|
| 69 |
-
# track_uri = find_track_by_name(track_name)
|
| 70 |
-
# track_name = sp.track(track_uri)["name"]
|
| 71 |
-
# artist_name = sp.track(track_uri)['artists'][0]['name']
|
| 72 |
-
# try:
|
| 73 |
-
# sp.start_playback(device_id=device_id, uris=[track_uri])
|
| 74 |
-
# return f"♫ Now playing {track_name} by {artist_name} ♫"
|
| 75 |
-
# except SpotifyException as e:
|
| 76 |
-
# return f"An error occurred with Spotify: {e}. \n\n**Remember to wake up Spotify.**"
|
| 77 |
-
# except Exception as e:
|
| 78 |
-
# return f"An unexpected error occurred: {e}."
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
# demo = gr.Interface(fn=test, inputs=None, outputs="text")
|
| 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
|
|
@@ -161,15 +30,24 @@ def spotify_auth(client_id, client_secret, code=None):
|
|
| 161 |
sp = spotipy.Spotify(auth_manager=auth_manager)
|
| 162 |
devices = sp.devices()
|
| 163 |
device_id = devices['devices'][0]['id']
|
|
|
|
| 164 |
results = sp.search(q="No Pole", type='track')
|
| 165 |
track_uri = results['tracks']['items'][0]['uri']
|
| 166 |
sp.start_playback(device_id=device_id, uris=[track_uri])
|
| 167 |
-
|
|
|
|
| 168 |
else:
|
| 169 |
auth_url = auth_manager.get_authorize_url()
|
| 170 |
return f"Please authorize the app by clicking [here]({auth_url}) and then paste the code below."
|
| 171 |
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
def gradio_interface():
|
| 174 |
interface = gr.Interface(fn=spotify_auth,
|
| 175 |
inputs=[
|
|
@@ -178,7 +56,7 @@ def gradio_interface():
|
|
| 178 |
gr.Textbox(label="Code")
|
| 179 |
],
|
| 180 |
outputs=["markdown"],
|
| 181 |
-
description=
|
| 182 |
allow_flagging="never",
|
| 183 |
theme="finlaymacklon/boxy_violet"
|
| 184 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import spotipy
|
| 3 |
from spotipy.oauth2 import SpotifyOAuth
|
|
|
|
| 30 |
sp = spotipy.Spotify(auth_manager=auth_manager)
|
| 31 |
devices = sp.devices()
|
| 32 |
device_id = devices['devices'][0]['id']
|
| 33 |
+
|
| 34 |
results = sp.search(q="No Pole", type='track')
|
| 35 |
track_uri = results['tracks']['items'][0]['uri']
|
| 36 |
sp.start_playback(device_id=device_id, uris=[track_uri])
|
| 37 |
+
|
| 38 |
+
return f"Account switched successfully.\n\n{devices}"
|
| 39 |
else:
|
| 40 |
auth_url = auth_manager.get_authorize_url()
|
| 41 |
return f"Please authorize the app by clicking [here]({auth_url}) and then paste the code below."
|
| 42 |
|
| 43 |
|
| 44 |
+
description ="""
|
| 45 |
+
1. Enter Client ID & Client Secret
|
| 46 |
+
2. Click 'Submit' & follow output instructions
|
| 47 |
+
3. After pasting the code, click 'Submit' again
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
|
| 51 |
def gradio_interface():
|
| 52 |
interface = gr.Interface(fn=spotify_auth,
|
| 53 |
inputs=[
|
|
|
|
| 56 |
gr.Textbox(label="Code")
|
| 57 |
],
|
| 58 |
outputs=["markdown"],
|
| 59 |
+
description=description,
|
| 60 |
allow_flagging="never",
|
| 61 |
theme="finlaymacklon/boxy_violet"
|
| 62 |
)
|