From bfabd26469d0ad1db2911ea28f042b7a50cccb18 Mon Sep 17 00:00:00 2001 From: Broque Thomas Date: Tue, 2 Dec 2025 16:26:18 -0800 Subject: [PATCH] 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. --- web_server.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/web_server.py b/web_server.py index 56d0e7c..4999957 100644 --- a/web_server.py +++ b/web_server.py @@ -14982,18 +14982,44 @@ def add_to_watchlist(): data = request.get_json() artist_id = data.get('artist_id') artist_name = data.get('artist_name') - + if not artist_id or not artist_name: return jsonify({"success": False, "error": "Missing artist_id or artist_name"}), 400 - + database = get_database() success = database.add_artist_to_watchlist(artist_id, artist_name) - + 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"}) else: return jsonify({"success": False, "error": "Failed to add artist to watchlist"}), 500 - + except Exception as e: print(f"Error adding to watchlist: {e}") return jsonify({"success": False, "error": str(e)}), 500