Last active
November 18, 2024 15:32
-
-
Save MartinWeiss12/1bc47cc0b1854574879a924c7e584233 to your computer and use it in GitHub Desktop.
Get Album Image URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
track_uris = cleaned_df['Track URI'] | |
unique_track_uris = list(set(track_uris)) | |
n_unique_track_sublists = len(unique_track_uris) // 20 + (len(unique_track_uris) % 20 > 0) | |
unique_track_sublists = [unique_track_uris[i * 20 : (i+1) * 20] for i in range(n_unique_track_sublists)] | |
track_artist_album_df = pd.DataFrame(columns=['Track URI', 'Artist URI', 'Album URI', 'Album Image URL']) | |
start_time = time.time() | |
for track_list in unique_track_sublists: | |
params = {'ids': ','.join(track_list)} | |
response = requests.get('https://api.spotify.com/v1/tracks', headers=headers, params=params) | |
if response.status_code == 200: | |
data = response.json() | |
for track in data['tracks']: | |
try: | |
track_uri = track['uri'].replace('spotify:track:', '') | |
artist_uri = track['artists'][0]['id'] | |
album_uri = track['album']['uri'].replace('spotify:album:', '') | |
album_image_url = track['album']['images'][0]['url'] | |
new_row = pd.Series({'Track URI': track_uri, | |
'Artist URI': artist_uri, 'Album URI': album_uri, | |
'Album Image URL': album_image_url | |
}) | |
track_artist_album_df = pd.concat([track_artist_album_df, | |
new_row.to_frame().T], | |
ignore_index=True | |
) | |
except: | |
continue | |
elapsed_time = time.time() - start_time | |
if elapsed_time >= 27: | |
print('Waiting for 32 seconds...') | |
time.sleep(32) | |
start_time = time.time() | |
print('Timer reset.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment