summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/link_plugin/twitter.ex
blob: 41e10ab2c04fbfd78c4af755234df9867e6adde3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
defmodule LSG.IRC.LinkPlugin.Twitter do
  @behaviour LSG.IRC.LinkPlugin

  @moduledoc """
  # Twitter Link Preview

  Configuration:

  needs an API key and auth tokens:

  ```
  config :extwitter, :oauth, [
   consumer_key: "zzzzz",
   consumer_secret: "xxxxxxx",
   access_token: "yyyyyy",
   access_token_secret: "ssshhhhhh"
  ]
  ```

  options:

  * `expand_quoted`: Add the quoted tweet instead of its URL. Default: true.
  """

  def match(uri = %URI{host: twitter, path: path}, _opts) when twitter in ["twitter.com", "m.twitter.com", "mobile.twitter.com"] do
    case String.split(path, "/", parts: 4) do
      ["", _username, "status", status_id] ->
        {status_id, _} = Integer.parse(status_id)
        {true, %{status_id: status_id}}
      _ -> false
    end
  end

  def match(_, _), do: false

  @impl true
  def post_match(_, _, _, _), do: false

  def expand(_uri, %{status_id: status_id}, opts) do
    expand_tweet(ExTwitter.show(status_id, tweet_mode: "extended"), opts)
  end

  defp expand_tweet(nil, _opts) 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
    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)

    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
      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

    #<<2, "#{tweet.user.name} (@#{tweet.user.screen_name})", 2, " ", 3, 61, "#{foot} #{nitter_link}", 3>>, reply_to] ++ text ++ quoted

    text = [head, reply_to | text] ++ quoted
    |> Enum.filter(& &1)
    {:ok, text}
  end

  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)
    extended = tweet.extended_entities || %{media: []}
    text = Enum.reduce(extended.media, text, fn(entity, text) ->
      url = Enum.filter(extended.media, fn(e) -> entity.url == e.url end)
      |> Enum.map(fn(e) ->
        cond do
          e.type == "video" -> e.expanded_url
          true -> e.media_url_https
        end
      end)
      |> 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