feat(spotify): add Spotify search, prioritize Spotify in ranking and recommendations
This commit is contained in:
1 parent
fb1a5e01dd
commit
3db9bdb38b
6 files changed
+207
-14
No files matched your search
+39
-2
@@ -127,6 +127,43 @@ class WaveRecommender:
|
||||
except Exception as e:
|
||||
print(f"[WaveRecommender] Error processing anchor {anchor_name}: {e}")
|
||||
|
||||
# Randomize candidates order so the playlist feels fresh
|
||||
random.shuffle(candidates)
|
||||
# Sourcing C: Spotify top tracks for anchor artists (User preference: Spotify)
|
||||
try:
|
||||
from spotify_scraper import SpotifyClient
|
||||
with SpotifyClient() as sp_client:
|
||||
for anchor_name in selected_anchors:
|
||||
try:
|
||||
sp_search = sp_client.search(anchor_name, types=["artist"], limit=1)
|
||||
if hasattr(sp_search, "artists") and sp_search.artists:
|
||||
sp_art = sp_client.get_artist(sp_search.artists[0].id)
|
||||
if hasattr(sp_art, "top_tracks") and sp_art.top_tracks:
|
||||
for st in sp_art.top_tracks[:5]:
|
||||
st_art = ", ".join(a.name for a in st.artists) if st.artists else anchor_name
|
||||
key = f"{_normalize(st_art)}|{_normalize(st.name)}"
|
||||
if key in existing_keys or key in wave_keys or key in seen_keys:
|
||||
continue
|
||||
cover = None
|
||||
if hasattr(st, "images") and st.images:
|
||||
img = st.images[0]
|
||||
cover = getattr(img, "url", None) or (img.get("url") if isinstance(img, dict) else None)
|
||||
album_name = getattr(st.album, "name", "Singles") if getattr(st, "album", None) else "Singles"
|
||||
meta = TrackMetadata(
|
||||
service="spotify",
|
||||
service_id=str(st.id),
|
||||
title=st.name,
|
||||
artist=st_art,
|
||||
album=album_name,
|
||||
duration_ms=getattr(st, "duration_ms", None),
|
||||
cover_url=cover,
|
||||
is_explicit=getattr(st, "explicit", False),
|
||||
source_url=getattr(st, "url", None) or f"https://open.spotify.com/track/{st.id}"
|
||||
)
|
||||
seen_keys.add(key)
|
||||
# Insert Spotify tracks at the beginning so they are preferred
|
||||
candidates.insert(0, meta)
|
||||
except Exception as err:
|
||||
print(f"[WaveRecommender] Spotify sourcing notice for {anchor_name}: {err}")
|
||||
except Exception as e:
|
||||
print(f"[WaveRecommender] Spotify client notice: {e}")
|
||||
|
||||
return candidates[:target_count]
|
||||
Reference in new issue
Block a user