- Using the @default_config attribute in a module will fill out a default value. - cfg[:timeout] sets the timeout for the task for this module to finish. - Update Omnibot.Irc.route_msg/2 to have each task spawn a second task, while the first task waits for its child to finish, otherwise killing it. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
42 lines
1.0 KiB
Elixir
42 lines
1.0 KiB
Elixir
defmodule Omnibot.Contrib.Linkbot do
|
|
use Omnibot.Module
|
|
require Logger
|
|
|
|
@default_config timeout: 30_000
|
|
|
|
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
|
|
Process.sleep(11_000)
|
|
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
|