Procházet zdrojové kódy

[finance] use coindesk.com data for btc rate

Innocenty Enikeev před 8 roky
rodič
revize
b0986de4dc
2 změnil soubory, kde provedl 11 přidání a 1 odebrání
  1. 8 1
      plugins/finance.lisp
  2. 3 0
      utils.lisp

+ 8 - 1
plugins/finance.lisp

@@ -39,6 +39,13 @@
       (aget "last" (aget "ticker" (json-request +btc-e-usd-url+)))
     (error (e) (log:error e))))
 
+(defparameter +coindesk-btc-usd-url+ "https://api.coindesk.com/site/headerdata.json?currency=BTC" "coindesk.com api endpoint")
+(defun get-coindesk-btc-usd ()
+  (handler-case
+      (agets (json-request +coindesk-btc-usd-url+)
+             "bpi" "USD" "rate_float")
+    (error (e) (log:error "~A" e))))
+
 (defun get-serie (series idx)
   (loop for row in series
      when (elt row idx)
@@ -108,7 +115,7 @@
   (let ((ts (local-time:timestamp-to-unix (local-time:now)))
         (rates (get-rates))
         (brent (get-brent))
-        (btc (get-btc-e)))
+        (btc (get-coindesk-btc-usd)))
     (db/add-finance ts (aget "USD/RUB" rates) (aget "EUR/RUB" rates) (aget "GBP/RUB" rates) brent btc)))
 
 ;;; Hooks

+ 3 - 0
utils.lisp

@@ -77,6 +77,9 @@ is replaced with replacement."
 (defmacro aget (key alist)
   `(cdr (assoc ,key ,alist :test #'equal)))
 
+(defun agets (alist &rest keys)
+  (reduce #'(lambda (a k) (aget k a)) keys :initial-value alist))
+
 (defun mappend (fn &rest lists)
   "Apply fn to each element of lists and append the results."
   (apply #'append (apply #'mapcar fn lists)))