diff --git a/lib/lsg_irc/link_plugin/imgur.ex b/lib/lsg_irc/link_plugin/imgur.ex index 41b7e08..443afdb 100644 --- a/lib/lsg_irc/link_plugin/imgur.ex +++ b/lib/lsg_irc/link_plugin/imgur.ex @@ -1,90 +1,96 @@ defmodule LSG.IRC.LinkPlugin.Imgur do @behaviour LSG.IRC.LinkPlugin @moduledoc """ # Imgur link preview No options. Needs to have a Imgur API key configured: ``` config :lsg, :imgur, client_id: "xxxxxxxx", client_secret: "xxxxxxxxxxxxxxxxxxxx" ``` """ @impl true + def match(uri = %URI{host: "imgur.io"}, arg) do + match(%URI{uri | host: "imgur.com"}, arg) + end + def match(uri = %URI{host: "i.imgur.io"}, arg) do + match(%URI{uri | host: "i.imgur.com"}, arg) + end def match(uri = %URI{host: "imgur.com", path: "/a/"<>album_id}, _) do {true, %{album_id: album_id}} end def match(uri = %URI{host: "imgur.com", path: "/gallery/"<>album_id}, _) do {true, %{album_id: album_id}} end def match(uri = %URI{host: "i.imgur.com", path: "/"<>image}, _) do [hash, _] = String.split(image, ".", parts: 2) {true, %{image_id: hash}} end def match(_, _), do: false @impl true def post_match(_, _, _, _), do: false def expand(_uri, %{album_id: album_id}, opts) do expand_imgur_album(album_id, opts) end def expand(_uri, %{image_id: image_id}, opts) do expand_imgur_image(image_id, opts) end def expand_imgur_image(image_id, opts) do client_id = Keyword.get(Application.get_env(:lsg, :imgur, []), :client_id, "42") headers = [{"Authorization", "Client-ID #{client_id}"}] options = [] case HTTPoison.get("https://api.imgur.com/3/image/#{image_id}", headers, options) do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> {:ok, json} = Jason.decode(body) data = json["data"] title = String.slice(data["title"] || data["description"], 0, 180) nsfw = if data["nsfw"], do: "(NSFW) - ", else: " " height = Map.get(data, "height") width = Map.get(data, "width") size = Map.get(data, "size") {:ok, "image, #{width}x#{height}, #{size} bytes #{nsfw}#{title}"} other -> :error end end def expand_imgur_album(album_id, opts) do client_id = Keyword.get(Application.get_env(:lsg, :imgur, []), :client_id, "42") headers = [{"Authorization", "Client-ID #{client_id}"}] options = [] case HTTPoison.get("https://api.imgur.com/3/album/#{album_id}", headers, options) do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> {:ok, json} = Jason.decode(body) data = json["data"] title = data["title"] nsfw = data["nsfw"] nsfw = if nsfw, do: "(NSFW) - ", else: "" if data["images_count"] == 1 do [image] = data["images"] title = if title || data["title"] do title = [title, data["title"]] |> Enum.filter(fn(x) -> x end) |> Enum.uniq() |> Enum.join(" — ") "#{title} — " else "" end {:ok, "#{nsfw}#{title}#{image["link"]}"} else title = if title, do: title, else: "Untitled album" {:ok, "#{nsfw}#{title} - #{data["images_count"]} images"} end other -> :error end end end diff --git a/lib/lsg_irc/link_plugin/youtube.ex b/lib/lsg_irc/link_plugin/youtube.ex index 536cab6..6a16332 100644 --- a/lib/lsg_irc/link_plugin/youtube.ex +++ b/lib/lsg_irc/link_plugin/youtube.ex @@ -1,73 +1,72 @@ defmodule LSG.IRC.LinkPlugin.YouTube do @behaviour LSG.IRC.LinkPlugin @moduledoc """ # YouTube link preview needs an API key: ``` config :lsg, :youtube, api_key: "xxxxxxxxxxxxx" ``` options: * `invidious`: Add a link to invidious. Default: "yewtu.be". """ @impl true def match(uri = %URI{host: yt, path: "/watch", query: "v="<>video_id}, _opts) when yt in ["youtube.com", "www.youtube.com"] do {true, %{video_id: video_id}} end def match(%URI{host: "youtu.be", path: "/"<>video_id}, _opts) do {true, %{video_id: video_id}} end def match(_, _), do: false @impl true def post_match(_, _, _, _), do: false @impl true def expand(uri, %{video_id: video_id}, opts) do key = Application.get_env(:lsg, :youtube)[:api_key] params = %{ "part" => "snippet,contentDetails,statistics", "id" => video_id, "key" => key } headers = [] options = [params: params] case HTTPoison.get("https://www.googleapis.com/youtube/v3/videos", [], options) do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> case Jason.decode(body) do {:ok, json} -> item = List.first(json["items"]) if item do snippet = item["snippet"] duration = item["contentDetails"]["duration"] |> String.replace("PT", "") |> String.downcase date = snippet["publishedAt"] |> DateTime.from_iso8601() |> elem(1) |> Timex.format("{relative}", :relative) |> elem(1) line = if host = Keyword.get(opts, :invidious, "yewtu.be") do ["-> https://#{host}/watch?v=#{video_id}"] else [] end {:ok, line ++ ["#{snippet["title"]}", "— #{duration} — uploaded by #{snippet["channelTitle"]} — #{date}" - <> " — #{item["statistics"]["viewCount"]} views, #{item["statistics"]["likeCount"]} likes," - <> " #{item["statistics"]["dislikeCount"]} dislikes"]} + <> " — #{item["statistics"]["viewCount"]} views, #{item["statistics"]["likeCount"]} likes"]} else :error end _ -> :error end end end end