Last active
November 20, 2017 16:36
-
-
Save dotemacs/21f9f2629132ebb8924384287d178e26 to your computer and use it in GitHub Desktop.
Get Bitcoin price in USD, Lumo script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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