Browse files

Revert to old stock api and add support for data extraction

  • Loading branch information...
devth committed Oct 8, 2017
1 parent 9f97de2 commit b8125f7ff35b6da739da9c86853a6e6271f892f2
Showing with 24 additions and 29 deletions.
  1. +1 −0 doc/
  2. +23 −29 src/yetibot/commands/stock.clj
@@ -3,6 +3,7 @@
## 0.4.13
- Upgrade to `yetibot.core 0.4.13`
- Fix the stock command
## 0.4.12
@@ -2,46 +2,40 @@
[clojure.string :as s]
[yetibot.core.hooks :refer [cmd-hook]]
[clj-http.client :as client]
[cheshire.core :as json]
[clojure.walk :refer [keywordize-keys]]))
[yetibot.core.util.http :refer [get-json]]))
(defn endpoint
"Creates a Google Finance query from stock symbol"
"Creates a YQL query from stock symbol"
(str "" stock-symbol))
(str ""
(defn get-body
"Gets json body from response"
(-> json
(s/replace-first "//" "")
(defn format-percent
"Formats number in map as percent"
[k m]
(update-in m [k] #(format "%.2f%%" (double %))))
(defn parse-quote
"Parses most relevant information from json"
(defn get-price
"Gets the price from a stock symbol via Yahoo API"
(let [response (client/get (endpoint stock-symbol){:throw-exceptions false})]
(if (= (:status response) 200)
(let [{:keys [name l hi lo mc cp el ecp]}(->> (get-body response) first keywordize-keys)]
[(str "Name: " name)
(str "Last Price: " l)
(str "High: " hi)
(str "Low: " lo)
(str "Market Cap: " mc)
(str "Change Percent: " cp"%")
(when (seq el) (str "After Hours Price: " el))
(when (seq ecp) (str "After Hours Change Percent: " ecp "%"))]))
(str "Unable to find symbol for " stock-symbol ". Try another symbol such as MSFT or AAPL."))))
(let [stock-info (get-json (endpoint stock-symbol))]
(if (:Name stock-info)
{:result/data stock-info
(->> stock-info
(format-percent :ChangePercent)
((juxt :Name :LastPrice :High :Low :MarketCap :ChangePercent))
(interleave ["Name:" "Last Price:" "High:" "Low:" "Market Cap:"
"Change Percent:"])
(partition 2)
(map #(s/join " " %)))}
(:Message stock-info))))
(defn stock-cmd
"stock <symbol> # displays current value in market"
{:yb/cat #{:info}}
[{:keys [args]}]
(parse-quote args))
(get-price args))
(cmd-hook ["stock" #"^stock$"]
_ stock-cmd)

0 comments on commit b8125f7

Please sign in to comment.