Innokenty Enikeev 10 anos atrás
pai
commit
545f561210
3 arquivos alterados com 21 adições e 17 exclusões
  1. 8 11
      chatikbot.lisp
  2. 7 6
      db.lisp
  3. 6 0
      finance.lisp

+ 8 - 11
chatikbot.lisp

@@ -180,20 +180,17 @@
   (handler-case
       (let ((ts (local-time:timestamp-to-unix (local-time:now)))
             (rates (get-rates))
-            (brent (get-brent)))
-        (db-add-finance ts
-                        (aget "USD/RUB" rates)
-                        (aget "EUR/RUB" rates)
-                        (aget "GBP/RUB" rates)
-                        brent))
+            (brent (get-brent))
+            (btc (get-btc-e)))
+        (db-add-finance ts (aget "USD/RUB" rates) (aget "EUR/RUB" rates) (aget "GBP/RUB" rates) brent btc))
     (error (e) (log:error e))))
 
 (defun handle-cmd-rates (chat-id message-id args)
   (log:info "handle-cmd-rates" chat-id message-id args)
-  (multiple-value-bind (ts usd eur gbp brent) (db-get-last-finance)
+  (multiple-value-bind (ts usd eur gbp brent btc) (db-get-last-finance)
     (telegram-send-message chat-id
-                           (format nil "Зеленый ~,2F, гейро ~,2F, британец ~,2F, чёрная ~,2F @ ~A"
-                                   usd eur gbp brent
+                           (format nil "Зеленый ~,2F, гейро ~,2F, британец ~,2F, чёрная ~,2F, btc ~,2F @ ~A"
+                                   usd eur gbp brent btc
                                    (format-ts (local-time:unix-to-timestamp ts))))))
 
 (defparameter +chart-ranges+ (list (cons "day" (* 24 60))
@@ -222,8 +219,8 @@
                                          (db-get-series after-ts fields avg)))))
         (telegram-send-photo chat-id chart
                              :caption
-                             (format nil "Зеленый ~,2F, гейро ~,2F, британец ~,2F, чёрная ~,2F @ ~A"
-                                     (elt rates 1) (elt rates 2) (elt rates 3) (elt rates 4)
+                             (format nil "Зеленый ~,2F, гейро ~,2F, британец ~,2F, чёрная ~,2F, btc ~,2F @ ~A"
+                                     (elt rates 1) (elt rates 2) (elt rates 3) (elt rates 4) (elt rates 5)
                                      (format-ts (local-time:unix-to-timestamp (elt rates 0))))))
     (error (e)
       (log:error e)

+ 7 - 6
db.lisp

@@ -15,7 +15,7 @@
 (defun db-init ()
   (with-db (db)
     ;; Finance
-    (sqlite:execute-non-query db "create table if not exists finance (ts, usd, eur, gbp, brent)")
+    (sqlite:execute-non-query db "create table if not exists finance (ts, usd, eur, gbp, brent, btc)")
     (sqlite:execute-non-query db "create index if not exists fin_ts_ids on finance (ts)")
 
     ;; Foursquare
@@ -36,20 +36,21 @@
     (sqlite:execute-non-query db "create index if not exists rss_chat_feeds_feed_idx on rss_chat_feeds (feed_id)")))
 
 ;; Finance
-(defun db-add-finance (ts usd eur gbp brent)
+(defun db-add-finance (ts usd eur gbp brent btc)
   (with-db (db)
-    (sqlite:execute-non-query db "insert into finance (ts, usd, eur, gbp, brent) values (?, ?, ?, ?, ?)"
-                              ts usd eur gbp brent)))
+    (sqlite:execute-non-query db "insert into finance (ts, usd, eur, gbp, brent, btc) values (?, ?, ?, ?, ?, ?)"
+                              ts usd eur gbp brent btc)))
 
 (defun db-get-last-finance ()
   (with-db (db)
-    (sqlite:execute-one-row-m-v db "select ts, usd, eur, gbp, brent from finance order by ts desc limit 1")))
+    (sqlite:execute-one-row-m-v db "select ts, usd, eur, gbp, brent, btc from finance order by ts desc limit 1")))
 
 
 (defparameter +db-finance-map+ '(("usd" . "USD/RUB")
                                  ("eur" . "EUR/RUB")
                                  ("gbp" . "GBP/RUB")
-                                 ("brent" . "Brent")))
+                                 ("brent" . "Brent")
+                                 ("btc" . "BTC/USD")))
 
 (defun db-get-series (after-ts &optional (fields '("usd" "eur" "gbp" "brent")) (avg 60))
   (when fields

+ 6 - 0
finance.lisp

@@ -27,6 +27,12 @@
           last))
     (error (e) (log:error e))))
 
+(defparameter +btc-e-usd-url+ "https://btc-e.com/api/2/btc_usd/ticker" "BTC-e BTC/USD ticker url")
+(defun get-btc-e ()
+  (handler-case
+      (aget "last" (aget "ticker" (json-request +btc-e-usd-url+)))
+    (error (e) (log:error e))))
+
 (defun get-serie (series idx)
   (loop for row in series
      when (elt row idx)