# plex_playlist_sync.py from config import ( LASTFM_API_KEY, LASTFM_API_SECRET, LASTFM_USERNAME, PLEX_BASEURL, PLEX_TOKEN, MIN_PLAYS, RECENT_MONTHS, MAX_SIMILAR_PER_ART, SIMILAR_MATCH_MIN, CACHE_TTL_HOURS, DEBUG_PRINT ) # Importera relevanta funktioner från lastfm_helpers from lastfm_helpers import recent_artists, get_artist_top_tracks from musicbrainz_helpers import load_cache, save_cache # Används för cache import pylast # Behövs för pylast.LastFMNetwork initiering i denna fil from plexapi.server import PlexServer import datetime import time import logging logging.basicConfig(level=logging.INFO) log = logging.getLogger("PlexPlaylistSync") # Initialisera Last.fm-nätverket i denna fil också, om det behövs här. # OBS: lastfm_helpers initialiserar det globalt, så du kan potentiellt ta bort detta block # om du är säker på att lastfm_helpers alltid laddas först och initierar nätverket korrekt. # Men för att vara säker, kan du behålla det eller flytta initieringen till en central punkt. LASTFM_NETWORK = None try: LASTFM_NETWORK = pylast.LastFMNetwork( api_key=LASTFM_API_KEY, api_secret=LASTFM_API_SECRET, username=LASTFM_USERNAME ) except Exception as e: log.error(f"Failed to initialize Last.fm network. Check keys/username: {e}") LASTFM_NETWORK = None # get_artist_top_tracks funktionen behöver INTE definieras här # om den importeras från lastfm_helpers.py # Jag tar bort den härifrån, så den används korrekt importerad. def create_lastfm_recommended_playlist(): start_time = time.time() log.info("🚀 Starting process to create Last.fm-based playlists in Plex.") try: plex = PlexServer(PLEX_BASEURL, PLEX_TOKEN) log.info(f"✅ Connected to Plex Media Server: {plex.baseurl}") except Exception as e: log.error(f"❌ Could not connect to Plex Server: {e}") log.error("Check PLEX_BASEURL and PLEX_TOKEN in config_local.py.") return music_library = None try: music_library = plex.library.section('Music') log.info(f"Using Plex library: '{music_library.title}'") except Exception as e: log.error(f"❌ Could not find 'Music' library in Plex: {e}") log.error("Check your music library name in Plex.") return artists_for_playlist = [] cache = load_cache() added_artists_mbids = cache.get("added_artists", []) log.info("Gathering artists for the playlist...") # Get names for artists added by Lidarr sync for mbid in added_artists_mbids: try: # Re-use the LASTFM_NETWORK initialized above or in lastfm_helpers artist_info = LASTFM_NETWORK.get_artist_by_mbid(mbid) # Needs LASTFM_NETWORK from this file or lastfm_helpers artists_for_playlist.append(artist_info.name) except Exception: log.debug(f"Could not find name for MBID: {mbid} via pylast.") pass # Add some of the most recently played artists as well recent_played_artists = recent_artists() # Uses recent_artists from lastfm_helpers for name, mbid in recent_played_artists[:10]: if name not in artists_for_playlist: artists_for_playlist.append(name) if not artists_for_playlist: log.info("No artists found to create a playlist. Ensure Last.fm sync has run.") return log.info(f"Found {len(artists_for_playlist)} unique artists to build the playlist.") all_recommended_tracks_info = [] for artist_name in artists_for_playlist: # Use the imported get_artist_top_tracks top_tracks = get_artist_top_tracks(artist_name, limit=3) all_recommended_tracks_info.extend(top_tracks) unique_tracks_to_add = [] seen_track_identifiers = set() log.info(f"Searching for {len(all_recommended_tracks_info)} potential tracks in Plex library...") for track_info in all_recommended_tracks_info: artist_name = track_info["artist"] track_title = track_info["title"] identifier = f"{artist_name}-{track_title}" if identifier in seen_track_identifiers: continue found_plex_track = None try: results = music_library.search(f"{artist_name} {track_title}", libtype='track') for item in results: if item.artist() and item.artist().title.lower() == artist_name.lower() and item.title.lower() == track_title.lower(): found_plex_track = item break except Exception as e: log.warning(f"Error searching Plex for '{track_title}' by '{artist_name}': {e}") if found_plex_track: unique_tracks_to_add.append(found_plex_track) seen_track_identifiers.add(identifier) log.info(f" ✅ Found: {found_plex_track.artist().title} - {found_plex_track.title}") else: log.info(f" ❌ Not found: '{track_title}' by '{artist_name}' in Plex.") if not unique_tracks_to_add: log.info("No new matching songs found in Plex to create a playlist.") return current_date = datetime.datetime.now().strftime("%Y-%m") playlist_name = f"Last.fm Recommendations {current_date}" playlist = None try: playlist = plex.playlist(playlist_name) log.info(f"Playlist '{playlist_name}' already exists.") except Exception: log.info(f"Creating new playlist: '{playlist_name}'") if playlist: existing_playlist_items = playlist.items() new_items_to_add = [track for track in unique_tracks_to_add if track not in existing_playlist_items] if new_items_to_add: try: playlist.addItems(new_items_to_add) log.info(f"Added {len(new_items_to_add)} new songs to existing playlist '{playlist_name}'.") except Exception as e: log.error(f"Could not add songs to existing playlist: {e}") else: log.info(f"No new songs to add to existing playlist '{playlist_name}'.") else: try: new_playlist = plex.createPlaylist(playlist_name, items=unique_tracks_to_add) log.info(f"✅ Created new playlist '{new_playlist.title}' with {len(unique_tracks_to_add)} songs.") except Exception as e: log.error(f"❌ Could not create playlist in Plex: {e}") log.info(f"🎉 Done! Process took {((time.time()-start_time)/60):.1f} minutes.") if __name__ == "__main__": create_lastfm_recommended_playlist()