Skip to content

Commit

Permalink
Add recordings download proxy. (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotduffy authored Oct 2, 2022
1 parent 08da9b9 commit 5f31ea8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
18 changes: 2 additions & 16 deletions custom_components/frigate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,7 @@
STATUS_RUNNING,
STATUS_STARTING,
)
from .views import (
JSMPEGProxyView,
NotificationsProxyView,
SnapshotsProxyView,
ThumbnailsProxyView,
VodProxyView,
VodSegmentProxyView,
)
from .views import async_setup as views_async_setup
from .ws_api import async_setup as ws_api_async_setup

SCAN_INTERVAL = timedelta(seconds=5)
Expand Down Expand Up @@ -171,14 +164,7 @@ async def async_setup(hass: HomeAssistant, config: Config) -> bool:
hass.data.setdefault(DOMAIN, {})

ws_api_async_setup(hass)

session = async_get_clientsession(hass)
hass.http.register_view(JSMPEGProxyView(session))
hass.http.register_view(NotificationsProxyView(session))
hass.http.register_view(SnapshotsProxyView(session))
hass.http.register_view(ThumbnailsProxyView(session))
hass.http.register_view(VodProxyView(session))
hass.http.register_view(VodSegmentProxyView(session))
views_async_setup(hass)
return True


Expand Down
31 changes: 31 additions & 0 deletions custom_components/frigate/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_URL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession

_LOGGER: logging.Logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -96,6 +97,18 @@ def get_frigate_instance_id_for_config_entry(
return get_frigate_instance_id(config) if config else None


def async_setup(hass: HomeAssistant) -> None:
"""Set up the views."""
session = async_get_clientsession(hass)
hass.http.register_view(JSMPEGProxyView(session))
hass.http.register_view(NotificationsProxyView(session))
hass.http.register_view(SnapshotsProxyView(session))
hass.http.register_view(RecordingProxyView(session))
hass.http.register_view(ThumbnailsProxyView(session))
hass.http.register_view(VodProxyView(session))
hass.http.register_view(VodSegmentProxyView(session))


# These proxies are inspired by:
# - https://github.com/home-assistant/supervisor/blob/main/supervisor/api/ingress.py

Expand Down Expand Up @@ -211,6 +224,24 @@ def _create_path(self, **kwargs: Any) -> str | None:
return f"api/events/{kwargs['eventid']}/snapshot.jpg"


class RecordingProxyView(ProxyView):
"""A proxy for recordings."""

url = "/api/frigate/{frigate_instance_id:.+}/recording/{camera:.+}/start/{start:[.0-9]+}/end/{end:[.0-9]*}"
extra_urls = [
"/api/frigate/recording/{camera:.+}/start/{start:[.0-9]+}/end/{end:[.0-9]*}"
]

name = "api:frigate:recording"

def _create_path(self, **kwargs: Any) -> str | None:
"""Create path."""
return (
f"api/{kwargs['camera']}/start/{kwargs['start']}"
+ f"/end/{kwargs['end']}/clip.mp4"
)


class ThumbnailsProxyView(ProxyView):
"""A proxy for snapshots."""

Expand Down
18 changes: 18 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ async def handler(request: web.Request) -> web.Response:
web.get("/api/events/1635807359.123456-random/snapshot.jpg", handler),
web.get("/live/front_door", ws_echo_handler),
web.get("/live/querystring", ws_qs_echo_handler),
web.get(
"/api/front_door/start/1664067600.02/end/1664068200.03/clip.mp4",
handler,
),
],
)

Expand Down Expand Up @@ -314,6 +318,20 @@ async def test_snapshot_proxy_view_read_error(
assert "Reverse proxy error" in caplog.text


async def test_recordings_proxy_view(
local_frigate: Any,
hass_client: Any,
) -> None:
"""Test recordings proxy."""

authenticated_hass_client = await hass_client()

resp = await authenticated_hass_client.get(
"/api/frigate/recording/front_door/start/1664067600.02/end/1664068200.03"
)
assert resp.status == HTTPStatus.OK


async def test_notifications_proxy_view_thumbnail(
local_frigate: Any,
hass_client_no_auth: Any,
Expand Down

0 comments on commit 5f31ea8

Please sign in to comment.