Skip to content

Commit

Permalink
Updated Youtube webhook payload fields
Browse files Browse the repository at this point in the history
  • Loading branch information
lucapasquale committed May 17, 2024
1 parent d208d78 commit 336e928
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/botchini_web/controllers/youtube_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 336e928

Please sign in to comment.