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}")
|
print(f"\n[{i}/{len(BANDS)}] Searching for: {band}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Search for songs by this band
|
# First, search for the artist to find the correct band
|
||||||
search_results = yt.search(band, filter="songs", limit=SONGS_PER_BAND * 2)
|
artist_results = yt.search(band, filter="artists", limit=1)
|
||||||
|
|
||||||
if not search_results:
|
|
||||||
print(f" ⚠ No results found for {band}")
|
|
||||||
failed_bands.append(band)
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Filter to get top songs and avoid duplicates
|
|
||||||
songs_to_add = []
|
songs_to_add = []
|
||||||
for result in search_results:
|
song_info_list = [] # Store song info for display
|
||||||
video_id = result.get("videoId")
|
|
||||||
if video_id and video_id not in added_video_ids:
|
if not artist_results:
|
||||||
songs_to_add.append(video_id)
|
# Fallback: try searching for songs if artist search fails
|
||||||
added_video_ids.add(video_id)
|
print(f" ⚠ No artist found for {band}, trying song search...")
|
||||||
if len(songs_to_add) >= SONGS_PER_BAND:
|
search_results = yt.search(band, filter="songs", limit=SONGS_PER_BAND * 2)
|
||||||
break
|
|
||||||
|
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:
|
if not songs_to_add:
|
||||||
print(f" ⚠ No new songs to add for {band} (may be duplicates)")
|
print(f" ⚠ No new songs to add for {band} (may be duplicates)")
|
||||||
@@ -190,9 +242,14 @@ def main():
|
|||||||
try:
|
try:
|
||||||
yt.add_playlist_items(playlist_id, songs_to_add)
|
yt.add_playlist_items(playlist_id, songs_to_add)
|
||||||
print(f" ✓ Added {len(songs_to_add)} song(s):")
|
print(f" ✓ Added {len(songs_to_add)} song(s):")
|
||||||
for result in search_results[:len(songs_to_add)]:
|
for song_info in song_info_list[:len(songs_to_add)]:
|
||||||
title = result.get("title", "Unknown")
|
title = song_info.get("title", "Unknown")
|
||||||
artist = result.get("artists", [{}])[0].get("name", "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}")
|
print(f" - {title} by {artist}")
|
||||||
total_added += len(songs_to_add)
|
total_added += len(songs_to_add)
|
||||||
added_successfully = True
|
added_successfully = True
|
||||||
|
|||||||
Reference in New Issue
Block a user