Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -113,25 +113,75 @@ def get_track_info(token, track_url):
|
|
| 113 |
return [response.json()]
|
| 114 |
return []
|
| 115 |
|
| 116 |
-
def
|
| 117 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
tracks_info = []
|
|
|
|
|
|
|
| 119 |
for track in tracks:
|
| 120 |
if not track:
|
| 121 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
tracks_info.append({
|
| 124 |
-
'artist':
|
| 125 |
'title': track.get('name', 'Unknown'),
|
| 126 |
'album': track.get('album', {}).get('name', 'Unknown'),
|
| 127 |
'isrc': track.get('external_ids', {}).get('isrc', 'Not available'),
|
| 128 |
-
'
|
|
|
|
|
|
|
|
|
|
| 129 |
'release_date': track.get('album', {}).get('release_date', 'Not available'),
|
| 130 |
-
'
|
|
|
|
|
|
|
| 131 |
'spotify_url': track.get('external_urls', {}).get('spotify', 'Not available'),
|
| 132 |
'preview_url': track.get('preview_url', 'Not available'),
|
| 133 |
'playlist_source': getattr(track, 'playlist_source', 'Direct Track')
|
| 134 |
})
|
|
|
|
| 135 |
return tracks_info
|
| 136 |
|
| 137 |
# Main Interface Function
|
|
@@ -192,8 +242,8 @@ def interface(project_name, spotify_urls, include_all_info=True):
|
|
| 192 |
error_message = "Could not find any tracks in the provided URLs."
|
| 193 |
return gr.Dataframe(value=pd.DataFrame({"Error": [error_message]})), None
|
| 194 |
|
| 195 |
-
# Extract track details
|
| 196 |
-
tracks_info = extract_track_details(all_tracks)
|
| 197 |
|
| 198 |
# Remove duplicate tracks (based on ISRC or title+artist if ISRC not available)
|
| 199 |
df = pd.DataFrame(tracks_info)
|
|
@@ -212,7 +262,7 @@ def interface(project_name, spotify_urls, include_all_info=True):
|
|
| 212 |
|
| 213 |
# Filter columns if not include_all_info
|
| 214 |
if not include_all_info:
|
| 215 |
-
columns_to_keep = ['artist', 'title', 'album', 'release_date', '
|
| 216 |
df = df[columns_to_keep]
|
| 217 |
|
| 218 |
# Save DataFrame to an Excel file
|
|
|
|
| 113 |
return [response.json()]
|
| 114 |
return []
|
| 115 |
|
| 116 |
+
def get_artist_details(token, artist_id):
|
| 117 |
+
"""Get artist information including genres."""
|
| 118 |
+
headers = {'Authorization': f'Bearer {token}'}
|
| 119 |
+
url = f'https://api.spotify.com/v1/artists/{artist_id}'
|
| 120 |
+
|
| 121 |
+
response = make_request_with_retry(url, headers)
|
| 122 |
+
if response:
|
| 123 |
+
return response.json()
|
| 124 |
+
return None
|
| 125 |
+
|
| 126 |
+
def extract_track_details(tracks, token):
|
| 127 |
+
"""Extract relevant information from track objects including artist details."""
|
| 128 |
tracks_info = []
|
| 129 |
+
artists_cache = {} # Cache artist details to reduce API calls
|
| 130 |
+
|
| 131 |
for track in tracks:
|
| 132 |
if not track:
|
| 133 |
continue
|
| 134 |
+
|
| 135 |
+
# Get artist details if we have an artist ID
|
| 136 |
+
artist_id = None
|
| 137 |
+
artist_name = 'Unknown'
|
| 138 |
+
genres = []
|
| 139 |
+
|
| 140 |
+
if track.get('artists') and len(track['artists']) > 0:
|
| 141 |
+
artist_id = track['artists'][0].get('id')
|
| 142 |
+
artist_name = track['artists'][0].get('name', 'Unknown')
|
| 143 |
+
|
| 144 |
+
# Only fetch artist details if we have an ID and haven't cached it already
|
| 145 |
+
if artist_id and artist_id not in artists_cache:
|
| 146 |
+
artist_data = get_artist_details(token, artist_id)
|
| 147 |
+
if artist_data:
|
| 148 |
+
artists_cache[artist_id] = {
|
| 149 |
+
'genres': artist_data.get('genres', []),
|
| 150 |
+
'popularity': artist_data.get('popularity', 'Not available'),
|
| 151 |
+
'followers': artist_data.get('followers', {}).get('total', 'Not available')
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
# Get genres from cache
|
| 155 |
+
if artist_id in artists_cache:
|
| 156 |
+
genres = artists_cache[artist_id].get('genres', [])
|
| 157 |
+
|
| 158 |
+
# Calculate duration in minutes:seconds format
|
| 159 |
+
duration_ms = track.get('duration_ms', 0)
|
| 160 |
+
if duration_ms:
|
| 161 |
+
minutes = duration_ms // 60000
|
| 162 |
+
seconds = (duration_ms % 60000) // 1000
|
| 163 |
+
duration_formatted = f"{minutes}:{seconds:02d}"
|
| 164 |
+
else:
|
| 165 |
+
duration_formatted = 'Not available'
|
| 166 |
|
| 167 |
tracks_info.append({
|
| 168 |
+
'artist': artist_name,
|
| 169 |
'title': track.get('name', 'Unknown'),
|
| 170 |
'album': track.get('album', {}).get('name', 'Unknown'),
|
| 171 |
'isrc': track.get('external_ids', {}).get('isrc', 'Not available'),
|
| 172 |
+
'track_popularity': track.get('popularity', 'Not available'),
|
| 173 |
+
'genres': ', '.join(genres) if genres else 'Not available',
|
| 174 |
+
'artist_popularity': artists_cache.get(artist_id, {}).get('popularity', 'Not available') if artist_id else 'Not available',
|
| 175 |
+
'artist_followers': artists_cache.get(artist_id, {}).get('followers', 'Not available') if artist_id else 'Not available',
|
| 176 |
'release_date': track.get('album', {}).get('release_date', 'Not available'),
|
| 177 |
+
'duration': duration_formatted,
|
| 178 |
+
'duration_ms': duration_ms,
|
| 179 |
+
'explicit': 'Yes' if track.get('explicit', False) else 'No',
|
| 180 |
'spotify_url': track.get('external_urls', {}).get('spotify', 'Not available'),
|
| 181 |
'preview_url': track.get('preview_url', 'Not available'),
|
| 182 |
'playlist_source': getattr(track, 'playlist_source', 'Direct Track')
|
| 183 |
})
|
| 184 |
+
|
| 185 |
return tracks_info
|
| 186 |
|
| 187 |
# Main Interface Function
|
|
|
|
| 242 |
error_message = "Could not find any tracks in the provided URLs."
|
| 243 |
return gr.Dataframe(value=pd.DataFrame({"Error": [error_message]})), None
|
| 244 |
|
| 245 |
+
# Extract track details including artist information
|
| 246 |
+
tracks_info = extract_track_details(all_tracks, token_spotify)
|
| 247 |
|
| 248 |
# Remove duplicate tracks (based on ISRC or title+artist if ISRC not available)
|
| 249 |
df = pd.DataFrame(tracks_info)
|
|
|
|
| 262 |
|
| 263 |
# Filter columns if not include_all_info
|
| 264 |
if not include_all_info:
|
| 265 |
+
columns_to_keep = ['artist', 'title', 'album', 'genres', 'release_date', 'track_popularity', 'explicit', 'spotify_url']
|
| 266 |
df = df[columns_to_keep]
|
| 267 |
|
| 268 |
# Save DataFrame to an Excel file
|