From 336e928eca368c48f1b6ce9ef3ce7164a1ca3ae7 Mon Sep 17 00:00:00 2001 From: Luca Pasquale Date: Thu, 16 May 2024 22:41:12 -0300 Subject: [PATCH] Updated Youtube webhook payload fields --- lib/botchini_web/controllers/youtube_controller.ex | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/botchini_web/controllers/youtube_controller.ex b/lib/botchini_web/controllers/youtube_controller.ex index 886d5b3..3f02f3e 100644 --- a/lib/botchini_web/controllers/youtube_controller.ex +++ b/lib/botchini_web/controllers/youtube_controller.ex @@ -49,19 +49,19 @@ defmodule BotchiniWeb.YoutubeController do end defp process_webhook(conn) do - event = + entry = conn.body_params |> Map.get("feed") |> Map.get("entry") - channel_id = Map.get(event, "{http://www.youtube.com/xml/schemas/2015}channelId") - video_id = Map.get(event, "{http://www.youtube.com/xml/schemas/2015}videoId") + channel_id = Map.get(entry, "yt:channelId") + video_id = Map.get(entry, "yt:videoId") Logger.info("Received webhook from youtube channel #{channel_id}, video #{video_id}") cache_key = "youtube_event:#{channel_id}:#{video_id}" - if should_notify?(event, cache_key) do + if should_notify?(entry, cache_key) do send_new_video_messages(channel_id, video_id) {:ok, _} = Cache.set(cache_key, @event_ttl, fn -> true end) @@ -70,10 +70,10 @@ defmodule BotchiniWeb.YoutubeController do text(conn, "ok") end - defp should_notify?(event, cache_key) do + defp should_notify?(entry, cache_key) do {:ok, notified} = Cache.get(cache_key) - {:ok, published_at, _} = DateTime.from_iso8601(event["published"]) + {:ok, published_at, _} = DateTime.from_iso8601(entry["published"]) published_recently = abs(DateTime.diff(published_at, DateTime.utc_now(), :hour)) <= 24 !notified && published_recently