summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Bracco <href@random.sh>2022-12-09 17:33:38 +0000
committerJordan Bracco <href@random.sh>2022-12-11 02:03:36 +0000
commit4c955dc5e80020f4e61bdcda3d88c4647ce61b17 (patch)
tree4acd423003361c99cf5a0bb4f2c415da318d4736
parentgpt web wip (diff)
link_plugin: improve twitter, youtube & logging
-rw-r--r--lib/lsg_irc/link_plugin.ex9
-rw-r--r--lib/lsg_irc/link_plugin/twitter.ex105
-rw-r--r--lib/lsg_irc/link_plugin/youtube.ex4
3 files changed, 88 insertions, 30 deletions
diff --git a/lib/lsg_irc/link_plugin.ex b/lib/lsg_irc/link_plugin.ex
index 3d657ad..aaf6c6f 100644
--- a/lib/lsg_irc/link_plugin.ex
+++ b/lib/lsg_irc/link_plugin.ex
@@ -111,8 +111,10 @@ defmodule LSG.IRC.LinkPlugin do
end
def expand_link(acc=[uri | _]) do
+ Logger.debug("link: expanding: #{inspect uri}")
handlers = Keyword.get(Application.get_env(:lsg, __MODULE__, [handlers: []]), :handlers)
handler = Enum.reduce_while(handlers, nil, fn({module, opts}, acc) ->
+ Logger.debug("link: attempt expanding: #{inspect module} for #{inspect uri}")
module = Module.concat([module])
case module.match(uri, opts) do
{true, params} -> {:halt, {module, params, opts}}
@@ -127,6 +129,7 @@ defmodule LSG.IRC.LinkPlugin do
end
def run_expand(acc=[uri|_], {module, params, opts}) do
+ Logger.debug("link: expanding #{inspect uri} with #{inspect module}")
case module.expand(uri, params, opts) do
{:ok, data} -> {:ok, acc, data}
:error -> expand_default(acc)
@@ -134,11 +137,12 @@ defmodule LSG.IRC.LinkPlugin do
end
rescue
e ->
- Logger.error(inspect(e))
+ Logger.error("link: rescued #{inspect uri} with #{inspect module}: #{inspect e}")
+ Logger.error(Exception.format(:error, e, __STACKTRACE__))
expand_default(acc)
catch
e, b ->
- Logger.error(inspect({b}))
+ Logger.error("link: catched #{inspect uri} with #{inspect module}: #{inspect {e, b}}")
expand_default(acc)
end
@@ -232,6 +236,7 @@ defmodule LSG.IRC.LinkPlugin do
end
def expand_default(acc = [uri = %URI{scheme: scheme} | _]) when scheme in ["http", "https"] do
+ Logger.debug("link: expanding #{uri} with default")
headers = [{"user-agent", "DmzBot (like TwitterBot)"}]
options = [follow_redirect: false, max_body_length: 30_000_000]
case get(URI.to_string(uri), headers, options) do
diff --git a/lib/lsg_irc/link_plugin/twitter.ex b/lib/lsg_irc/link_plugin/twitter.ex
index e462384..41e10ab 100644
--- a/lib/lsg_irc/link_plugin/twitter.ex
+++ b/lib/lsg_irc/link_plugin/twitter.ex
@@ -44,47 +44,58 @@ defmodule LSG.IRC.LinkPlugin.Twitter do
:error
end
+ defp link_tweet(tweet_or_screen_id_tuple, opts, force_twitter_com \\ false)
+
+ defp link_tweet({screen_name, id}, opts, force_twitter_com) do
+ path = "/#{screen_name}/status/#{id}"
+ nitter = Keyword.get(opts, :nitter)
+ host = if !force_twitter_com && nitter, do: nitter, else: "twitter.com"
+ "https://#{host}/#{screen_name}/status/#{id}"
+ end
+
+ defp link_tweet(tweet, opts, force_twitter_com) do
+ link_tweet({tweet.user.screen_name, tweet.id}, opts, force_twitter_com)
+ end
+
defp expand_tweet(tweet, opts) do
- text = expand_twitter_text(tweet)
- text = if tweet.quoted_status do
- quote_url = "https://twitter.com/#{tweet.quoted_status.user.screen_name}/status/#{tweet.quoted_status.id}"
- String.replace(text, quote_url, "")
- else
- text
- end
+ head = format_tweet_header(tweet, opts)
+ # Format tweet text
+ text = expand_twitter_text(tweet, opts)
+ text = if tweet.quoted_status do
+ quote_url = link_tweet(tweet.quoted_status, opts, true)
+ String.replace(text, quote_url, "")
+ else
+ text
+ end
text = IRC.splitlong(text)
- {:ok, at} = Timex.parse(tweet.created_at, "%a %b %e %H:%M:%S %z %Y", :strftime)
- {:ok, format} = Timex.format(at, "{relative}", :relative)
-
- replyto = if tweet.in_reply_to_status_id do
- replyurl = "https://twitter.com/#{tweet.in_reply_to_screen_name}/status/#{tweet.in_reply_to_status_id}"
- if tweet.in_reply_to_screen_name == tweet.user.screen_name do
- "— continued from #{replyurl}"
- else
- "— replying to #{replyurl}"
- end
- else
- ""
+ reply_to = if tweet.in_reply_to_status_id do
+ reply_url = link_tweet({tweet.in_reply_to_screen_name, tweet.in_reply_to_status_id}, opts)
+ text = if tweet.in_reply_to_screen_name == tweet.user.screen_name, do: "continued from", else: "replying to"
+ <<3, 15, " ↪ ", text::binary, " ", reply_url::binary, 3>>
end
quoted = if tweet.quoted_status do
- quote_url = "https://twitter.com/#{tweet.quoted_status.user.screen_name}/status/#{tweet.quoted_status.id}"
- full_text = expand_twitter_text(tweet.quoted_status)
- |> IRC.splitlong_with_prefix(">")
- ["> #{tweet.quoted_status.user.name} (@#{tweet.quoted_status.user.screen_name}): #{quote_url}"] ++ full_text
+ full_text = tweet.quoted_status
+ |> expand_twitter_text(opts)
+ |> IRC.splitlong_with_prefix(">")
+
+ head = format_tweet_header(tweet.quoted_status, opts, details: false, prefix: "↓ quoting")
+
+ [head | full_text]
else
[]
end
- foot = "— #{format} - #{tweet.retweet_count} retweets - #{tweet.favorite_count} likes"
+ #<<2, "#{tweet.user.name} (@#{tweet.user.screen_name})", 2, " ", 3, 61, "#{foot} #{nitter_link}", 3>>, reply_to] ++ text ++ quoted
- text = ["#{tweet.user.name} (@#{tweet.user.screen_name}):", replyto] ++ text ++ quoted ++ [foot]
+ text = [head, reply_to | text] ++ quoted
+ |> Enum.filter(& &1)
{:ok, text}
end
- defp expand_twitter_text(tweet) do
+ defp expand_twitter_text(tweet, _opts) do
text = Enum.reduce(tweet.entities.urls, tweet.full_text, fn(entity, text) ->
String.replace(text, entity.url, entity.expanded_url)
end)
@@ -100,6 +111,48 @@ defmodule LSG.IRC.LinkPlugin.Twitter do
|> Enum.join(" ")
String.replace(text, entity.url, url)
end)
+ |> HtmlEntities.decode()
+ end
+
+ defp format_tweet_header(tweet, opts, format_opts \\ []) do
+ prefix = Keyword.get(format_opts, :prefix, nil)
+ details = Keyword.get(format_opts, :details, true)
+
+ padded_prefix = if prefix, do: "#{prefix} ", else: ""
+ author = <<padded_prefix::binary, 2, "#{tweet.user.name} (@#{tweet.user.screen_name})", 2>>
+
+ link = link_tweet(tweet, opts)
+
+ {:ok, at} = Timex.parse(tweet.created_at, "%a %b %e %H:%M:%S %z %Y", :strftime)
+ {:ok, formatted_time} = Timex.format(at, "{relative}", :relative)
+
+ nsfw = if tweet.possibly_sensitive, do: <<3, 52, "NSFW", 3>>
+
+ rts = if tweet.retweet_count && tweet.retweet_count > 0, do: "#{tweet.retweet_count} RT"
+ likes = if tweet.favorite_count && tweet.favorite_count > 0, do: "#{tweet.favorite_count} ❤︎"
+ qrts = if tweet.quote_count && tweet.quote_count > 0, do: "#{tweet.quote_count} QRT"
+ replies = if tweet.reply_count && tweet.reply_count > 0, do: "#{tweet.reply_count} Reps"
+
+ dmcad = if tweet.withheld_copyright, do: <<3, 52, "DMCA", 3>>
+ withheld_local = if tweet.withheld_in_countries && length(tweet.withheld_in_countries) > 0 do
+ "Withheld in #{length(tweet.withheld_in_countries)} countries"
+ end
+
+ verified = if tweet.user.verified, do: <<3, 51, "✔", 3>>
+
+ meta = if details do
+ [verified, nsfw, formatted_time, dmcad, withheld_local, rts, qrts, likes, replies]
+ else
+ [verified, nsfw, formatted_time, dmcad, withheld_local]
+ end
+
+ meta = meta
+ |> Enum.filter(& &1)
+ |> Enum.join(" - ")
+
+ meta = <<3, 15, meta::binary, " → #{link}", 3>>
+
+ <<author::binary, " — ", meta::binary>>
end
end
diff --git a/lib/lsg_irc/link_plugin/youtube.ex b/lib/lsg_irc/link_plugin/youtube.ex
index 6a16332..f38eca3 100644
--- a/lib/lsg_irc/link_plugin/youtube.ex
+++ b/lib/lsg_irc/link_plugin/youtube.ex
@@ -13,7 +13,7 @@ defmodule LSG.IRC.LinkPlugin.YouTube do
options:
- * `invidious`: Add a link to invidious. Default: "yewtu.be".
+ * `invidious`: Add a link to invidious.
"""
@impl true
@@ -54,7 +54,7 @@ defmodule LSG.IRC.LinkPlugin.YouTube do
|> Timex.format("{relative}", :relative)
|> elem(1)
- line = if host = Keyword.get(opts, :invidious, "yewtu.be") do
+ line = if host = Keyword.get(opts, :invidious) do
["-> https://#{host}/watch?v=#{video_id}"]
else
[]