diff --git a/lib/botchini/services/twitch/twitch.ex b/lib/botchini/services/twitch/twitch.ex index 86ab23a..06c6164 100644 --- a/lib/botchini/services/twitch/twitch.ex +++ b/lib/botchini/services/twitch/twitch.ex @@ -3,23 +3,17 @@ defmodule Botchini.Services.Twitch do Handles communication with Twitch API """ - # use Tesla - alias Botchini.Services.Twitch.AuthMiddleware alias Botchini.Services.Twitch.Structs.{Channel, Stream, User} - # plug(Tesla.Middleware.JSON) - # plug(Tesla.Middleware.Logger) - # plug(Botchini.Services.Twitch.AuthMiddleware) - # plug(Tesla.Middleware.BaseUrl, "https://api.twitch.tv/helix") - - # plug(Tesla.Middleware.Headers, [ - # {"Client-ID", Application.fetch_env!(:botchini, :twitch_client_id)} - # ]) - @spec search_channels(String.t()) :: list(Channel.t()) def search_channels(term) do - resp = api() |> Req.get!(url: "/search/channels", params: [query: term, first: 10]) + resp = + Req.get!( + api(), + url: "/search/channels", + params: [query: term, first: 10] + ) resp.body |> Map.get("data") @@ -28,7 +22,12 @@ defmodule Botchini.Services.Twitch do @spec get_user(String.t()) :: User.t() | nil def get_user(user_id) do - resp = api() |> Req.get!(url: "/users", params: [id: user_id]) + resp = + Req.get!( + api(), + url: "/users", + params: [id: user_id] + ) user = resp.body @@ -40,50 +39,68 @@ defmodule Botchini.Services.Twitch do @spec get_user_by_user_login(String.t()) :: User.t() | nil def get_user_by_user_login(user_login) do - # {:ok, %{body: body}} = get("/users", query: [login: String.downcase(user_login)]) + resp = + Req.get!( + api(), + url: "/users", + params: [login: String.downcase(user_login)] + ) - # user = - # body - # |> Map.get("data") - # |> List.first() + user = + resp.body + |> Map.get("data") + |> List.first() - # if user != nil, do: User.new(user), else: nil + if user != nil, do: User.new(user), else: nil end @spec get_stream(String.t()) :: Stream.t() | nil def get_stream(user_id) do - # {:ok, %{body: body}} = get("/streams", query: [user_id: user_id]) - - # stream = - # body - # |> Map.get("data") - # |> List.first() + resp = + Req.get!( + api(), + url: "/streams", + params: [user_id: user_id] + ) + + stream = + resp.body + |> Map.get("data") + |> List.first() - # if stream != nil, do: Stream.new(stream), else: nil + if stream != nil, do: Stream.new(stream), else: nil end @spec add_stream_webhook(String.t()) :: any() def add_stream_webhook(user_id) do - # {:ok, %{body: body}} = - # post("/eventsub/subscriptions", %{ - # type: "stream.online", - # version: "1", - # condition: %{broadcaster_user_id: user_id}, - # transport: %{ - # method: "webhook", - # callback: Application.fetch_env!(:botchini, :host) <> "/api/twitch/webhooks/callback", - # secret: Application.fetch_env!(:botchini, :twitch_webhook_secret) - # } - # }) - - # body - # |> Map.get("data") - # |> List.first() + resp = + Req.post!( + api(), + url: "/eventsub/subscriptions", + json: %{ + type: "stream.online", + version: "1", + condition: %{broadcaster_user_id: user_id}, + transport: %{ + method: "webhook", + callback: Application.fetch_env!(:botchini, :host) <> "/api/twitch/webhooks/callback", + secret: Application.fetch_env!(:botchini, :twitch_webhook_secret) + } + } + ) + + resp.body + |> Map.get("data") + |> List.first() end @spec delete_stream_webhook(String.t()) :: any() def delete_stream_webhook(subscription_id) do - # delete("/eventsub/subscriptions", query: [id: subscription_id]) + Req.delete!( + api(), + url: "/eventsub/subscriptions", + params: [id: subscription_id] + ) end defp api() do diff --git a/lib/botchini/services/youtube/youtube.ex b/lib/botchini/services/youtube/youtube.ex index 436b80f..3a4d6bd 100644 --- a/lib/botchini/services/youtube/youtube.ex +++ b/lib/botchini/services/youtube/youtube.ex @@ -3,91 +3,85 @@ defmodule Botchini.Services.Youtube do Handles communication with YouTube API """ - # use Tesla - # alias Tesla.Multipart require Logger alias Botchini.Services.Youtube.Structs.{Channel, Video} - # plug(Tesla.Middleware.JSON) - # plug(Tesla.Middleware.Logger) - - # plug(Tesla.Middleware.Query, - # key: Application.fetch_env!(:botchini, :youtube_api_key) - # ) - - @youtube_api "https://www.googleapis.com/youtube/v3" - @spec search_channels(String.t()) :: list(Channel.t()) def search_channels(term) do - # {:ok, %{body: body}} = - # get("#{@youtube_api}/search", - # query: [part: "snippet", type: "channel", q: term] - # ) - - # case Map.get(body, "items") do - # nil -> - # [] - - # items -> - # Enum.map(items, fn item -> - # Channel.new(%{id: item["id"]["channelId"], snippet: item["snippet"]}) - # end) - # end + resp = + Req.get!( + api(), + url: "/search", + params: [part: "snippet", type: "channel", q: term] + ) + + case Map.get(resp.body, "items") do + nil -> + [] + + items -> + Enum.map(items, fn item -> + Channel.new(%{id: item["id"]["channelId"], snippet: item["snippet"]}) + end) + end end @spec get_channel(String.t()) :: Channel.t() | nil def get_channel(channel_id) do - # {:ok, %{body: body}} = - # get("#{@youtube_api}/channels", - # query: [part: "snippet", id: channel_id] - # ) - - # case Map.get(body, "items") do - # nil -> - # nil - - # items -> - # List.first(items) - # |> Channel.new() - # end + resp = + Req.get!( + api(), + url: "/channels", + params: [part: "snippet", id: channel_id] + ) + + case Map.get(resp.body, "items") do + nil -> + nil + + items -> + List.first(items) + |> Channel.new() + end end @spec get_video(String.t()) :: Video.t() | nil def get_video(video_id) do - # {:ok, %{body: body}} = - # get("https://www.googleapis.com/youtube/v3/videos", - # query: [part: "snippet,liveStreamingDetails", id: video_id] - # ) - - # case Map.get(body, "items") do - # nil -> - # nil - - # items -> - # List.first(items) - # |> Video.new() - # end + resp = + Req.get!( + api(), + url: "/videos", + params: [part: "snippet,liveStreamingDetails", id: video_id] + ) + + case Map.get(resp.body, "items") do + nil -> + nil + + items -> + List.first(items) + |> Video.new() + end end @spec manage_channel_pubsub(String.t(), boolean()) :: {:ok} def manage_channel_pubsub(channel_id, subscribe) do - # callback_url = "#{Application.fetch_env!(:botchini, :host)}/api/youtube/webhooks/callback" - # topic_url = "https://www.youtube.com/xml/feeds/videos.xml?channel_id=#{channel_id}" - - # mp = - # Multipart.new() - # |> Multipart.add_field("hub.callback", callback_url) - # |> Multipart.add_field("hub.topic", topic_url) - # |> Multipart.add_field("hub.verify", "async") - # |> Multipart.add_field("hub.mode", if(subscribe, do: "subscribe", else: "unsubscribe")) - # |> Multipart.add_field( - # "hub.secret", - # Application.fetch_env!(:botchini, :youtube_webhook_secret) - # ) - - # {:ok, _} = post("https://pubsubhubbub.appspot.com/subscribe", mp) - # {:ok} + callback_url = "#{Application.fetch_env!(:botchini, :host)}/api/youtube/webhooks/callback" + topic_url = "https://www.youtube.com/xml/feeds/videos.xml?channel_id=#{channel_id}" + + Req.post!( + url: "https://pubsubhubbub.appspot.com/subscribe", + form: [ + "hub.verify": "async", + "hub.mode": if(subscribe, do: "subscribe", else: "unsubscribe"), + "hub.callback": callback_url, + "hub.topic": topic_url, + "hub.secret": Application.fetch_env!(:botchini, :youtube_webhook_secret) + ] + ) + + {:ok} end @spec get_video_id_from_url(String.t()) :: String.t() @@ -102,4 +96,11 @@ defmodule Botchini.Services.Youtube do List.last(match) end end + + defp api() do + Req.new( + base_url: "https://www.googleapis.com/youtube/v3", + params: [key: Application.fetch_env!(:botchini, :youtube_api_key)] + ) + end end diff --git a/mix.exs b/mix.exs index 7111638..edfd999 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Botchini.MixProject do def project do [ app: :botchini, - version: "8.9.1", + version: "8.9.2", elixir: "~> 1.16.1", build_embedded: Mix.env() == :prod, start_permanent: Mix.env() == :prod, @@ -51,9 +51,6 @@ defmodule Botchini.MixProject do # HTTP Client {:req, "~> 0.4.0"}, {:exconstructor, "~> 1.2.13"}, - # {:tesla, "~> 1.9.0"}, - # {:gun, "~> 2.1.0"}, - # {:hackney, "~> 1.20.1"}, # Helpers {:ink, "~> 1.0"}, {:quantum, "~> 3.0"},