feat: Add Mopidy RPC call to refresh playlists on changes
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
33
main.py
33
main.py
@@ -62,6 +62,27 @@ def delete_playlist_file(playlist_dir, playlist_name):
|
||||
logging.error(f"Error deleting file {filepath}: {e}")
|
||||
|
||||
|
||||
def call_mopidy_rpc(method, params=None):
|
||||
"""Calls a Mopidy RPC method."""
|
||||
if not settings.MOPIDY_RPC_URL:
|
||||
return
|
||||
|
||||
data = {
|
||||
'jsonrpc': '2.0',
|
||||
'id': 1,
|
||||
'method': method,
|
||||
}
|
||||
if params:
|
||||
data['params'] = params
|
||||
|
||||
try:
|
||||
response = requests.post(settings.MOPIDY_RPC_URL, json=data)
|
||||
response.raise_for_status()
|
||||
logging.info(f"Successfully called Mopidy RPC method: {method}")
|
||||
except requests.exceptions.RequestException as e:
|
||||
logging.error(f"Error calling Mopidy RPC: {e}")
|
||||
|
||||
|
||||
def main():
|
||||
"""Get playlists from a Navidrome server using the Subsonic API."""
|
||||
parser = argparse.ArgumentParser(description="Sync Navidrome playlists to Mopidy.")
|
||||
@@ -127,6 +148,7 @@ def main():
|
||||
return
|
||||
|
||||
logging.info(f"Found {len(playlists)} playlists to sync.")
|
||||
synced_a_playlist = False
|
||||
for playlist in playlists:
|
||||
playlist_id = playlist.get('id')
|
||||
playlist_name = playlist.get('name')
|
||||
@@ -136,13 +158,19 @@ def main():
|
||||
transformed_output = transform_m3u_to_m3u8(raw_output)
|
||||
if transformed_output:
|
||||
save_playlist_file(mopidy_playlist_dir, playlist_name, transformed_output)
|
||||
playlists_changed = True
|
||||
synced_a_playlist = True
|
||||
|
||||
if synced_a_playlist:
|
||||
call_mopidy_rpc('core.playlists.refresh')
|
||||
|
||||
logging.info("Force sync complete.")
|
||||
return
|
||||
|
||||
known_playlists = {}
|
||||
|
||||
while True:
|
||||
playlists_changed = False
|
||||
api_url = f"{navidrome_url.rstrip('/')}/rest/getPlaylists.view"
|
||||
|
||||
try:
|
||||
@@ -201,6 +229,7 @@ def main():
|
||||
playlist_name = playlist_data.get('name')
|
||||
logging.info(f"Playlist deleted: '{playlist_name}' ({playlist_id}). Removing file.")
|
||||
delete_playlist_file(mopidy_playlist_dir, playlist_name)
|
||||
playlists_changed = True
|
||||
|
||||
# Check for song count or name changes in existing playlists
|
||||
for playlist_id, playlist_data in current_playlists.items():
|
||||
@@ -215,6 +244,7 @@ def main():
|
||||
name_changed = previous_name != current_name
|
||||
|
||||
if song_count_changed or name_changed:
|
||||
playlists_changed = True
|
||||
log_msg = f"Playlist '{previous_name}' ({playlist_id}) changed."
|
||||
if name_changed:
|
||||
log_msg += f" Name: '{previous_name}' -> '{current_name}'."
|
||||
@@ -233,6 +263,9 @@ def main():
|
||||
# Update the state for the next iteration.
|
||||
known_playlists = current_playlists
|
||||
|
||||
if playlists_changed:
|
||||
call_mopidy_rpc('core.playlists.refresh')
|
||||
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user