Add linkbot contrib module
Linkbot listens for http:// and https:// links, and attempts to get their titles. This also adds the "tesla" dependency to mix.exs Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
38
lib/contrib/linkbot.ex
Normal file
38
lib/contrib/linkbot.ex
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
defmodule Omnibot.Contrib.Linkbot do
|
||||||
|
use Omnibot.Module
|
||||||
|
require Logger
|
||||||
|
|
||||||
|
defmodule Client do
|
||||||
|
use Tesla
|
||||||
|
|
||||||
|
plug Tesla.Middleware.Headers, [{"user-agent", "Tesla/Omnibot"}]
|
||||||
|
plug Tesla.Middleware.FollowRedirects, max_redirects: 10
|
||||||
|
plug Tesla.Middleware.Compression, format: "gzip"
|
||||||
|
|
||||||
|
@title_regex ~r"<title>(?<title>.+)</title>"i
|
||||||
|
|
||||||
|
def get_title(url) do
|
||||||
|
if should_get?(url) do
|
||||||
|
resp = get!(url)
|
||||||
|
%{"title" => title} = Regex.named_captures(@title_regex, resp.body)
|
||||||
|
title
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp should_get?(url) do
|
||||||
|
resp = head!(url)
|
||||||
|
Tesla.get_header(resp, "content-type")
|
||||||
|
|> String.downcase()
|
||||||
|
|> String.contains?(["html", "text"])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@url_regex ~r"\bhttps?://[^\s]+"
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def on_channel_msg(irc, channel, _nick, line) do
|
||||||
|
Regex.scan(@url_regex, line)
|
||||||
|
|> Enum.map(fn url -> Client.get_title(url) end)
|
||||||
|
|> Enum.each(fn title -> Irc.send_to(irc, channel, title) end)
|
||||||
|
end
|
||||||
|
end
|
||||||
5
mix.exs
5
mix.exs
@@ -29,9 +29,6 @@ defmodule Omnibot.MixProject do
|
|||||||
|
|
||||||
# Run "mix help deps" to learn about dependencies.
|
# Run "mix help deps" to learn about dependencies.
|
||||||
defp deps do
|
defp deps do
|
||||||
[
|
[{:tesla, "~> 1.3.0"}]
|
||||||
# {:dep_from_hexpm, "~> 0.3.0"},
|
|
||||||
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
|
||||||
]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user