summaryrefslogtreecommitdiff
path: root/lib/plugins/finance.ex
blob: d6f890acdab6b27afff7783b48662254709b2d1c (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
defmodule Nola.Plugins.Finance do
  require Logger

  @moduledoc """
  # finance

  Données de [alphavantage.co](https://alphavantage.co).

  ## forex / monnaies / crypto-monnaies

  * **`!forex <MONNAIE1> [MONNAIE2]`**: taux de change entre deux monnaies.
  * **`!forex <MONTANT> <MONNAIE1> <MONNAIE2>`**: converti `montant` entre deux monnaies
  * **`?currency <recherche>`**: recherche une monnaie

  Utiliser le symbole des monnaies (EUR, USD, ...).

  ## bourses

  * **`!stocks <SYMBOLE>`**
  * **`?stocks <recherche>`** cherche un symbole

  Pour les symboles non-US, ajouter le suffixe (RNO Paris: RNO.PAR).

  """

  @currency_list "http://www.alphavantage.co/physical_currency_list/"
  @crypto_list "http://www.alphavantage.co/digital_currency_list/"

  HTTPoison.start()
  load_currency = fn(url) ->
    resp = HTTPoison.get!(url)
    resp.body
    |> String.strip()
    |> String.split("\n")
    |> Enum.drop(1)
    |> Enum.map(fn(line) ->
      [symbol, name] = line
      |> String.strip()
      |> String.split(",", parts: 2)
      {symbol, name}
    end)
    |> Enum.into(Map.new)
  end
  fiat = load_currency.(@currency_list)
  crypto = load_currency.(@crypto_list)
  @currencies Map.merge(fiat, crypto)

  def irc_doc, do: @moduledoc
  def start_link() do
    GenServer.start_link(__MODULE__, [], name: __MODULE__)
  end

  def init([]) do
    regopts = [plugin: __MODULE__]
    {:ok, _} = Registry.register(IRC.PubSub, "trigger:forex", regopts)
    {:ok, _} = Registry.register(IRC.PubSub, "trigger:currency", regopts)
    {:ok, _} = Registry.register(IRC.PubSub, "trigger:stocks", regopts)
    {:ok, nil}
  end


  def handle_info({:irc, :trigger, "stocks", message = %{trigger: %{type: :query, args: args = search}}}, state) do
    search = Enum.join(search, "%20")
    url = "https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords=#{search}&apikey=#{api_key()}"
    case HTTPoison.get(url) do
      {:ok, %HTTPoison.Response{status_code: 200, body: data}} ->
        data = Poison.decode!(data)
        if error = Map.get(data, "Error Message") do
          Logger.error("AlphaVantage API invalid request #{url} - #{inspect error}")
          message.replyfun.("stocks: requête invalide")
        else
          items = for item <- Map.get(data, "bestMatches") do
            symbol = Map.get(item, "1. symbol")
            name = Map.get(item, "2. name")
            type = Map.get(item, "3. type")
            region = Map.get(item, "4. region")
            currency = Map.get(item, "8. currency")
            "#{symbol}: #{name} (#{region}; #{currency}; #{type})"
          end
          |> Enum.join(", ")
          items = if items == "" do
            "no results!"
          else
            items
          end
          message.replyfun.(items)
        end
      {:ok, resp = %HTTPoison.Response{status_code: code}} ->
        Logger.error "AlphaVantage API error: #{code} #{url} - #{inspect resp}"
        message.replyfun.("forex: erreur (api #{code})")
      {:error, %HTTPoison.Error{reason: error}} ->
        Logger.error "AlphaVantage HTTP error: #{inspect error}"
        message.replyfun.("forex: erreur (http #{inspect error})")
    end
    {:noreply, state}
  end

  def handle_info({:irc, :trigger, "stocks", message = %{trigger: %{type: :bang, args: args = [symbol]}}}, state) do
    url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=#{symbol}&apikey=#{api_key()}"
    case HTTPoison.get(url) do
      {:ok, %HTTPoison.Response{status_code: 200, body: data}} ->
        data = Poison.decode!(data)
        if error = Map.get(data, "Error Message") do
          Logger.error("AlphaVantage API invalid request #{url} - #{inspect error}")
          message.replyfun.("stocks: requête invalide")
        else
          data = Map.get(data, "Global Quote")
          open = Map.get(data, "02. open")
          high = Map.get(data, "03. high")
          low = Map.get(data, "04. low")
          price = Map.get(data, "05. price")
          volume = Map.get(data, "06. volume")
          prev_close = Map.get(data, "08. previous close")
          change = Map.get(data, "09. change")
          change_pct = Map.get(data, "10. change percent")

          msg = "#{symbol}: #{price} #{change} [#{change_pct}] (high: #{high}, low: #{low}, open: #{open}, prev close: #{prev_close}) (volume: #{volume})"
          message.replyfun.(msg)
        end
      {:ok, resp = %HTTPoison.Response{status_code: code}} ->
        Logger.error "AlphaVantage API error: #{code} #{url} - #{inspect resp}"
        message.replyfun.("stocks: erreur (api #{code})")
      {:error, %HTTPoison.Error{reason: error}} ->
        Logger.error "AlphaVantage HTTP error: #{inspect error}"
        message.replyfun.("stocks: erreur (http #{inspect error})")
    end
    {:noreply, state}
  end


  def handle_info({:irc, :trigger, "forex", message = %{trigger: %{type: :bang, args: args = [_ | _]}}}, state) do
    {amount, from, to} = case args do
      [amount, from, to] ->
        {amount, _} = Float.parse(amount)
        {amount, from, to}
      [from, to] ->
        {1, from, to}
      [from] ->
        {1, from, "EUR"}
    end
    url = "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=#{from}&to_currency=#{to}&apikey=#{api_key()}"
    case HTTPoison.get(url) do
      {:ok, %HTTPoison.Response{status_code: 200, body: data}} ->
        data = Poison.decode!(data)
        if error = Map.get(data, "Error Message") do
          Logger.error("AlphaVantage API invalid request #{url} - #{inspect error}")
          message.replyfun.("forex: requête invalide")
        else
          data = Map.get(data, "Realtime Currency Exchange Rate")
          from_name = Map.get(data, "2. From_Currency Name")
          to_name = Map.get(data, "4. To_Currency Name")
          rate = Map.get(data, "5. Exchange Rate")
          {rate, _} = Float.parse(rate)
          value = amount*rate
          message.replyfun.("#{amount} #{from} (#{from_name}) -> #{value} #{to} (#{to_name}) (#{rate})")
        end
      {:ok, resp = %HTTPoison.Response{status_code: code}} ->
        Logger.error "AlphaVantage API error: #{code} #{url} - #{inspect resp}"
        message.replyfun.("forex: erreur (api #{code})")
      {:error, %HTTPoison.Error{reason: error}} ->
        Logger.error "AlphaVantage HTTP error: #{inspect error}"
        message.replyfun.("forex: erreur (http #{inspect error})")
    end
    {:noreply, state}
  end

  def handle_info({:irc, :trigger, "currency", message = %{trigger: %{type: :query, args: args = search}}}, state) do
    search = Enum.join(search, " ")
    results = Enum.filter(@currencies, fn({symbol, name}) ->
      String.contains?(String.downcase(name), String.downcase(search)) || String.contains?(String.downcase(symbol), String.downcase(search))
    end)
    |> Enum.map(fn({symbol, name}) ->
      "#{symbol}: #{name}"
    end)
    |> Enum.join(", ")

    if results == "" do
      message.replyfun.("no results!")
    else
      message.replyfun.(results)
    end
    {:noreply, state}
  end

  defp api_key() do
    Application.get_env(:nola, :alphavantage, [])
    |> Keyword.get(:api_key, "demo")
  end

end