diff --git a/main.py b/main.py index a865aba..c989480 100644 --- a/main.py +++ b/main.py @@ -12,28 +12,27 @@ logging.basicConfig( format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s', level=logging.DEBUG if DEBUG else logging.INFO) -def run_pls_command(playlist_id, playlist_name): - """Runs the docker exec command for a given playlist and transforms the output.""" +def transform_m3u_to_m3u8(m3u_content): + """Transforms M3U content to M3U8 format.""" + transformed_lines = [] + for line in m3u_content.strip().splitlines(): + if line.strip() and not line.startswith('#'): + if line.startswith('/music/'): + path = line[len('/music/'):] + encoded_path = urllib.parse.quote(path) + transformed_lines.append(f"local:track:{encoded_path}") + return '\n'.join(transformed_lines) + + +def run_pls_command(playlist_id): + """Runs the docker exec command and returns combined stdout/stderr.""" cmd = ['docker', 'exec', 'navidrome-navidrome-1', '/app/navidrome', 'pls', '-l', 'error', '-np', playlist_id] try: - result = subprocess.run(cmd, capture_output=True, text=True, check=False) - print(f"\n--- Output for playlist '{playlist_name}' ---") - if result.stdout.strip(): - transformed_lines = [] - for line in result.stdout.strip().splitlines(): - if line.strip() and not line.startswith('#'): - if line.startswith('/music/'): - path = line[len('/music/'):] - encoded_path = urllib.parse.quote(path) - transformed_lines.append(f"local:track:{encoded_path}") - if transformed_lines: - print('\n'.join(transformed_lines)) - if result.stderr.strip(): - print("STDERR:") - print(result.stderr.strip()) - print("--- End output ---\n") + result = subprocess.run(cmd, capture_output=True, text=True, check=False, stderr=subprocess.STDOUT) + return result.stdout except FileNotFoundError: logging.error("Command 'docker' not found. Please ensure it is installed and in your PATH.") + return "" def main(): @@ -115,7 +114,13 @@ def main(): playlist_data = current_playlists[playlist_id] playlist_name = playlist_data.get('name') logging.info(f"New playlist detected: '{playlist_name}' ({playlist_id}). Running command.") - run_pls_command(playlist_id, playlist_name) + raw_output = run_pls_command(playlist_id) + if raw_output: + transformed_output = transform_m3u_to_m3u8(raw_output) + if transformed_output: + print(f"\n--- Output for playlist '{playlist_name}' ---") + print(transformed_output) + print("--- End output ---\n") # Deleted playlists deleted_ids = set(known_playlists.keys()) - set(current_playlists.keys()) @@ -133,7 +138,13 @@ def main(): if previous_song_count != current_song_count: playlist_name = playlist_data.get('name') logging.info(f"Song count changed for '{playlist_name}' ({playlist_id}): {previous_song_count} -> {current_song_count}. Running command.") - run_pls_command(playlist_id, playlist_name) + raw_output = run_pls_command(playlist_id) + if raw_output: + transformed_output = transform_m3u_to_m3u8(raw_output) + if transformed_output: + print(f"\n--- Output for playlist '{playlist_name}' ---") + print(transformed_output) + print("--- End output ---\n") # Update the state for the next iteration. known_playlists = current_playlists