diff --git a/frigate/api/app.py b/frigate/api/app.py index e48196721..9d76ea417 100644 --- a/frigate/api/app.py +++ b/frigate/api/app.py @@ -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) diff --git a/frigate/api/fastapi_app.py b/frigate/api/fastapi_app.py index 902926488..9828aa426 100644 --- a/frigate/api/fastapi_app.py +++ b/frigate/api/fastapi_app.py @@ -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 diff --git a/frigate/api/preview.py b/frigate/api/preview.py index 46d3d0e82..8e500f5bd 100644 --- a/frigate/api/preview.py +++ b/frigate/api/preview.py @@ -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//start//end/") -@PreviewBp.route("/preview//start//end/") -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,25 +60,24 @@ def preview_ts(camera_name, start_ts, end_ts): ) if not clips: - return make_response( - jsonify( - { - "success": False, - "message": "No previews found.", - } - ), - 404, + return JSONResponse( + content={ + "success": False, + "message": "No previews found.", + }, + status_code=404, ) - return make_response(jsonify(clips), 200) + return JSONResponse(content=clips, status_code=200) -@PreviewBp.route("/preview/////") -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) - - datetime.now(pytz.timezone(tz_name.replace(",", "/"))).utcoffset() + datetime(int(parts[0]), int(parts[1]), int(day), int(hour), tzinfo=timezone.utc) + - datetime.now(pytz.timezone(tz_name.replace(",", "/"))).utcoffset() ) end_date = start_date + timedelta(hours=1) - timedelta(milliseconds=1) start_ts = start_date.timestamp() @@ -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//start//end//frames") -@PreviewBp.route( - "/preview//start//end//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, + )