summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJordan Bracco <href@random.sh>2024-09-28 16:26:52 +0200
committerJordan Bracco <href@random.sh>2024-09-28 16:26:52 +0200
commit5474567f4bcb9e2ea907b45ac9942bce636ee386 (patch)
tree387e912ac89e0a953926e5af455e093c32278be5 /lib
parentuser mention: no message when failed because thats annoying (diff)
finance thing
Diffstat (limited to 'lib')
-rw-r--r--lib/plugins/finance.ex43
1 files changed, 26 insertions, 17 deletions
diff --git a/lib/plugins/finance.ex b/lib/plugins/finance.ex
index 3ecc2fb..b083df8 100644
--- a/lib/plugins/finance.ex
+++ b/lib/plugins/finance.ex
@@ -60,11 +60,17 @@ defmodule Nola.Plugins.Finance do
def handle_info({:irc, :trigger, "stocks", message = %{trigger: %{type: :query, args: args = search}}}, state) do
+ search(search, message)
+ {:noreply, state}
+ end
+
+ defp search(search, message) 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)
+ IO.inspect(data)
if error = Map.get(data, "Error Message") do
Logger.error("AlphaVantage API invalid request #{url} - #{inspect error}")
message.replyfun.("stocks: requête invalide")
@@ -92,7 +98,6 @@ defmodule Nola.Plugins.Finance do
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
@@ -100,22 +105,26 @@ defmodule Nola.Plugins.Finance do
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)
+ IO.inspect(data)
+ case data do
+ %{"Error Message" => error} ->
+ Logger.error("AlphaVantage API invalid request #{url} - #{inspect error}")
+ message.replyfun.("stocks: error: #{error}")
+ %{"Global Quote" => data = %{"01. symbol" => _}} ->
+ 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)
+ _ ->
+ message.replyfun.("stocks: unknown symbol: #{symbol}")
+ search([symbol], message)
end
{:ok, resp = %HTTPoison.Response{status_code: code}} ->
Logger.error "AlphaVantage API error: #{code} #{url} - #{inspect resp}"