From 0372886d88cbecf9fa8dbc3c5b9af9938f134706 Mon Sep 17 00:00:00 2001 From: oscar <> Date: Sun, 7 Sep 2025 08:00:10 -0700 Subject: [PATCH] moments update --- helpers/db_moments.py | 24 ++++ routes/api.py | 27 ++++- templates/video_view.html | 234 +++++++++++++++++++++++++++++++++++++- 3 files changed, 274 insertions(+), 11 deletions(-) create mode 100644 helpers/db_moments.py diff --git a/helpers/db_moments.py b/helpers/db_moments.py new file mode 100644 index 0000000..f9feafa --- /dev/null +++ b/helpers/db_moments.py @@ -0,0 +1,24 @@ +# helpers/db_moments.py +from typing import List, Dict +from config import get_local_db_connection + +def add_moment(video_uuid: str, ts_seconds: int) -> str: + conn, cursor = get_local_db_connection() + cursor.execute( + "INSERT INTO moments (video_id, timestamp) VALUES (%s, %s)", + (video_uuid, ts_seconds), + ) + conn.commit() + new_id = cursor.lastrowid + cursor.close(); conn.close() + return new_id + +def list_moments(video_uuid: str): + conn, cursor = get_local_db_connection() + cursor.execute( + "SELECT id, timestamp FROM moments WHERE video_id = %s ORDER BY timestamp ASC", + (video_uuid,), + ) + rows = cursor.fetchall() + cursor.close(); conn.close() + return rows diff --git a/routes/api.py b/routes/api.py index 6a93d58..4181048 100644 --- a/routes/api.py +++ b/routes/api.py @@ -2,6 +2,7 @@ from flask import Blueprint, request, jsonify import os, subprocess from helpers.db import db_get_video from helpers.favorites import db_toggle_fav, db_get_fav_set +from helpers.db_moments import add_moment, list_moments from config import get_local_db_connection api = Blueprint("api", __name__) @@ -40,20 +41,17 @@ def api_fav_list(): @api.route('/delete-file', methods=['POST']) def delete_file(): - data = request.json - file_path = data.get("file_path") + data = request.json or {} + file_path = os.path.abspath(data.get("file_path","")) video_id = data.get("video_id") if not file_path or not os.path.exists(file_path): return jsonify({"error": "File not found"}), 404 try: - # delete from filesystem os.remove(file_path) - # optional: mark video as missing in DB conn, cur = get_local_db_connection() - cur = conn.cursor() cur.execute("UPDATE videos SET status = 'missing' WHERE video_id = %s", (video_id,)) conn.commit() cur.close(); conn.close() @@ -61,3 +59,22 @@ def delete_file(): return jsonify({"success": True}) except Exception as e: return jsonify({"error": str(e)}), 500 + +@api.route("/api/moments/", methods=["GET"]) +def api_list_moments(video_id): + moments = list_moments(video_id) + return jsonify(ok=True, moments=moments) + +@api.route("/api/moments/", methods=["POST"]) +def api_add_moment(video_id): + data = request.get_json(force=True, silent=True) or {} + try: + ts = int(data.get("timestamp", 0)) + except (TypeError, ValueError): + return jsonify(ok=False, error="Invalid timestamp"), 400 + + if ts < 0: + return jsonify(ok=False, error="Negative timestamp"), 400 + + new_id = add_moment(video_id, ts) + return jsonify(ok=True, id=new_id, timestamp=ts) diff --git a/templates/video_view.html b/templates/video_view.html index 8160b96..5636e31 100644 --- a/templates/video_view.html +++ b/templates/video_view.html @@ -60,6 +60,87 @@ } + + + + \ No newline at end of file