feat: Save transformed playlists as M3U8 files in MOPIDY_PLAYLIST_DIR
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
66
main.py
66
main.py
@@ -35,8 +35,40 @@ def run_pls_command(playlist_id):
|
||||
return ""
|
||||
|
||||
|
||||
def save_playlist_file(playlist_dir, playlist_name, content):
|
||||
"""Saves the transformed playlist content to a file."""
|
||||
filename = f"{playlist_name}.m3u8"
|
||||
filepath = os.path.join(playlist_dir, filename)
|
||||
try:
|
||||
with open(filepath, 'w', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
logging.info(f"Saved playlist to {filepath}")
|
||||
except IOError as e:
|
||||
logging.error(f"Error writing to file {filepath}: {e}")
|
||||
|
||||
|
||||
def delete_playlist_file(playlist_dir, playlist_name):
|
||||
"""Deletes a playlist file."""
|
||||
filename = f"{playlist_name}.m3u8"
|
||||
filepath = os.path.join(playlist_dir, filename)
|
||||
if os.path.exists(filepath):
|
||||
try:
|
||||
os.remove(filepath)
|
||||
logging.info(f"Deleted playlist file {filepath}")
|
||||
except OSError as e:
|
||||
logging.error(f"Error deleting file {filepath}: {e}")
|
||||
|
||||
|
||||
def main():
|
||||
"""Get playlists from a Navidrome server using the Subsonic API."""
|
||||
mopidy_playlist_dir = os.environ.get('MOPIDY_PLAYLIST_DIR')
|
||||
if not mopidy_playlist_dir:
|
||||
logging.error("MOPIDY_PLAYLIST_DIR environment variable must be set.")
|
||||
return
|
||||
if not os.path.isdir(mopidy_playlist_dir):
|
||||
logging.error(f"Mopidy playlist directory not found: {mopidy_playlist_dir}")
|
||||
return
|
||||
|
||||
navidrome_url = os.environ.get('NAVIDROME_URL')
|
||||
username = os.environ.get('NAVIDROME_USER')
|
||||
|
||||
@@ -113,38 +145,48 @@ def main():
|
||||
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.")
|
||||
logging.info(f"New playlist detected: '{playlist_name}' ({playlist_id}). Generating file.")
|
||||
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")
|
||||
save_playlist_file(mopidy_playlist_dir, playlist_name, transformed_output)
|
||||
|
||||
# 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}).")
|
||||
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)
|
||||
|
||||
# Check for song count changes in existing playlists
|
||||
# Check for song count or name changes in existing playlists
|
||||
for playlist_id, playlist_data in current_playlists.items():
|
||||
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')
|
||||
current_name = playlist_data.get('name')
|
||||
previous_name = previous_data.get('name')
|
||||
|
||||
song_count_changed = previous_song_count != current_song_count
|
||||
name_changed = previous_name != current_name
|
||||
|
||||
if song_count_changed or name_changed:
|
||||
log_msg = f"Playlist '{previous_name}' ({playlist_id}) changed."
|
||||
if name_changed:
|
||||
log_msg += f" Name: '{previous_name}' -> '{current_name}'."
|
||||
delete_playlist_file(mopidy_playlist_dir, previous_name)
|
||||
if song_count_changed:
|
||||
log_msg += f" Song count: {previous_song_count} -> {current_song_count}."
|
||||
|
||||
logging.info(log_msg + " Regenerating file.")
|
||||
|
||||
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.")
|
||||
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")
|
||||
save_playlist_file(mopidy_playlist_dir, current_name, transformed_output)
|
||||
|
||||
# Update the state for the next iteration.
|
||||
known_playlists = current_playlists
|
||||
|
||||
Reference in New Issue
Block a user