diff options
author | href <href@random.sh> | 2021-09-01 10:30:18 +0200 |
---|---|---|
committer | href <href@random.sh> | 2021-09-01 10:30:18 +0200 |
commit | 75687711f35355bc30e4829439384aab28fcac6d (patch) | |
tree | 8f3256f472893c39720a684d390e890a152f7303 /lib/lsg_irc/outline_plugin.ex | |
parent | link: post_* callbacks; html & pdftitle. (diff) |
Commit all the changes that hasn't been committed + updates.
Diffstat (limited to '')
-rw-r--r-- | lib/lsg_irc/outline_plugin.ex | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/lib/lsg_irc/outline_plugin.ex b/lib/lsg_irc/outline_plugin.ex index 7bfaac1..471448b 100644 --- a/lib/lsg_irc/outline_plugin.ex +++ b/lib/lsg_irc/outline_plugin.ex @@ -12,14 +12,15 @@ defmodule LSG.IRC.OutlinePlugin do require Logger def start_link() do - GenServer.start_link(__MODULE__, []) + GenServer.start_link(__MODULE__, [], name: __MODULE__) end defstruct [:file, :hosts] def init([]) do - {:ok, _} = Registry.register(IRC.PubSub, "trigger:outline", []) - {:ok, _} = Registry.register(IRC.PubSub, "message", []) + regopts = [plugin: __MODULE__] + {:ok, _} = Registry.register(IRC.PubSub, "trigger:outline", regopts) + {:ok, _} = Registry.register(IRC.PubSub, "message", regopts) file = Path.join(LSG.data_path, "/outline.txt") hosts = case File.read(file) do {:error, :enoent} -> @@ -51,7 +52,8 @@ defmodule LSG.IRC.OutlinePlugin do uri = URI.parse(word) if uri.scheme && uri.host do if Enum.any?(state.hosts, fn(host) -> String.ends_with?(uri.host, host) end) do - line = "-> https://outline.com/#{word}" + outline_url = outline(word) + line = "-> #{outline(word)}" message.replyfun.(line) end end @@ -69,4 +71,32 @@ defmodule LSG.IRC.OutlinePlugin do File.write(file, string) end + def outline(url) do + unexpanded = "https://outline.com/#{url}" + headers = [ + {"User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0"}, + {"Accept", "*/*"}, + {"Accept-Language", "en-US,en;q=0.5"}, + {"Origin", "https://outline.com"}, + {"DNT", "1"}, + {"Referer", unexpanded}, + {"Pragma", "no-cache"}, + {"Cache-Control", "no-cache"} + ] + params = %{"source_url" => url} + case HTTPoison.get("https://api.outline.com/v3/parse_article", headers, params: params) do + {:ok, %HTTPoison.Response{status_code: 200, body: json}} -> + body = Poison.decode!(json) + if Map.get(body, "success") do + code = get_in(body, ["data", "short_code"]) + "https://outline.com/#{code}" + else + unexpanded + end + error -> + Logger.info("outline.com error: #{inspect error}") + unexpanded + end + end + end |