Simplify highlight API for automation

This commit is contained in:
human insan
2026-08-27 23:09:15 +01:00
parent df4153a9dd
commit 890e40f3cf
2 changed files with 37 additions and 11 deletions
+29 -5
View File
@@ -13,6 +13,7 @@ from urllib.parse import parse_qs, urlparse
DATE_RE = re.compile(r"^\d{8}$")
VIDEO_RE = re.compile(r"^(\d{2})\.(\d{2})\.(\d{2})\.(avi|mp4)$", re.IGNORECASE)
TIME_RE = re.compile(r"^(\d{1,2}):(\d{2})(?::(\d{2}))?$")
APP_DIR = Path(__file__).resolve().parent
@@ -43,6 +44,25 @@ def scan_recordings(root):
return recordings
def parse_clock(value):
if isinstance(value, (int, float)):
return float(value)
match = TIME_RE.fullmatch(str(value).strip())
if not match:
raise ValueError
hour, minute, second = int(match.group(1)), int(match.group(2)), int(match.group(3) or 0)
if hour > 23 or minute > 59 or second > 59:
raise ValueError
return hour * 3600 + minute * 60 + second
def normalize_date(value):
date = str(value).replace("-", "")
if not DATE_RE.fullmatch(date):
raise ValueError
return date
class PlayerHandler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
@@ -75,17 +95,21 @@ class PlayerHandler(BaseHTTPRequestHandler):
try:
length = int(self.headers.get("Content-Length", "0"))
item = json.loads(self.rfile.read(length))
date = str(item["date"])
start = float(item["start"])
end = float(item["end"])
if not DATE_RE.fullmatch(date) or not (0 <= start < end <= 86400):
date = normalize_date(item["date"])
start = parse_clock(item.get("start", item.get("start_time")))
end_value = item.get("end", item.get("end_time"))
if end_value is None and item.get("duration") is not None:
end = start + parse_clock(item["duration"])
else:
end = parse_clock(end_value)
if not (0 <= start < end <= 86400):
raise ValueError
highlight = {
"id": max((entry["id"] for entry in self.server.highlights), default=0) + 1,
"date": date,
"start": start,
"end": end,
"label": str(item.get("label", "Untitled highlight"))[:120],
"label": str(item.get("label", item.get("title", "Untitled highlight")))[:120],
}
except (KeyError, TypeError, ValueError, json.JSONDecodeError):
return self.send_error(400, "Invalid highlight")