diff --git a/core/plex_client.py b/core/plex_client.py index 3a3b2019..2aa56bcd 100644 --- a/core/plex_client.py +++ b/core/plex_client.py @@ -121,25 +121,85 @@ class PlexClient: logger.error(f"Failed to connect to Plex server: {e}") self.server = None + def get_available_music_libraries(self) -> List[Dict[str, str]]: + """Get list of all available music libraries on the Plex server""" + if not self.ensure_connection() or not self.server: + return [] + + try: + music_libraries = [] + for section in self.server.library.sections(): + if section.type == 'artist': + music_libraries.append({ + 'title': section.title, + 'key': str(section.key) + }) + + logger.debug(f"Found {len(music_libraries)} music libraries") + return music_libraries + except Exception as e: + logger.error(f"Error getting music libraries: {e}") + return [] + + def set_music_library_by_name(self, library_name: str) -> bool: + """Set the active music library by name""" + if not self.server: + return False + + try: + for section in self.server.library.sections(): + if section.type == 'artist' and section.title == library_name: + self.music_library = section + logger.info(f"Set music library to: {library_name}") + + # Store preference in database + from database.music_database import MusicDatabase + db = MusicDatabase() + db.set_preference('plex_music_library', library_name) + + return True + + logger.warning(f"Music library '{library_name}' not found") + return False + except Exception as e: + logger.error(f"Error setting music library: {e}") + return False + def _find_music_library(self): if not self.server: return - + try: music_sections = [] - + # Collect all music libraries for section in self.server.library.sections(): if section.type == 'artist': music_sections.append(section) - + if not music_sections: logger.warning("No music library found on Plex server") return - + + # Check if user has a saved preference + try: + from database.music_database import MusicDatabase + db = MusicDatabase() + preferred_library = db.get_preference('plex_music_library') + + if preferred_library: + # Try to find the preferred library + for section in music_sections: + if section.title == preferred_library: + self.music_library = section + logger.debug(f"Using user-selected music library: {section.title}") + return + except Exception as e: + logger.debug(f"Could not check library preference: {e}") + # Priority order for common library names priority_names = ['Music', 'music', 'Audio', 'audio', 'Songs', 'songs'] - + # First, try to find a library with a priority name for priority_name in priority_names: for section in music_sections: @@ -147,16 +207,16 @@ class PlexClient: self.music_library = section logger.debug(f"Found preferred music library: {section.title}") return - + # If no priority match found, use the first one self.music_library = music_sections[0] logger.debug(f"Found music library (first available): {self.music_library.title}") - + # Log other available libraries if multiple exist if len(music_sections) > 1: other_libraries = [s.title for s in music_sections[1:]] logger.info(f"Other music libraries available: {', '.join(other_libraries)}") - + except Exception as e: logger.error(f"Error finding music library: {e}") diff --git a/database/music_database.py b/database/music_database.py index 25c7caa2..acd78112 100644 --- a/database/music_database.py +++ b/database/music_database.py @@ -1875,6 +1875,14 @@ class MusicDatabase: def get_last_full_refresh(self) -> Optional[str]: """Get the date of the last full refresh""" return self.get_metadata('last_full_refresh') + + def set_preference(self, key: str, value: str): + """Set a user preference (alias for set_metadata for clarity)""" + self.set_metadata(key, value) + + def get_preference(self, key: str) -> Optional[str]: + """Get a user preference (alias for get_metadata for clarity)""" + return self.get_metadata(key) # Wishlist management methods diff --git a/web_server.py b/web_server.py index 0bd24f8d..54440239 100644 --- a/web_server.py +++ b/web_server.py @@ -1991,10 +1991,58 @@ def detect_media_server_endpoint(): add_activity_item("❌", "Auto-Detect Failed", f"No {server_type} server found", "Now") return jsonify({"success": False, "error": f"No {server_type} server found on common local addresses."}) +@app.route('/api/plex/music-libraries', methods=['GET']) +def get_plex_music_libraries(): + """Get list of all available music libraries from Plex""" + try: + libraries = plex_client.get_available_music_libraries() + + # Get currently selected library + from database.music_database import MusicDatabase + db = MusicDatabase() + selected_library = db.get_preference('plex_music_library') + + # Get the currently active library name + current_library = None + if plex_client.music_library: + current_library = plex_client.music_library.title + + return jsonify({ + "success": True, + "libraries": libraries, + "selected": selected_library, + "current": current_library + }) + except Exception as e: + logger.error(f"Error getting Plex music libraries: {e}") + return jsonify({"success": False, "error": str(e)}), 500 + +@app.route('/api/plex/select-music-library', methods=['POST']) +def select_plex_music_library(): + """Set the active Plex music library""" + try: + data = request.get_json() + library_name = data.get('library_name') + + if not library_name: + return jsonify({"success": False, "error": "No library name provided"}), 400 + + success = plex_client.set_music_library_by_name(library_name) + + if success: + add_activity_item("📚", "Library Selected", f"Plex music library set to: {library_name}", "Now") + return jsonify({"success": True, "message": f"Music library set to: {library_name}"}) + else: + return jsonify({"success": False, "error": f"Library '{library_name}' not found"}), 404 + + except Exception as e: + logger.error(f"Error setting Plex music library: {e}") + return jsonify({"success": False, "error": str(e)}), 500 + @app.route('/api/detect-soulseek', methods=['POST']) def detect_soulseek_endpoint(): print("Received auto-detect request for slskd") - + # Add activity for soulseek auto-detect start add_activity_item("🔍", "Auto-Detect Started", "Searching for slskd server", "Now") found_url = run_detection('slskd') diff --git a/webui/index.html b/webui/index.html index 7249539f..0eefa940 100644 --- a/webui/index.html +++ b/webui/index.html @@ -1653,6 +1653,15 @@ +