v1.0: number and date new playlists
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
octotrigger.log
|
||||||
+25
-28
@@ -251,12 +251,19 @@ def trigger_navidrome_scan(session):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f" [X] Failed to trigger scan: {e}")
|
logger.error(f" [X] Failed to trigger scan: {e}")
|
||||||
|
|
||||||
def update_navidrome_playlist(session, playlist_name, song_ids):
|
def next_playlist_number(playlists):
|
||||||
"""
|
"""Return the next number after the highest numbered playlist."""
|
||||||
Creates or updates a playlist on the direct Navidrome instance.
|
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("=" * 40)
|
||||||
logger.info(f"[*] Configuring Playlist: '{playlist_name}'")
|
|
||||||
logger.info(f" Tracks to add: {len(song_ids)}")
|
logger.info(f" Tracks to add: {len(song_ids)}")
|
||||||
|
|
||||||
if not song_ids:
|
if not song_ids:
|
||||||
@@ -266,40 +273,33 @@ def update_navidrome_playlist(session, playlist_name, song_ids):
|
|||||||
params = get_auth_params()
|
params = get_auth_params()
|
||||||
|
|
||||||
try:
|
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"
|
url_get = f"{ND_DIRECT_URL}/rest/getPlaylists"
|
||||||
r = session.get(url_get, params=params, timeout=15)
|
r = session.get(url_get, params=params, timeout=15)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
|
||||||
data = r.json()
|
data = r.json()
|
||||||
playlists = data.get('subsonic-response', {}).get('playlists', {}).get('playlist', [])
|
playlists = data.get('subsonic-response', {}).get('playlists', {}).get('playlist', [])
|
||||||
|
playlist_number = next_playlist_number(playlists)
|
||||||
target_id = None
|
current_date = datetime.now().strftime("%m-%d-%y")
|
||||||
for pl in playlists:
|
playlist_name = f"{playlist_number}. {current_date} - {base_playlist_name}"
|
||||||
if pl.get('name') == playlist_name:
|
logger.info(f"[*] Creating Playlist: '{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.")
|
|
||||||
|
|
||||||
# 2. Create or Update Playlist
|
# Always create a fresh playlist: its sequential number is unique.
|
||||||
params = get_auth_params()
|
params = get_auth_params()
|
||||||
params['name'] = playlist_name
|
params['name'] = playlist_name
|
||||||
params['songId'] = song_ids # requests handles list -> songId=1&songId=2
|
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"
|
url_create = f"{ND_DIRECT_URL}/rest/createPlaylist"
|
||||||
r = session.get(url_create, params=params, timeout=15)
|
r = session.get(url_create, params=params, timeout=15)
|
||||||
r.raise_for_status()
|
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:
|
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
|
# MAIN PROCESS EXECUTION
|
||||||
@@ -315,13 +315,10 @@ def run_process(playlist_data):
|
|||||||
base_playlist_name = playlist_data.get("playlist_name", "Unknown Playlist")
|
base_playlist_name = playlist_data.get("playlist_name", "Unknown Playlist")
|
||||||
track_list = playlist_data.get("tracks", [])
|
track_list = playlist_data.get("tracks", [])
|
||||||
|
|
||||||
# 2. Append Date (DD-MM-YY)
|
# The sequential number and MM-DD-YY prefix are assigned when creating the playlist.
|
||||||
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
|
|
||||||
|
|
||||||
logger.info(f"--- {CLIENT_NAME} v{CLIENT_VERSION} ---")
|
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)}")
|
logger.info(f"Tracks Loaded: {len(track_list)}")
|
||||||
|
|
||||||
session = requests.Session()
|
session = requests.Session()
|
||||||
@@ -378,7 +375,7 @@ def run_process(playlist_data):
|
|||||||
|
|
||||||
time.sleep(0.5)
|
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 ---")
|
logger.info("--- End of Script ---")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user