Преглед изворни кода

[finance] Use new yahoo endpoint

Innocenty Enikeew пре 8 година
родитељ
комит
f3bcfbf220
1 измењених фајлова са 10 додато и 13 уклоњено
  1. 10 13
      plugins/finance.lisp

+ 10 - 13
plugins/finance.lisp

@@ -1,11 +1,9 @@
 (in-package #:chatikbot)
 
-(defparameter +yahoo-url+ "https://query.yahooapis.com/v1/public/yql" "Yahoo Finance API endpoint")
-(defparameter +yahoo-query+ "select Name,Rate from yahoo.finance.xchange where pair in (~{\"~A\"~^,~})")
-
+(defparameter +yahoo-url+ "https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&fields=regularMarketPrice" "Yahoo Finance API endpoint")
 (defparameter +brent-url+ "http://www.cmegroup.com/CmeWS/mvc/Quotes/Future/424/G")
 
-(defvar *rate-pairs* '("USDRUB" "EURRUB" "GBPRUB"))
+(defvar *rate-pairs* '("USDRUB=X" "EURRUB=X" "GBPRUB=X"))
 
 (defparameter +chart-ranges+ (list (cons "day" (* 24 60))
                                    (cons "week" (* 7 24 60))
@@ -14,16 +12,15 @@
                                    (cons "year" (* 365 24 60))))
 
 (defun get-rates (&optional (pairs *rate-pairs*))
-  (let ((response (json-request
-                   +yahoo-url+
-                   :parameters (append '(("format" . "json")
-                                         ("env" . "store://datatables.org/alltableswithkeys"))
-                                       (list (cons "q" (format nil +yahoo-query+ pairs)))))))
+  (let ((response (agets (json-request
+                          +yahoo-url+
+                          :parameters `(("symbols" . ,(format nil "~{~A~^,~}" pairs))))
+                         "quoteResponse")))
     (when (aget "error" response)
       (error "Error in rates request"))
-    (loop for rate in (aget "rate" (aget "results" (aget "query" response)))
-       collect (cons (aget "Name" rate)
-                     (read-from-string (aget "Rate" rate))))))
+    (loop for rate in (agets response "result")
+       collect (cons (or (aget "shortName" rate) (aget "symbol" rate))
+                     (aget "regularMarketPrice" rate)))))
 
 (defun get-brent ()
   (handler-case
@@ -116,7 +113,7 @@
         (rates (get-rates))
         (brent (get-brent))
         (btc (get-coindesk-btc-usd)))
-    (db/add-finance ts (aget "USD/RUB" rates) (aget "EUR/RUB" rates) (aget "GBP/RUB" rates) brent btc)))
+    (db/add-finance ts (aget "USDRUB=X" rates) (aget "EUR/RUB" rates) (aget "GBP/RUB" rates) brent btc)))
 
 ;;; Hooks
 (def-message-cmd-handler handler-rates (:rates)