Simplify highlight API for automation
This commit is contained in:
@@ -12,6 +12,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
|
||||
|
||||
|
||||
@@ -42,6 +43,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"
|
||||
|
||||
@@ -74,17 +94,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")
|
||||
|
||||
Reference in New Issue
Block a user