feat: Detect new and deleted playlists; refactor command execution
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
71
main.py
71
main.py
@@ -11,6 +11,22 @@ 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."""
|
||||
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():
|
||||
print(result.stdout.strip())
|
||||
if result.stderr.strip():
|
||||
print("STDERR:")
|
||||
print(result.stderr.strip())
|
||||
print("--- End output ---\n")
|
||||
except FileNotFoundError:
|
||||
logging.error("Command 'docker' not found. Please ensure it is installed and in your PATH.")
|
||||
|
||||
|
||||
def main():
|
||||
"""Get playlists from a Navidrome server using the Subsonic API."""
|
||||
navidrome_url = os.environ.get('NAVIDROME_URL')
|
||||
@@ -42,7 +58,7 @@ def main():
|
||||
'f': 'json'
|
||||
}
|
||||
|
||||
playlist_song_counts = {}
|
||||
known_playlists = {}
|
||||
|
||||
while True:
|
||||
api_url = f"{navidrome_url.rstrip('/')}/rest/getPlaylists.view"
|
||||
@@ -70,37 +86,48 @@ def main():
|
||||
continue
|
||||
|
||||
playlists = subsonic_response.get('playlists', {}).get('playlist', [])
|
||||
|
||||
current_playlists = {p.get('id'): p for p in playlists}
|
||||
|
||||
# On the first successful poll, print the list of playlists and populate the counts
|
||||
if not playlist_song_counts and playlists:
|
||||
# On first successful poll, just populate state and print list
|
||||
if not known_playlists and playlists:
|
||||
logging.info("Initial scan. Monitoring playlists for changes.")
|
||||
print(f"Playlists for {username}:")
|
||||
for playlist_id, playlist_data in current_playlists.items():
|
||||
print(f" - [{playlist_id}] {playlist_data.get('name')} ({playlist_data.get('songCount')} songs)")
|
||||
known_playlists = current_playlists
|
||||
time.sleep(5)
|
||||
continue
|
||||
|
||||
# Check for song count changes in playlists we are already tracking
|
||||
# --- Detect changes after initial scan ---
|
||||
|
||||
# Newly created playlists
|
||||
newly_added_ids = set(current_playlists.keys()) - set(known_playlists.keys())
|
||||
for playlist_id in newly_added_ids:
|
||||
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)
|
||||
|
||||
# Deleted playlists
|
||||
deleted_ids = set(known_playlists.keys()) - set(current_playlists.keys())
|
||||
for playlist_id in deleted_ids:
|
||||
playlist_data = known_playlists[playlist_id]
|
||||
logging.info(f"Playlist deleted: '{playlist_data.get('name')}' ({playlist_id}).")
|
||||
|
||||
# Check for song count changes in existing playlists
|
||||
for playlist_id, playlist_data in current_playlists.items():
|
||||
current_song_count = playlist_data.get('songCount')
|
||||
if playlist_id in playlist_song_counts and playlist_song_counts[playlist_id] != current_song_count:
|
||||
logging.info(f"Song count changed for '{playlist_data.get('name')}' ({playlist_id}): {playlist_song_counts[playlist_id]} -> {current_song_count}. Running command.")
|
||||
if playlist_id in known_playlists:
|
||||
previous_data = known_playlists[playlist_id]
|
||||
current_song_count = playlist_data.get('songCount')
|
||||
previous_song_count = previous_data.get('songCount')
|
||||
|
||||
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_data.get('name')}' ---")
|
||||
if result.stdout.strip():
|
||||
print(result.stdout.strip())
|
||||
if result.stderr.strip():
|
||||
print("STDERR:")
|
||||
print(result.stderr.strip())
|
||||
print("--- End output ---\n")
|
||||
except FileNotFoundError:
|
||||
logging.error("Command 'docker' not found. Please ensure it is installed and in your PATH.")
|
||||
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)
|
||||
|
||||
# Update the state for the next iteration. This handles new, deleted, and updated playlists.
|
||||
playlist_song_counts = {pid: pdata.get('songCount') for pid, pdata in current_playlists.items()}
|
||||
# Update the state for the next iteration.
|
||||
known_playlists = current_playlists
|
||||
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user