|
|
@@ -0,0 +1,21 @@
|
|
|
+(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\"~^,~})")
|
|
|
+
|
|
|
+(defvar *rate-pairs* '("USDRUB" "EURRUB"))
|
|
|
+
|
|
|
+(defun get-rates (&optional (pairs *rate-pairs*))
|
|
|
+ (let ((response (yason:parse
|
|
|
+ (flexi-streams:octets-to-string
|
|
|
+ (drakma:http-request +yahoo-url+
|
|
|
+ :parameters (append '(("format" . "json")
|
|
|
+ ("env" . "store://datatables.org/alltableswithkeys"))
|
|
|
+ (list (cons "q" (format nil +yahoo-query+ pairs)))))
|
|
|
+ :external-format :utf-8)
|
|
|
+ :object-as :alist)))
|
|
|
+ (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)
|
|
|
+ (aget "Rate" rate)))))
|