Cache artist image on watchlist addition
When an artist is added to the watchlist, their image is now fetched from Spotify and cached in the database immediately if available. This improves user experience by ensuring artist images are present without requiring a separate fetch.
This commit is contained in:
parent
0519c58cd7
commit
bfabd26469
1 changed files with 30 additions and 4 deletions
|
|
@ -14990,6 +14990,32 @@ def add_to_watchlist():
|
||||||
success = database.add_artist_to_watchlist(artist_id, artist_name)
|
success = database.add_artist_to_watchlist(artist_id, artist_name)
|
||||||
|
|
||||||
if success:
|
if success:
|
||||||
|
# Fetch and cache artist image immediately from Spotify
|
||||||
|
try:
|
||||||
|
if spotify_client and spotify_client.is_authenticated():
|
||||||
|
artist_data = spotify_client.get_artist(artist_id)
|
||||||
|
if artist_data and 'images' in artist_data and artist_data['images']:
|
||||||
|
# Get medium-sized image (usually the second one, or first if only one)
|
||||||
|
image_url = None
|
||||||
|
if len(artist_data['images']) > 1:
|
||||||
|
image_url = artist_data['images'][1]['url']
|
||||||
|
else:
|
||||||
|
image_url = artist_data['images'][0]['url']
|
||||||
|
|
||||||
|
# Update in database
|
||||||
|
if image_url:
|
||||||
|
database.update_watchlist_artist_image(artist_id, image_url)
|
||||||
|
print(f"✅ Cached artist image for {artist_name}")
|
||||||
|
else:
|
||||||
|
print(f"⚠️ No image URL found for {artist_name}")
|
||||||
|
else:
|
||||||
|
print(f"⚠️ No images in Spotify data for {artist_name}")
|
||||||
|
else:
|
||||||
|
print(f"⚠️ Spotify client not available for fetching artist image")
|
||||||
|
except Exception as img_error:
|
||||||
|
# Don't fail the add operation if image fetch fails
|
||||||
|
print(f"⚠️ Could not fetch artist image for {artist_name}: {img_error}")
|
||||||
|
|
||||||
return jsonify({"success": True, "message": f"Added {artist_name} to watchlist"})
|
return jsonify({"success": True, "message": f"Added {artist_name} to watchlist"})
|
||||||
else:
|
else:
|
||||||
return jsonify({"success": False, "error": "Failed to add artist to watchlist"}), 500
|
return jsonify({"success": False, "error": "Failed to add artist to watchlist"}), 500
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue