v1.0: number and date new playlists
This commit is contained in:
+25
-28
@@ -251,12 +251,19 @@ def trigger_navidrome_scan(session):
|
||||
except Exception as e:
|
||||
logger.error(f" [X] Failed to trigger scan: {e}")
|
||||
|
||||
def update_navidrome_playlist(session, playlist_name, song_ids):
|
||||
"""
|
||||
Creates or updates a playlist on the direct Navidrome instance.
|
||||
"""
|
||||
def next_playlist_number(playlists):
|
||||
"""Return the next number after the highest numbered playlist."""
|
||||
highest_number = 0
|
||||
for playlist in playlists:
|
||||
match = re.match(r'^\s*(\d+)\.\s+', playlist.get('name', ''))
|
||||
if match:
|
||||
highest_number = max(highest_number, int(match.group(1)))
|
||||
return highest_number + 1
|
||||
|
||||
|
||||
def update_navidrome_playlist(session, base_playlist_name, song_ids):
|
||||
"""Create a newly numbered, date-prefixed playlist on Navidrome."""
|
||||
logger.info("=" * 40)
|
||||
logger.info(f"[*] Configuring Playlist: '{playlist_name}'")
|
||||
logger.info(f" Tracks to add: {len(song_ids)}")
|
||||
|
||||
if not song_ids:
|
||||
@@ -266,40 +273,33 @@ def update_navidrome_playlist(session, playlist_name, song_ids):
|
||||
params = get_auth_params()
|
||||
|
||||
try:
|
||||
# 1. Check for existing playlist to avoid duplicates
|
||||
# Read existing playlists so each newly created playlist gets the next number.
|
||||
url_get = f"{ND_DIRECT_URL}/rest/getPlaylists"
|
||||
r = session.get(url_get, params=params, timeout=15)
|
||||
r.raise_for_status()
|
||||
|
||||
data = r.json()
|
||||
playlists = data.get('subsonic-response', {}).get('playlists', {}).get('playlist', [])
|
||||
|
||||
target_id = None
|
||||
for pl in playlists:
|
||||
if pl.get('name') == playlist_name:
|
||||
target_id = pl.get('id')
|
||||
logger.info(f" [i] Found existing playlist ID: {target_id} (Updating)")
|
||||
break
|
||||
|
||||
if not target_id:
|
||||
logger.info(f" [i] Playlist does not exist. Creating new.")
|
||||
playlist_number = next_playlist_number(playlists)
|
||||
current_date = datetime.now().strftime("%m-%d-%y")
|
||||
playlist_name = f"{playlist_number}. {current_date} - {base_playlist_name}"
|
||||
logger.info(f"[*] Creating Playlist: '{playlist_name}'")
|
||||
|
||||
# 2. Create or Update Playlist
|
||||
# Always create a fresh playlist: its sequential number is unique.
|
||||
params = get_auth_params()
|
||||
params['name'] = playlist_name
|
||||
params['songId'] = song_ids # requests handles list -> songId=1&songId=2
|
||||
|
||||
if target_id:
|
||||
params['playlistId'] = target_id
|
||||
|
||||
url_create = f"{ND_DIRECT_URL}/rest/createPlaylist"
|
||||
r = session.get(url_create, params=params, timeout=15)
|
||||
r.raise_for_status()
|
||||
|
||||
logger.info(f" [OK] Playlist '{playlist_name}' successfully saved.")
|
||||
logger.info(f" [OK] Playlist '{playlist_name}' successfully created.")
|
||||
return playlist_name
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f" [X] Failed to update playlist: {e}")
|
||||
logger.error(f" [X] Failed to create playlist: {e}")
|
||||
return None
|
||||
|
||||
# ==========================================
|
||||
# MAIN PROCESS EXECUTION
|
||||
@@ -315,13 +315,10 @@ def run_process(playlist_data):
|
||||
base_playlist_name = playlist_data.get("playlist_name", "Unknown Playlist")
|
||||
track_list = playlist_data.get("tracks", [])
|
||||
|
||||
# 2. Append Date (DD-MM-YY)
|
||||
current_date = datetime.now().strftime("%d-%m-%y")
|
||||
#final_playlist_name = f"{base_playlist_name} ({current_date})"
|
||||
final_playlist_name = f"{current_date} {base_playlist_name}" #deze zelf gemaakt ipv bovenste
|
||||
# The sequential number and MM-DD-YY prefix are assigned when creating the playlist.
|
||||
|
||||
logger.info(f"--- {CLIENT_NAME} v{CLIENT_VERSION} ---")
|
||||
logger.info(f"Playlist Target: {final_playlist_name}")
|
||||
logger.info(f"Playlist Target: {base_playlist_name}")
|
||||
logger.info(f"Tracks Loaded: {len(track_list)}")
|
||||
|
||||
session = requests.Session()
|
||||
@@ -378,7 +375,7 @@ def run_process(playlist_data):
|
||||
|
||||
time.sleep(0.5)
|
||||
|
||||
update_navidrome_playlist(session, final_playlist_name, final_song_ids)
|
||||
update_navidrome_playlist(session, base_playlist_name, final_song_ids)
|
||||
|
||||
logger.info("--- End of Script ---")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user