summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/link_plugin/imgur.ex
blob: 41b7e08ce6504fdbf6e78bc882f6597599fcc249 (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
defmodule LSG.IRC.LinkPlugin.Imgur do
  @behaviour LSG.IRC.LinkPlugin

  @moduledoc """
  # Imgur link preview

  No options.

  Needs to have a Imgur API key configured:

  ```
  config :lsg, :imgur,
    client_id: "xxxxxxxx",
    client_secret: "xxxxxxxxxxxxxxxxxxxx"
  ```
  """

  @impl true
  def match(uri = %URI{host: "imgur.com", path: "/a/"<>album_id}, _) do
    {true, %{album_id: album_id}}
  end
  def match(uri = %URI{host: "imgur.com", path: "/gallery/"<>album_id}, _) do
    {true, %{album_id: album_id}}
  end
  def match(uri = %URI{host: "i.imgur.com", path: "/"<>image}, _) do
    [hash, _] = String.split(image, ".", parts: 2)
    {true, %{image_id: hash}}
  end
  def match(_, _), do: false

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

  def expand(_uri, %{album_id: album_id}, opts) do
    expand_imgur_album(album_id, opts)
  end

  def expand(_uri, %{image_id: image_id}, opts) do
    expand_imgur_image(image_id, opts)
  end

  def expand_imgur_image(image_id, opts) do
    client_id = Keyword.get(Application.get_env(:lsg, :imgur, []), :client_id, "42")
    headers = [{"Authorization", "Client-ID #{client_id}"}]
    options = []
    case HTTPoison.get("https://api.imgur.com/3/image/#{image_id}", headers, options) do
      {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
        {:ok, json} = Jason.decode(body)
        data = json["data"]
        title = String.slice(data["title"] || data["description"], 0, 180)
        nsfw = if data["nsfw"], do: "(NSFW) - ", else: " "
        height = Map.get(data, "height")
        width = Map.get(data, "width")
        size = Map.get(data, "size")
        {:ok, "image, #{width}x#{height}, #{size} bytes #{nsfw}#{title}"}
      other ->
        :error
    end
  end

  def expand_imgur_album(album_id, opts) do
    client_id = Keyword.get(Application.get_env(:lsg, :imgur, []), :client_id, "42")
    headers = [{"Authorization", "Client-ID #{client_id}"}]
    options = []
    case HTTPoison.get("https://api.imgur.com/3/album/#{album_id}", headers, options) do
      {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
        {:ok, json} = Jason.decode(body)
        data = json["data"]
        title = data["title"]
        nsfw = data["nsfw"]
        nsfw = if nsfw, do: "(NSFW) - ", else: ""
        if data["images_count"] == 1 do
          [image] = data["images"]
          title = if title || data["title"] do
            title = [title, data["title"]] |> Enum.filter(fn(x) -> x end) |> Enum.uniq() |> Enum.join(" — ")
            "#{title} — "
          else
            ""
          end
          {:ok, "#{nsfw}#{title}#{image["link"]}"}
        else
          title = if title, do: title, else: "Untitled album"
          {:ok, "#{nsfw}#{title} - #{data["images_count"]} images"}
        end
      other ->
        :error
    end
  end

end