Skip to content

Instantly share code, notes, and snippets.

@dotemacs
Last active November 20, 2017 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotemacs/21f9f2629132ebb8924384287d178e26 to your computer and use it in GitHub Desktop.
Save dotemacs/21f9f2629132ebb8924384287d178e26 to your computer and use it in GitHub Desktop.
Get Bitcoin price in USD, Lumo script
#!/usr/bin/env lumo
(ns btc.core
(:require [clojure.string :as str]
[goog.string :as gstring]
[goog.string.format]))
(.on js/process "uncaughtException" #(js/console.error %))
(def http (js/require "http"))
(defn parse-price
[data]
"Extract the price of Bitcoin in USD from `data`"
(-> data
js/JSON.parse
(js->clj :keywordize-keys true)
(get-in [:bpi :USD :rate])
(str/replace #"," "")
((fn [f] (print "BTC:" (gstring/format "%.2f" f))))))
(defn get-btc
"Get the Bitcoin price, from `host` & `path`"
[host path]
(let [request (.get http #js{:host host
:port 80
:method "GET"
:path path}
(fn [response]
(let [body (atom "")]
(-> response
(.on "data" #(swap! body str (.toString %)))
(.on "end" #(parse-price @body))))))]
(.on request "error"
(fn [err] (print "an error " err "occured with:" (str/join "" [host path]))))))
(get-btc "api.coindesk.com" "/v1/bpi/currentprice.json")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment