You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.4 KiB
Python

2 months ago
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
2 months ago
from helpers.db_moments import add_moment, list_moments
2 months ago
from config import get_local_db_connection
api = Blueprint("api", __name__)
import requests
SESSION = requests.Session()
SESSION.trust_env = False # ignore system proxies
2 months ago
@api.route('/open-folder', methods=['POST'])
def open_folder():
data = request.json
file_path = data.get("file_path")
file_path = os.path.abspath(file_path)
dir_path = os.path.dirname(file_path)
if not file_path or not os.path.exists(file_path):
return jsonify({"error": "File not found"}), 404
# Open folder in Explorer and select file
subprocess.run(f'explorer {dir_path}')
return jsonify({"success": True})
2 months ago
@api.route("/api/fav/toggle/<video_id>", methods=["POST"])
def api_fav_toggle(video_id):
# Optional: validate video exists
try:
_ = db_get_video(video_id)
except Exception:
return jsonify({"error": "video not found"}), 404
is_fav = db_toggle_fav(video_id)
return jsonify({"ok": True, "video_id": video_id, "is_favorite": is_fav})
2 months ago
@api.route("/api/fav/list")
def api_fav_list():
return jsonify({"favorites": sorted(list(db_get_fav_set()))})
2 months ago
@api.route('/delete-file', methods=['POST'])
def delete_file():
2 months ago
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:
os.remove(file_path)
conn, cur = get_local_db_connection()
cur.execute("UPDATE videos SET status = 'deleted' WHERE video_id = %s", (video_id,))
conn.commit()
cur.close(); conn.close()
return jsonify({"success": True})
except Exception as e:
return jsonify({"error": str(e)}), 500
2 months ago
@api.route("/api/moments/<video_id>", methods=["GET"])
def api_list_moments(video_id):
moments = list_moments(video_id)
return jsonify(ok=True, moments=moments)
@api.route("/api/moments/<video_id>", 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)
@api.route("/api/get_recording/", methods=["GET"])
def get_online():
url = "http://127.0.0.1:8000/get_streamers" # force IPv4
r = SESSION.get(url, timeout=1) # small timeout
streamers = r.json().get("streamers", [])
for s in streamers:
s["is_online"] = (s.get("status") == "Channel online")
return jsonify(streamers)
@api.route("/api/favorite_user", methods=["POST"])
def favorite_user():
data = request.get_json(force=True)
username = data.get("username")
fav = bool(data.get("favorite"))
conn, cur = get_local_db_connection()
if fav:
cur.execute("INSERT INTO favorite_users (username) VALUES (%s) ON CONFLICT DO NOTHING", (username,))
else:
cur.execute("DELETE FROM favorite_users WHERE username = %s", (username,))
conn.commit()
cur.close(); conn.close()
return {"ok": True}