feat: Continuously poll playlists and execute command on song count change
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
79
main.py
79
main.py
@@ -2,6 +2,8 @@ import os, logging
|
|||||||
import hashlib
|
import hashlib
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
import subprocess
|
||||||
|
import time
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
DEBUG = os.environ.get('DEBUG')
|
DEBUG = os.environ.get('DEBUG')
|
||||||
@@ -40,36 +42,67 @@ def main():
|
|||||||
'f': 'json'
|
'f': 'json'
|
||||||
}
|
}
|
||||||
|
|
||||||
api_url = f"{navidrome_url.rstrip('/')}/rest/getPlaylists.view"
|
playlist_song_counts = {}
|
||||||
|
|
||||||
try:
|
while True:
|
||||||
response = requests.get(api_url, params=params)
|
api_url = f"{navidrome_url.rstrip('/')}/rest/getPlaylists.view"
|
||||||
response.raise_for_status() # Raise an exception for bad status codes
|
|
||||||
except requests.exceptions.RequestException as e:
|
|
||||||
logging.error(f"Error connecting to Navidrome: {e}")
|
|
||||||
return
|
|
||||||
|
|
||||||
data = response.json()
|
try:
|
||||||
|
response = requests.get(api_url, params=params)
|
||||||
|
response.raise_for_status() # Raise an exception for bad status codes
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
logging.error(f"Error connecting to Navidrome: {e}")
|
||||||
|
time.sleep(5)
|
||||||
|
continue
|
||||||
|
|
||||||
subsonic_response = data.get('subsonic-response')
|
data = response.json()
|
||||||
if not subsonic_response:
|
|
||||||
logging.error("Invalid response format from server.")
|
|
||||||
return
|
|
||||||
|
|
||||||
if subsonic_response.get('status') == 'failed':
|
subsonic_response = data.get('subsonic-response')
|
||||||
error = subsonic_response.get('error', {})
|
if not subsonic_response:
|
||||||
logging.error(f"API Error: {error.get('message')} (code: {error.get('code')})")
|
logging.error("Invalid response format from server.")
|
||||||
return
|
time.sleep(5)
|
||||||
|
continue
|
||||||
|
|
||||||
playlists = subsonic_response.get('playlists', {}).get('playlist', [])
|
if subsonic_response.get('status') == 'failed':
|
||||||
|
error = subsonic_response.get('error', {})
|
||||||
|
logging.error(f"API Error: {error.get('message')} (code: {error.get('code')})")
|
||||||
|
time.sleep(5)
|
||||||
|
continue
|
||||||
|
|
||||||
if not playlists:
|
playlists = subsonic_response.get('playlists', {}).get('playlist', [])
|
||||||
logging.info("No playlists found for user '%s'.", username)
|
|
||||||
return
|
current_playlists = {p.get('id'): p for p in playlists}
|
||||||
|
|
||||||
print(f"Playlists for {username}:")
|
# On the first successful poll, print the list of playlists and populate the counts
|
||||||
for playlist in playlists:
|
if not playlist_song_counts and playlists:
|
||||||
print(f" - [{playlist.get('id')}] {playlist.get('name')} ({playlist.get('songCount')} songs)")
|
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)")
|
||||||
|
|
||||||
|
# Check for song count changes in playlists we are already tracking
|
||||||
|
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.")
|
||||||
|
|
||||||
|
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.")
|
||||||
|
|
||||||
|
# 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()}
|
||||||
|
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user