POC: Converted preview related endpoints to FastAPI

This commit is contained in:
Rui Alves 2024-09-07 17:06:49 +01:00
parent dc5f85c7ca
commit 773d211017
3 changed files with 27 additions and 30 deletions

View File

@ -25,7 +25,6 @@ from frigate.api.event import EventBp
from frigate.api.export import ExportBp
from frigate.api.media import MediaBp
from frigate.api.notification import NotificationBp
from frigate.api.preview import PreviewBp
from frigate.api.review import ReviewBp
from frigate.config import FrigateConfig
from frigate.const import CONFIG_DIR
@ -51,7 +50,6 @@ bp = Blueprint("frigate", __name__)
bp.register_blueprint(EventBp)
bp.register_blueprint(ExportBp)
bp.register_blueprint(MediaBp)
bp.register_blueprint(PreviewBp)
bp.register_blueprint(ReviewBp)
bp.register_blueprint(AuthBp)
bp.register_blueprint(NotificationBp)

View File

@ -4,6 +4,7 @@ from fastapi import FastAPI
from frigate.api import app as main_app
from frigate.api.defs.tags import Tags
from frigate.api import preview
from frigate.plus import PlusApi
from frigate.ptz.onvif import OnvifController
from frigate.stats.emitter import StatsEmitter
@ -41,6 +42,7 @@ def create_fastapi_app(
app = FastAPI(debug=False, tags_metadata=tags_metadata)
# Routes
app.include_router(main_app.router)
app.include_router(preview.router)
# App Properties
app.frigate_config = frigate_config
app.detected_frames_processor = detected_frames_processor

View File

@ -5,23 +5,21 @@ import os
from datetime import datetime, timedelta, timezone
import pytz
from flask import (
Blueprint,
jsonify,
make_response,
)
from fastapi import APIRouter
from fastapi.responses import JSONResponse
from frigate.api.defs.tags import Tags
from frigate.const import CACHE_DIR, PREVIEW_FRAME_TYPE
from frigate.models import Previews
logger = logging.getLogger(__name__)
PreviewBp = Blueprint("previews", __name__)
router = APIRouter(tags=[Tags.preview])
@PreviewBp.route("/preview/<camera_name>/start/<int:start_ts>/end/<int:end_ts>")
@PreviewBp.route("/preview/<camera_name>/start/<float:start_ts>/end/<float:end_ts>")
def preview_ts(camera_name, start_ts, end_ts):
@router.get("/preview/{camera_name}/start/{start_ts}/end/{end_ts}")
def preview_ts(camera_name: str, start_ts: float, end_ts: float):
"""Get all mp4 previews relevant for time period."""
if camera_name != "all":
camera_clause = Previews.camera == camera_name
@ -62,21 +60,20 @@ def preview_ts(camera_name, start_ts, end_ts):
)
if not clips:
return make_response(
jsonify(
{
return JSONResponse(
content={
"success": False,
"message": "No previews found.",
}
),
404,
},
status_code=404,
)
return make_response(jsonify(clips), 200)
return JSONResponse(content=clips, status_code=200)
@PreviewBp.route("/preview/<year_month>/<day>/<hour>/<camera_name>/<tz_name>")
def preview_hour(year_month, day, hour, camera_name, tz_name):
@router.get("/preview/{year_month}/{day}/{hour}/{camera_name}/{tz_name}")
def preview_hour(year_month: str, day: int, hour: int, camera_name: str, tz_name: str):
"""Get all mp4 previews relevant for time period given the timezone"""
parts = year_month.split("-")
start_date = (
datetime(int(parts[0]), int(parts[1]), int(day), int(hour), tzinfo=timezone.utc)
@ -89,11 +86,8 @@ def preview_hour(year_month, day, hour, camera_name, tz_name):
return preview_ts(camera_name, start_ts, end_ts)
@PreviewBp.route("/preview/<camera_name>/start/<int:start_ts>/end/<int:end_ts>/frames")
@PreviewBp.route(
"/preview/<camera_name>/start/<float:start_ts>/end/<float:end_ts>/frames"
)
def get_preview_frames_from_cache(camera_name: str, start_ts, end_ts):
@router.get("/preview/{camera_name}/start/{start_ts}/end/{end_ts}/frames")
def get_preview_frames_from_cache(camera_name: str, start_ts: float, end_ts: float):
"""Get list of cached preview frames"""
preview_dir = os.path.join(CACHE_DIR, "preview_frames")
file_start = f"preview_{camera_name}"
@ -113,4 +107,7 @@ def get_preview_frames_from_cache(camera_name: str, start_ts, end_ts):
selected_previews.append(file)
return jsonify(selected_previews)
return JSONResponse(
content=selected_previews,
status_code=200,
)