File size: 13,602 Bytes
20521cb
 
 
53d7f84
 
 
 
20521cb
53d7f84
 
 
 
20521cb
 
2d033ff
20521cb
53d7f84
20521cb
2d033ff
53d7f84
20521cb
53d7f84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20521cb
53d7f84
20521cb
53d7f84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d033ff
53d7f84
 
2d033ff
53d7f84
 
 
 
2d033ff
53d7f84
 
 
2d033ff
53d7f84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d033ff
 
53d7f84
2d033ff
53d7f84
2d033ff
53d7f84
2d033ff
 
53d7f84
7a38972
53d7f84
2d033ff
 
 
 
 
 
 
53d7f84
 
2d033ff
53d7f84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a38972
2d033ff
53d7f84
2d033ff
53d7f84
2d033ff
 
 
53d7f84
 
2d033ff
53d7f84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a38972
2d033ff
53d7f84
2d033ff
53d7f84
2d033ff
 
53d7f84
2d033ff
53d7f84
 
2d033ff
53d7f84
 
 
 
2d033ff
53d7f84
 
2d033ff
53d7f84
2d033ff
 
 
 
 
 
53d7f84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d033ff
53d7f84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a38972
2d033ff
53d7f84
2d033ff
53d7f84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20521cb
 
53d7f84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import os
import requests
import gradio as gr
import urllib.parse
import base64
import secrets
import hashlib

# Spotify API credentials
CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID")
CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRET")
REDIRECT_URI = os.getenv("REDIRECT_URI", "https://lenpanda-spotify-mcp.hf.space")
SPOTIFY_AUTH_URL = "https://accounts.spotify.com/authorize"
SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token"
SCOPE = "playlist-modify-public playlist-modify-private user-read-private"

# Global variables to store auth state
access_token = None
user_info = None
auth_state = None

def generate_auth_url():
    """Generate Spotify authorization URL"""
    global auth_state
    auth_state = secrets.token_urlsafe(32)
    
    params = {
        'client_id': CLIENT_ID,
        'response_type': 'code',
        'redirect_uri': REDIRECT_URI,
        'scope': SCOPE,
        'state': auth_state,
        'show_dialog': 'true'
    }
    
    auth_url = f"{SPOTIFY_AUTH_URL}?{urllib.parse.urlencode(params)}"
    
    return f"""
## 🎡 Spotify Authentication Required

To use this app, you need to authenticate with Spotify:

1. **Click the link below** to authorize this app:
   
   **[πŸ”— Login with Spotify]({auth_url})**

2. **After authorization**, you'll be redirected back with a code in the URL
3. **Copy the entire URL** from your browser's address bar
4. **Paste it** in the "Authorization URL" field below
5. **Click "Complete Authentication"**

### Current Status: ❌ Not Authenticated
"""

def complete_auth(auth_url_input):
    """Complete authentication using the callback URL"""
    global access_token, user_info, auth_state
    
    if not auth_url_input or not auth_url_input.strip():
        return "❌ Please paste the callback URL from Spotify", ""
    
    try:
        # Parse the URL to extract code and state
        parsed_url = urllib.parse.urlparse(auth_url_input)
        params = urllib.parse.parse_qs(parsed_url.query)
        
        code = params.get('code', [None])[0]
        state = params.get('state', [None])[0]
        error = params.get('error', [None])[0]
        
        if error:
            return f"❌ Spotify authentication error: {error}", ""
        
        if not code:
            return "❌ No authorization code found in URL. Please make sure you copied the complete callback URL.", ""
        
        if state != auth_state:
            return "❌ Invalid state parameter. Please try the authentication process again.", ""
        
        # Exchange code for access token
        token_data = {
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": REDIRECT_URI,
            "client_id": CLIENT_ID,
            "client_secret": CLIENT_SECRET
        }
        
        response = requests.post(SPOTIFY_TOKEN_URL, data=token_data)
        token_json = response.json()
        
        if response.status_code == 200:
            access_token = token_json.get("access_token")
            
            # Get user info
            headers = {"Authorization": f"Bearer {access_token}"}
            user_response = requests.get("https://api.spotify.com/v1/me", headers=headers)
            
            if user_response.status_code == 200:
                user_info = user_response.json()
                success_msg = f"βœ… Successfully authenticated as: **{user_info.get('display_name', 'Unknown User')}**"
                return success_msg, success_msg
            else:
                return "βœ… Authentication successful, but couldn't get user info.", "βœ… Authenticated"
        else:
            error_msg = token_json.get('error_description', 'Failed to get access token')
            return f"❌ Token exchange failed: {error_msg}", ""
            
    except Exception as e:
        return f"❌ Error processing authentication: {str(e)}", ""

def get_auth_status():
    """Get current authentication status"""
    if access_token and user_info:
        return f"βœ… Authenticated as: **{user_info.get('display_name', 'Unknown User')}**"
    else:
        return "❌ Not authenticated"

def search_songs(query):
    """Search for songs on Spotify"""
    if not access_token:
        return "❌ Please authenticate first!"
    
    if not query.strip():
        return "Please enter a search query."
    
    headers = {"Authorization": f"Bearer {access_token}"}
    params = {"q": query, "type": "track", "limit": 10}
    
    try:
        response = requests.get("https://api.spotify.com/v1/search", headers=headers, params=params)
        
        if response.status_code == 200:
            data = response.json()
            tracks = data.get("tracks", {}).get("items", [])
            
            if not tracks:
                return "No tracks found."
            
            result = "🎡 **Search Results:**\n\n"
            for i, track in enumerate(tracks, 1):
                artists = ", ".join([artist["name"] for artist in track["artists"]])
                result += f"**{i}. {track['name']}** by {artists}\n"
                result += f"   Album: {track['album']['name']}\n"
                result += f"   URI: `{track['uri']}`\n"
                result += f"   Preview: {track.get('preview_url', 'Not available')}\n\n"
            
            return result
        else:
            error_msg = response.json().get('error', {}).get('message', 'Unknown error')
            return f"❌ Error searching: {error_msg}"
    except Exception as e:
        return f"❌ Search error: {str(e)}"

def get_user_playlists():
    """Get user's playlists"""
    if not access_token:
        return "❌ Please authenticate first!"
    
    headers = {"Authorization": f"Bearer {access_token}"}
    
    try:
        response = requests.get("https://api.spotify.com/v1/me/playlists", headers=headers)
        
        if response.status_code == 200:
            data = response.json()
            playlists = data.get("items", [])
            
            if not playlists:
                return "No playlists found."
            
            result = "πŸ“‹ **Your Playlists:**\n\n"
            for playlist in playlists:
                result += f"**{playlist['name']}**\n"
                result += f"   ID: `{playlist['id']}`\n"
                result += f"   Tracks: {playlist['tracks']['total']}\n"
                result += f"   Public: {'Yes' if playlist['public'] else 'No'}\n\n"
            
            return result
        else:
            error_msg = response.json().get('error', {}).get('message', 'Unknown error')
            return f"❌ Error getting playlists: {error_msg}"
    except Exception as e:
        return f"❌ Playlist error: {str(e)}"

def add_song_to_playlist(playlist_id, track_uri):
    """Add a song to a playlist"""
    if not access_token:
        return "❌ Please authenticate first!"
    
    if not playlist_id.strip() or not track_uri.strip():
        return "❌ Please provide both playlist ID and track URI."
    
    # Clean and format track URI
    track_uri = track_uri.strip()
    if not track_uri.startswith("spotify:track:"):
        if "open.spotify.com/track/" in track_uri:
            track_id = track_uri.split("/track/")[1].split("?")[0]
            track_uri = f"spotify:track:{track_id}"
        elif len(track_uri) == 22:  # Spotify track ID length
            track_uri = f"spotify:track:{track_uri}"
        else:
            return "❌ Invalid track URI format. Use spotify:track:... or Spotify URL"
    
    url = f"https://api.spotify.com/v1/playlists/{playlist_id.strip()}/tracks"
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json"
    }
    data = {"uris": [track_uri]}
    
    try:
        response = requests.post(url, headers=headers, json=data)
        
        if response.status_code == 201:
            return "βœ… Song added to playlist successfully!"
        else:
            error_data = response.json().get('error', {})
            error_msg = error_data.get('message', 'Unknown error')
            return f"❌ Error adding song: {error_msg}"
    except Exception as e:
        return f"❌ Add song error: {str(e)}"

def create_playlist(playlist_name, description="", public=True):
    """Create a new playlist"""
    if not access_token:
        return "❌ Please authenticate first!"
    
    if not playlist_name.strip():
        return "❌ Please provide a playlist name."
    
    if not user_info:
        return "❌ User information not available."
    
    url = f"https://api.spotify.com/v1/users/{user_info['id']}/playlists"
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json"
    }
    data = {
        "name": playlist_name.strip(),
        "description": description.strip(),
        "public": public
    }
    
    try:
        response = requests.post(url, headers=headers, json=data)
        
        if response.status_code == 201:
            playlist = response.json()
            return f"βœ… Playlist created successfully!\n\n**{playlist['name']}**\nID: `{playlist['id']}`\nURL: {playlist['external_urls']['spotify']}"
        else:
            error_data = response.json().get('error', {})
            error_msg = error_data.get('message', 'Unknown error')
            return f"❌ Error creating playlist: {error_msg}"
    except Exception as e:
        return f"❌ Create playlist error: {str(e)}"

# Create Gradio interface
with gr.Blocks(title="🎡 Spotify Playlist Manager", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🎡 Spotify Playlist Manager")
    
    with gr.Tabs():
        # Authentication Tab
        with gr.TabItem("πŸ” Authentication"):
            auth_instructions = gr.Markdown(generate_auth_url())
            
            with gr.Row():
                auth_url_input = gr.Textbox(
                    label="Paste the callback URL here after authorizing",
                    placeholder="https://lenpanda-spotify-mcp.hf.space/?code=...",
                    lines=2
                )
            
            with gr.Row():
                complete_auth_btn = gr.Button("Complete Authentication", variant="primary")
                refresh_auth_btn = gr.Button("Refresh Auth Status")
            
            auth_status = gr.Markdown("❌ Not authenticated")
            
            complete_auth_btn.click(
                complete_auth,
                inputs=auth_url_input,
                outputs=[gr.Markdown(), auth_status]
            )
            
            refresh_auth_btn.click(
                get_auth_status,
                outputs=auth_status
            )
        
        # Search Tab
        with gr.TabItem("πŸ” Search Songs"):
            gr.Markdown("Search for songs on Spotify")
            
            with gr.Row():
                search_input = gr.Textbox(
                    label="Search for songs",
                    placeholder="Enter song name, artist, or album"
                )
                search_btn = gr.Button("Search", variant="primary")
            
            search_output = gr.Markdown("Enter a search query above")
            
            search_btn.click(search_songs, inputs=search_input, outputs=search_output)
        
        # Playlists Tab
        with gr.TabItem("πŸ“‹ Playlists"):
            gr.Markdown("View and manage your playlists")
            
            with gr.Row():
                get_playlists_btn = gr.Button("Get My Playlists", variant="primary")
            
            playlists_output = gr.Markdown("Click the button above to load your playlists")
            
            get_playlists_btn.click(get_user_playlists, outputs=playlists_output)
        
        # Add Song Tab
        with gr.TabItem("βž• Add Song"):
            gr.Markdown("Add a song to one of your playlists")
            
            playlist_id_input = gr.Textbox(
                label="Playlist ID",
                placeholder="Get this from the Playlists tab"
            )
            
            track_uri_input = gr.Textbox(
                label="Track URI or Spotify URL",
                placeholder="spotify:track:... or https://open.spotify.com/track/..."
            )
            
            add_song_btn = gr.Button("Add Song to Playlist", variant="primary")
            add_song_output = gr.Markdown("")
            
            add_song_btn.click(
                add_song_to_playlist,
                inputs=[playlist_id_input, track_uri_input],
                outputs=add_song_output
            )
        
        # Create Playlist Tab
        with gr.TabItem("βž• Create Playlist"):
            gr.Markdown("Create a new playlist")
            
            playlist_name_input = gr.Textbox(
                label="Playlist Name",
                placeholder="My Awesome Playlist"
            )
            
            playlist_desc_input = gr.Textbox(
                label="Description (optional)",
                placeholder="A description for your playlist"
            )
            
            playlist_public = gr.Checkbox(label="Make playlist public", value=True)
            
            create_playlist_btn = gr.Button("Create Playlist", variant="primary")
            create_playlist_output = gr.Markdown("")
            
            create_playlist_btn.click(
                create_playlist,
                inputs=[playlist_name_input, playlist_desc_input, playlist_public],
                outputs=create_playlist_output
            )

if __name__ == "__main__":
    demo.launch()