Use artist filter to find bands instead of song search
- Search for artists first using filter='artists' to find the correct band - Get songs from the artist using get_artist() to avoid matching song titles - Fallback to song search if artist search fails - Fixes issue where 'WATCH ME RISE' matched songs instead of the artist
This commit is contained in:
+76
-19
@@ -159,23 +159,75 @@ def main():
|
||||
print(f"\n[{i}/{len(BANDS)}] Searching for: {band}")
|
||||
|
||||
try:
|
||||
# Search for songs by this band
|
||||
search_results = yt.search(band, filter="songs", limit=SONGS_PER_BAND * 2)
|
||||
|
||||
if not search_results:
|
||||
print(f" ⚠ No results found for {band}")
|
||||
failed_bands.append(band)
|
||||
continue
|
||||
|
||||
# Filter to get top songs and avoid duplicates
|
||||
# First, search for the artist to find the correct band
|
||||
artist_results = yt.search(band, filter="artists", limit=1)
|
||||
songs_to_add = []
|
||||
for result in search_results:
|
||||
video_id = result.get("videoId")
|
||||
if video_id and video_id not in added_video_ids:
|
||||
songs_to_add.append(video_id)
|
||||
added_video_ids.add(video_id)
|
||||
if len(songs_to_add) >= SONGS_PER_BAND:
|
||||
break
|
||||
song_info_list = [] # Store song info for display
|
||||
|
||||
if not artist_results:
|
||||
# Fallback: try searching for songs if artist search fails
|
||||
print(f" ⚠ No artist found for {band}, trying song search...")
|
||||
search_results = yt.search(band, filter="songs", limit=SONGS_PER_BAND * 2)
|
||||
|
||||
if not search_results:
|
||||
print(f" ⚠ No results found for {band}")
|
||||
failed_bands.append(band)
|
||||
continue
|
||||
|
||||
# Filter to get top songs and avoid duplicates
|
||||
for result in search_results:
|
||||
video_id = result.get("videoId")
|
||||
if video_id and video_id not in added_video_ids:
|
||||
songs_to_add.append(video_id)
|
||||
song_info_list.append(result) # Store for display
|
||||
added_video_ids.add(video_id)
|
||||
if len(songs_to_add) >= SONGS_PER_BAND:
|
||||
break
|
||||
else:
|
||||
# Found artist, get their songs
|
||||
artist_browse_id = artist_results[0].get("browseId")
|
||||
if not artist_browse_id:
|
||||
print(f" ⚠ No browseId found for artist {band}")
|
||||
failed_bands.append(band)
|
||||
continue
|
||||
|
||||
# Get artist information which includes songs
|
||||
try:
|
||||
artist_info = yt.get_artist(artist_browse_id)
|
||||
# Get songs from the artist
|
||||
artist_songs = artist_info.get("songs", {}).get("results", [])
|
||||
|
||||
if not artist_songs:
|
||||
print(f" ⚠ No songs found for artist {band}")
|
||||
failed_bands.append(band)
|
||||
continue
|
||||
|
||||
# Filter to get top songs and avoid duplicates
|
||||
for song in artist_songs:
|
||||
video_id = song.get("videoId")
|
||||
if video_id and video_id not in added_video_ids:
|
||||
songs_to_add.append(video_id)
|
||||
song_info_list.append(song) # Store for display
|
||||
added_video_ids.add(video_id)
|
||||
if len(songs_to_add) >= SONGS_PER_BAND:
|
||||
break
|
||||
except Exception as e:
|
||||
print(f" ⚠ Error getting artist info: {e}, falling back to song search...")
|
||||
# Fallback to song search
|
||||
search_results = yt.search(band, filter="songs", limit=SONGS_PER_BAND * 2)
|
||||
if not search_results:
|
||||
print(f" ⚠ No results found for {band}")
|
||||
failed_bands.append(band)
|
||||
continue
|
||||
|
||||
for result in search_results:
|
||||
video_id = result.get("videoId")
|
||||
if video_id and video_id not in added_video_ids:
|
||||
songs_to_add.append(video_id)
|
||||
song_info_list.append(result) # Store for display
|
||||
added_video_ids.add(video_id)
|
||||
if len(songs_to_add) >= SONGS_PER_BAND:
|
||||
break
|
||||
|
||||
if not songs_to_add:
|
||||
print(f" ⚠ No new songs to add for {band} (may be duplicates)")
|
||||
@@ -190,9 +242,14 @@ def main():
|
||||
try:
|
||||
yt.add_playlist_items(playlist_id, songs_to_add)
|
||||
print(f" ✓ Added {len(songs_to_add)} song(s):")
|
||||
for result in search_results[:len(songs_to_add)]:
|
||||
title = result.get("title", "Unknown")
|
||||
artist = result.get("artists", [{}])[0].get("name", "Unknown")
|
||||
for song_info in song_info_list[:len(songs_to_add)]:
|
||||
title = song_info.get("title", "Unknown")
|
||||
# Handle both artist list format and single artist format
|
||||
artists = song_info.get("artists", [])
|
||||
if isinstance(artists, list) and len(artists) > 0:
|
||||
artist = artists[0].get("name", "Unknown") if isinstance(artists[0], dict) else str(artists[0])
|
||||
else:
|
||||
artist = song_info.get("artist", "Unknown")
|
||||
print(f" - {title} by {artist}")
|
||||
total_added += len(songs_to_add)
|
||||
added_successfully = True
|
||||
|
||||
Reference in New Issue
Block a user