Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,44 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import time
|
|
@@ -7,11 +47,15 @@ import os
|
|
| 7 |
from tempfile import NamedTemporaryFile
|
| 8 |
|
| 9 |
# Spotify API credentials from environment variables
|
| 10 |
-
client_ids = os.getenv("SPOTIFY_CLIENT_IDS")
|
| 11 |
-
client_secrets = os.getenv("SPOTIFY_CLIENT_SECRETS")
|
| 12 |
|
|
|
|
| 13 |
if not client_ids or not client_secrets:
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
client_ids = client_ids.split(',')
|
| 17 |
client_secrets = client_secrets.split(',')
|
|
@@ -22,16 +66,28 @@ total_requests = 0
|
|
| 22 |
|
| 23 |
# Spotify Functions
|
| 24 |
def get_token(client_id, client_secret):
|
|
|
|
| 25 |
url = 'https://accounts.spotify.com/api/token'
|
| 26 |
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
| 27 |
payload = {'grant_type': 'client_credentials'}
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
return None
|
| 36 |
|
| 37 |
def handle_rate_limit(response, attempt):
|
|
@@ -286,6 +342,8 @@ def interface(project_name, spotify_urls, include_all_info=True):
|
|
| 286 |
if not project_name:
|
| 287 |
project_name = "spotify_tracks"
|
| 288 |
|
|
|
|
|
|
|
| 289 |
# Split and clean URLs
|
| 290 |
urls_list = [url.strip() for url in spotify_urls.strip().split('\n') if url.strip()]
|
| 291 |
|
|
@@ -310,8 +368,24 @@ def interface(project_name, spotify_urls, include_all_info=True):
|
|
| 310 |
# Get token
|
| 311 |
token_spotify = get_token(client_ids[current_api_index], client_secrets[current_api_index])
|
| 312 |
if not token_spotify:
|
| 313 |
-
|
| 314 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
print(f"Successfully authenticated with Spotify API")
|
| 317 |
|
|
|
|
| 1 |
+
# For demo/testing mode when no real API credentials are available
|
| 2 |
+
DEMO_DATA = [
|
| 3 |
+
{
|
| 4 |
+
'artist': 'Demo Artist 1',
|
| 5 |
+
'title': 'Demo Track 1',
|
| 6 |
+
'album': 'Demo Album',
|
| 7 |
+
'isrc': 'ABCDE1234567',
|
| 8 |
+
'track_popularity': 75,
|
| 9 |
+
'genres': 'Pop, Electronic',
|
| 10 |
+
'artist_popularity': 80,
|
| 11 |
+
'artist_followers': 1000000,
|
| 12 |
+
'release_date': '2024-01-01',
|
| 13 |
+
'duration': '3:45',
|
| 14 |
+
'duration_ms': 225000,
|
| 15 |
+
'explicit': 'No',
|
| 16 |
+
'spotify_url': 'https://open.spotify.com/track/demo1',
|
| 17 |
+
'preview_url': 'Not available',
|
| 18 |
+
'playlist_source': 'Demo Playlist'
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
'artist': 'Demo Artist 2',
|
| 22 |
+
'title': 'Demo Track 2',
|
| 23 |
+
'album': 'Another Demo Album',
|
| 24 |
+
'isrc': 'FGHIJ7654321',
|
| 25 |
+
'track_popularity': 65,
|
| 26 |
+
'genres': 'Rock, Alternative',
|
| 27 |
+
'artist_popularity': 70,
|
| 28 |
+
'artist_followers': 750000,
|
| 29 |
+
'release_date': '2024-02-15',
|
| 30 |
+
'duration': '4:20',
|
| 31 |
+
'duration_ms': 260000,
|
| 32 |
+
'explicit': 'Yes',
|
| 33 |
+
'spotify_url': 'https://open.spotify.com/track/demo2',
|
| 34 |
+
'preview_url': 'Not available',
|
| 35 |
+
'playlist_source': 'Demo Playlist'
|
| 36 |
+
}
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
def get_demo_data():
|
| 40 |
+
"""Returns demo data when not using real API credentials."""
|
| 41 |
+
return DEMO_DATAimport gradio as gr
|
| 42 |
import requests
|
| 43 |
import pandas as pd
|
| 44 |
import time
|
|
|
|
| 47 |
from tempfile import NamedTemporaryFile
|
| 48 |
|
| 49 |
# Spotify API credentials from environment variables
|
| 50 |
+
client_ids = os.getenv("SPOTIFY_CLIENT_IDS", "")
|
| 51 |
+
client_secrets = os.getenv("SPOTIFY_CLIENT_SECRETS", "")
|
| 52 |
|
| 53 |
+
# For testing/demo purposes only - DO NOT use in production
|
| 54 |
if not client_ids or not client_secrets:
|
| 55 |
+
print("WARNING: No API credentials found in environment variables. Using demo mode with limited functionality.")
|
| 56 |
+
# Provide fake credentials for demo purposes
|
| 57 |
+
client_ids = "demo_client_id"
|
| 58 |
+
client_secrets = "demo_client_secret"
|
| 59 |
|
| 60 |
client_ids = client_ids.split(',')
|
| 61 |
client_secrets = client_secrets.split(',')
|
|
|
|
| 66 |
|
| 67 |
# Spotify Functions
|
| 68 |
def get_token(client_id, client_secret):
|
| 69 |
+
"""Get Spotify API token. Returns a demo token in case of failure for testing."""
|
| 70 |
url = 'https://accounts.spotify.com/api/token'
|
| 71 |
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
| 72 |
payload = {'grant_type': 'client_credentials'}
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
response = requests.post(url, headers=headers, data=payload, auth=(client_id, client_secret))
|
| 76 |
+
global total_requests
|
| 77 |
+
total_requests += 1
|
| 78 |
+
|
| 79 |
+
if response.status_code == 200:
|
| 80 |
+
print("Successfully obtained Spotify API token")
|
| 81 |
+
return response.json().get('access_token')
|
| 82 |
+
else:
|
| 83 |
+
print(f"Error getting token: {response.status_code} - {response.text}")
|
| 84 |
+
# For demo/testing only - simulate a token
|
| 85 |
+
if client_id == "demo_client_id":
|
| 86 |
+
print("Using demo token for testing purposes")
|
| 87 |
+
return "demo_token"
|
| 88 |
+
return None
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"Exception during token retrieval: {str(e)}")
|
| 91 |
return None
|
| 92 |
|
| 93 |
def handle_rate_limit(response, attempt):
|
|
|
|
| 342 |
if not project_name:
|
| 343 |
project_name = "spotify_tracks"
|
| 344 |
|
| 345 |
+
print(f"Starting to process request for project: {project_name}")
|
| 346 |
+
|
| 347 |
# Split and clean URLs
|
| 348 |
urls_list = [url.strip() for url in spotify_urls.strip().split('\n') if url.strip()]
|
| 349 |
|
|
|
|
| 368 |
# Get token
|
| 369 |
token_spotify = get_token(client_ids[current_api_index], client_secrets[current_api_index])
|
| 370 |
if not token_spotify:
|
| 371 |
+
if client_ids[0] == "demo_client_id":
|
| 372 |
+
print("Running in demo mode with sample data")
|
| 373 |
+
# In demo mode, return sample data
|
| 374 |
+
demo_data = get_demo_data()
|
| 375 |
+
df = pd.DataFrame(demo_data)
|
| 376 |
+
|
| 377 |
+
# Save DataFrame to an Excel file
|
| 378 |
+
tmpfile = NamedTemporaryFile(delete=False, suffix='.xlsx')
|
| 379 |
+
df.to_excel(tmpfile.name, index=False)
|
| 380 |
+
|
| 381 |
+
# Rename the file with the project name
|
| 382 |
+
project_file_name = f"{project_name}.xlsx"
|
| 383 |
+
shutil.move(tmpfile.name, project_file_name)
|
| 384 |
+
|
| 385 |
+
return df, project_file_name
|
| 386 |
+
else:
|
| 387 |
+
error_message = "Failed to authenticate with Spotify API. Please try again later."
|
| 388 |
+
return gr.Dataframe(value=pd.DataFrame({"Error": [error_message]})), None
|
| 389 |
|
| 390 |
print(f"Successfully authenticated with Spotify API")
|
| 391 |
|