From 83f87139fbf18b1bf06347aa5e226cd1edc4f7cc Mon Sep 17 00:00:00 2001 From: Collin Heist Date: Wed, 21 Aug 2024 22:11:44 -0600 Subject: [PATCH] [103] Allow integration with Plex Webhooks Create POST /api/webhooks/plex endpoint which can be triggered directly by Plex to immediately cause Card creation --- app/routers/webhooks.py | 35 +++++++++++++++++++++++++++++++++++ modules/ref/version_webui | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) mode change 100644 => 100755 app/routers/webhooks.py diff --git a/app/routers/webhooks.py b/app/routers/webhooks.py old mode 100644 new mode 100755 index d2a19e99..f02d5f55 --- a/app/routers/webhooks.py +++ b/app/routers/webhooks.py @@ -63,6 +63,41 @@ def create_cards_for_plex_rating_key( ) +@webhook_router.post('/plex', tags=['Plex']) +async def process_plex_webhook( + request: Request, + # FastAPI cannot parse the payload, for some reason, so this needs to + # be parsed from the request.form() directly + # webhook: PlexWebhook = Form(...), + snapshot: bool = Query(default=True), + trigger_on: Optional[str] = Query(default=None), + db: Session = Depends(get_database), + plex_interface: PlexInterface = Depends(require_plex_interface), + ) -> None: + """ + """ + + # Get contextual logger + log: Logger = request.state.log + + webhook = PlexWebhook.parse_raw((await request.form()).get('payload')) + + # Only process new or watched content + if (webhook.event not in ('library.new', 'media.scrobble') + and (not trigger_on + or (trigger_on and webhook.event not in trigger_on))): + log.debug(f'Skipping Webhook of type "{webhook.event}"') + return None + + return process_rating_key( + db, + plex_interface, + webhook.Metadata.ratingKey, + snapshot=snapshot, + log=log, + ) + + @webhook_router.post('/sonarr/cards', tags=['Sonarr']) def create_cards_for_sonarr_webhook( request: Request, diff --git a/modules/ref/version_webui b/modules/ref/version_webui index 67882b3f..07572cb7 100755 --- a/modules/ref/version_webui +++ b/modules/ref/version_webui @@ -1 +1 @@ -v2.0-alpha.11.0-webui102 \ No newline at end of file +v2.0-alpha.11.0-webui103 \ No newline at end of file