Przeglądaj źródła

Fix streams closure. Refactor finance

Innokenty Enikev 10 lat temu
rodzic
commit
f6ce001784
2 zmienionych plików z 14 dodań i 27 usunięć
  1. 6 19
      finance.lisp
  2. 8 8
      utils.lisp

+ 6 - 19
finance.lisp

@@ -8,16 +8,11 @@
 (defvar *rate-pairs* '("USDRUB" "EURRUB" "GBPRUB"))
 (defvar *rate-pairs* '("USDRUB" "EURRUB" "GBPRUB"))
 
 
 (defun get-rates (&optional (pairs *rate-pairs*))
 (defun get-rates (&optional (pairs *rate-pairs*))
-  (let ((response (yason:parse
-                   (flexi-streams:make-flexi-stream
-                    (drakma:http-request
-                     +yahoo-url+
-                     :parameters (append '(("format" . "json")
-                                           ("env" . "store://datatables.org/alltableswithkeys"))
-                                         (list (cons "q" (format nil +yahoo-query+ pairs))))
-                     :force-binary t :want-stream t :decode-content t)
-                    :external-format :utf-8)
-                   :object-as :alist)))
+  (let ((response (json-request
+                   +yahoo-url+
+                   :parameters (append '(("format" . "json")
+                                         ("env" . "store://datatables.org/alltableswithkeys"))
+                                       (list (cons "q" (format nil +yahoo-query+ pairs)))))))
     (when (aget "error" response)
     (when (aget "error" response)
       (error "Error in rates request"))
       (error "Error in rates request"))
     (loop for rate in (aget "rate" (aget "results" (aget "query" response)))
     (loop for rate in (aget "rate" (aget "results" (aget "query" response)))
@@ -27,15 +22,7 @@
 (defun get-brent ()
 (defun get-brent ()
   (handler-case
   (handler-case
       (let ((last (read-from-string
       (let ((last (read-from-string
-                   (aget "last" (first (aget "quotes"
-                                             (yason:parse
-                                              (flexi-streams:make-flexi-stream
-                                               (drakma:http-request +brent-url+
-                                                                    :want-stream t
-                                                                    :force-binary t
-                                                                    :decode-content t)
-                                               :external-format :utf-8)
-                                              :object-as :alist)))))))
+                   (aget "last" (first (aget "quotes" (json-request  +brent-url+)))))))
         (when (numberp last)
         (when (numberp last)
           last))
           last))
     (error (e) (log:error e))))
     (error (e) (log:error e))))

+ 8 - 8
utils.lisp

@@ -93,16 +93,16 @@ is replaced with replacement."
 
 
 ;; XML processing
 ;; XML processing
 (defun xml-request (url)
 (defun xml-request (url)
-  (multiple-value-bind (http-stream status headers uri stream)
+  (multiple-value-bind (stream status headers uri http-stream)
       (drakma:http-request (http-default url)
       (drakma:http-request (http-default url)
                            :force-binary t
                            :force-binary t
                            :want-stream t
                            :want-stream t
                            :decode-content t)
                            :decode-content t)
-    (declare (ignore status headers stream))
+    (declare (ignore status headers))
     (unwind-protect
     (unwind-protect
          (progn
          (progn
-           (setf (flex:flexi-stream-external-format http-stream) :utf-8)
-           (values (plump:parse http-stream) uri))
+           (setf (flex:flexi-stream-external-format stream) :utf-8)
+           (values (plump:parse stream) uri))
       (ignore-errors (close http-stream)))))
       (ignore-errors (close http-stream)))))
 
 
 (defun get-by-tag (node tag)
 (defun get-by-tag (node tag)
@@ -110,15 +110,15 @@ is replaced with replacement."
 
 
 ;; JSON processing
 ;; JSON processing
 (defun json-request (url &key (method :get) parameters (object-as :alist))
 (defun json-request (url &key (method :get) parameters (object-as :alist))
-  (multiple-value-bind (http-stream status headers uri stream)
+  (multiple-value-bind (stream status headers uri http-stream)
       (drakma:http-request (http-default url) :method method :parameters parameters
       (drakma:http-request (http-default url) :method method :parameters parameters
                              :external-format-out :utf-8
                              :external-format-out :utf-8
                              :force-binary t :want-stream t :decode-content t)
                              :force-binary t :want-stream t :decode-content t)
-    (declare (ignore status headers stream))
+    (declare (ignore status headers))
     (unwind-protect
     (unwind-protect
          (progn
          (progn
-           (setf (flex:flexi-stream-external-format http-stream) :utf-8)
-           (values (yason:parse http-stream :object-as object-as) uri))
+           (setf (flex:flexi-stream-external-format stream) :utf-8)
+           (values (yason:parse stream :object-as object-as) uri))
       (ignore-errors (close http-stream)))))
       (ignore-errors (close http-stream)))))
 
 
 ;; Fix bug in local-time (following symlinks in /usr/share/zoneinfo/
 ;; Fix bug in local-time (following symlinks in /usr/share/zoneinfo/