Add Amazon buy list feature for missing songs

This commit is contained in:
nmemmert committed 2025-11-17 11:17:43 -05:00
1 parent be9a1a1b64
commit 2893487134
1 file changed
+28 -3
+28 -3
View File
@@ -120,11 +120,12 @@ def find_song_in_library(artist, title, library_path):
return None return None
def create_playlist_in_plex(plex, songs, playlist_name): def create_playlist_in_plex(plex, songs, playlist_name):
"""Create playlist in Plex""" """Create playlist in Plex and return added count and missing songs"""
# Assuming Plex has a music library named 'Music' # Assuming Plex has a music library named 'Music'
music_library = plex.library.section('Music') music_library = plex.library.section('Music')
tracks = [] tracks = []
seen_keys = set() # Track which ratingKeys we've already added to prevent duplicates within this run seen_keys = set() # Track which ratingKeys we've already added to prevent duplicates within this run
missing = []
for song in songs: for song in songs:
# Clean title and artist by removing text in parentheses and normalizing spaces # Clean title and artist by removing text in parentheses and normalizing spaces
@@ -176,8 +177,9 @@ def create_playlist_in_plex(plex, songs, playlist_name):
break break
# If no artist match, skip this track (don't add wrong songs) # If no artist match, skip this track (don't add wrong songs)
if not matched: if not matched:
pass # Don't add if artist doesn't match # Take the first match missing.append(song)
added = 0
if tracks: if tracks:
try: try:
# Try to find existing playlist # Try to find existing playlist
@@ -193,18 +195,25 @@ def create_playlist_in_plex(plex, songs, playlist_name):
print(f"Added {len(new_tracks)} new songs to existing playlist '{playlist_name}':") print(f"Added {len(new_tracks)} new songs to existing playlist '{playlist_name}':")
for track in new_tracks: for track in new_tracks:
print(f" + '{track.title}' by {track.artist().title}") print(f" + '{track.title}' by {track.artist().title}")
added = len(new_tracks)
else: else:
print(f"No new songs to add - all {len(tracks)} matching songs already in playlist.") print(f"No new songs to add - all {len(tracks)} matching songs already in playlist.")
added = 0
except: except:
# Playlist doesn't exist, create it # Playlist doesn't exist, create it
plex.createPlaylist(playlist_name, tracks) plex.createPlaylist(playlist_name, tracks)
print(f"Created playlist '{playlist_name}' with {len(tracks)} songs:") print(f"Created playlist '{playlist_name}' with {len(tracks)} songs:")
for track in tracks: for track in tracks:
print(f" + '{track.title}' by {track.artist().title}") print(f" + '{track.title}' by {track.artist().title}")
added = len(tracks)
except Exception as e: except Exception as e:
print(f"Error with playlist: {e}") print(f"Error with playlist: {e}")
added = 0
else: else:
print("No matching tracks found in Plex.") print("No matching tracks found in Plex.")
added = 0
return added, missing
def prompt_for_config(): def prompt_for_config():
"""Prompt user for configuration variables""" """Prompt user for configuration variables"""
@@ -313,9 +322,25 @@ def main():
# Create playlist # Create playlist
if songs: if songs:
create_playlist_in_plex(plex, songs, PLAYLIST_NAME) added, missing = create_playlist_in_plex(plex, songs, PLAYLIST_NAME)
else: else:
print("No songs found.") print("No songs found.")
added, missing = 0, []
# Create Amazon buy list for missing songs
if missing:
print(f"\n{len(missing)} songs not found in your library. Creating Amazon buy list...")
with open('amazon_buy_list.txt', 'w') as f:
f.write("Songs not in your library - Amazon search links:\n\n")
for song in missing:
artist = song['artist']
title = song['title']
# Create Amazon search URL
query = f"{artist} {title}".replace(' ', '+')
url = f"https://www.amazon.com/s?k={query}&i=digital-music"
f.write(f"{artist} - {title}\n{url}\n\n")
print(f" - {artist} - {title}: {url}")
print("Buy list saved to amazon_buy_list.txt")
# Log completion # Log completion
end_time = datetime.now() end_time = datetime.now()