feat(api): auto-link rehearsal sessions on watcher upload and nc-scan
parse_rehearsal_date() extracts YYMMDD / YYYYMMDD from the file path and get_or_create() a RehearsalSession. Both the watcher nc-upload endpoint and the nc-scan endpoint now set song.session_id when a dated folder is detected. Existing songs without a session_id are back-filled on the next import of the same folder. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
47
api/src/rehearsalhub/services/session.py
Normal file
47
api/src/rehearsalhub/services/session.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""Helpers for parsing rehearsal session dates from Nextcloud paths."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from datetime import date
|
||||
|
||||
# Matches a YYMMDD or YYYYMMDD directory segment anywhere in the path.
|
||||
# e.g. "bands/my-band/231015/take.wav" → "231015" → date(2023, 10, 15)
|
||||
_YYMMDD_RE = re.compile(r"(?:^|/)(\d{6})(?:/|$)")
|
||||
_YYYYMMDD_RE = re.compile(r"(?:^|/)(\d{8})(?:/|$)")
|
||||
|
||||
|
||||
def parse_rehearsal_date(nc_file_path: str) -> date | None:
|
||||
"""
|
||||
Extract a rehearsal date from a Nextcloud file path.
|
||||
|
||||
Supports YYMMDD (e.g. 231015 → 2023-10-15) and
|
||||
YYYYMMDD (e.g. 20231015 → 2023-10-15) folder names.
|
||||
Returns None if no date segment is found or the date is invalid.
|
||||
"""
|
||||
# Try YYYYMMDD first (more specific)
|
||||
m = _YYYYMMDD_RE.search(nc_file_path)
|
||||
if m:
|
||||
s = m.group(1)
|
||||
try:
|
||||
return date(int(s[:4]), int(s[4:6]), int(s[6:8]))
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
m = _YYMMDD_RE.search(nc_file_path)
|
||||
if m:
|
||||
s = m.group(1)
|
||||
yy, mm, dd = int(s[0:2]), int(s[2:4]), int(s[4:6])
|
||||
# Assume 2000s
|
||||
try:
|
||||
return date(2000 + yy, mm, dd)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def nc_folder_for_path(nc_file_path: str) -> str:
|
||||
"""Return the parent directory of a file path, with trailing slash."""
|
||||
from pathlib import Path
|
||||
return str(Path(nc_file_path).parent).rstrip("/") + "/"
|
||||
Reference in New Issue
Block a user