فهرست منبع

Use streams instead of strings for json parsing

Innocenty Enikeew 10 سال پیش
والد
کامیت
a643c3de4b
7فایلهای تغییر یافته به همراه66 افزوده شده و 73 حذف شده
  1. 0 1
      chatikbot.asd
  2. 11 8
      finance.lisp
  3. 11 12
      forecast.lisp
  4. 8 6
      foursquare.lisp
  5. 6 3
      rss.lisp
  6. 11 27
      telegram.lisp
  7. 19 16
      vk.lisp

+ 0 - 1
chatikbot.asd

@@ -8,7 +8,6 @@
                #:bordeaux-threads
                #:cl-date-time-parser
                #:clon
-               #:dexador
                #:drakma
                #:flexi-streams
                #:local-time

+ 11 - 8
finance.lisp

@@ -9,13 +9,15 @@
 
 (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)))
+                   (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)))
     (when (aget "error" response)
       (error "Error in rates request"))
     (loop for rate in (aget "rate" (aget "results" (aget "query" response)))
@@ -27,8 +29,9 @@
       (let ((last (read-from-string
                    (aget "last" (first (aget "quotes"
                                              (yason:parse
-                                              (flexi-streams:octets-to-string
+                                              (flexi-streams:make-flexi-stream
                                                (drakma:http-request +brent-url+
+                                                                    :want-stream t
                                                                     :force-binary t
                                                                     :decode-content t)
                                                :external-format :utf-8)

+ 11 - 12
forecast.lisp

@@ -6,18 +6,17 @@
 (defun forecast (lat lon &key time (currently t) minutely hourly daily alerts)
   (handler-case
       (bordeaux-threads:with-timeout (5)
-	(yason:parse
-	 (flexi-streams:octets-to-string
-	  (drakma:http-request (format
-				nil
-				"~A/~A/~A,~A~@[,~A~]?units=si&exclude=~:[currently,~;~]~:[minutely,~;~]~:[hourly,~;~]~:[daily,~;~]~:[alerts,~;~]flags&lang=ru"
-				+forecast-api-url+ *forecast-api-key* lat lon time
-				currently minutely hourly daily alerts))
-	  :external-format :utf8)
-	 :object-as :alist))
-    (bordeaux-threads:timeout (e)
-      (declare (ignore e))
-      (error "Timeout"))))
+        (yason:parse
+         (flexi-streams:make-flexi-stream
+          (drakma:http-request
+           (format nil
+                   "~A/~A/~A,~A~@[,~A~]?units=si&exclude=~:[currently,~;~]~:[minutely,~;~]~:[hourly,~;~]~:[daily,~;~]~:[alerts,~;~]flags&lang=ru"
+                   +forecast-api-url+ *forecast-api-key* lat lon time
+                   currently minutely hourly daily alerts)
+           :force-binary t :want-stream t :decode-content t)
+          :external-format :utf8)
+         :object-as :alist))
+    (bordeaux-threads:timeout () (error "Timeout"))))
 
 (defvar *forecast-point-formats*
   '((:current . (:year "-" (:month 2) "-" (:day 2) " " (:hour 2) ":" (:min 2)))

+ 8 - 6
foursquare.lisp

@@ -11,16 +11,18 @@
 (defun %fsq-api-call (method &optional params)
   (let* ((resp
           (yason:parse
-           (flexi-streams:octets-to-string
+           (flex:make-flexi-stream
             (handler-case
                 (bordeaux-threads:with-timeout (5)
                   (drakma:http-request
                    (format nil *fsq-api-url* method)
-                   :parameters                   
-                   (list*
-                    (cons "oauth_token" *fsq-access-token*)
-                    (cons "v" "20150811")
-                    params)))
+                   :parameters (list*
+                                (cons "oauth_token" *fsq-access-token*)
+                                (cons "v" "20150811")
+                                params)
+                   :force-binary t
+                   :want-stream t
+                   :decode-content t))
               (bordeaux-threads:timeout (e)
                 (declare (ignore e))
                 (error "Timeout")))

+ 6 - 3
rss.lisp

@@ -19,11 +19,14 @@
   (nreverse (plump::get-elements-by-tag-name node tag)))
 
 (defun url-parse (url)
-  (multiple-value-bind (body status headers uri stream)
-      (drakma:http-request (http-default url) :force-binary t :decode-content t)
+  (multiple-value-bind (body-stream status headers uri stream)
+      (drakma:http-request (http-default url)
+                           :force-binary t
+                           :want-stream t
+                           :decode-content t)
     (declare (ignore status headers stream))
     (values
-     (plump:parse (flexi-streams:octets-to-string body :external-format :utf-8))
+     (plump:parse (flex:make-flexi-stream body-stream :external-format :utf-8))
      uri)))
 
 (defun find-rss-links (url)

+ 11 - 27
telegram.lisp

@@ -4,22 +4,6 @@
 (defparameter +telegram-api-format+ "https://api.telegram.org/bot~A/~A")
 (defvar *telegram-timeout* 30 "Default Telegram timeout")
 
-;; (defun %telegram-api-call (method &optional args)
-;;   (let* ((params (loop for (k . v) in args collect (cons
-;;                                                     (princ-to-string k)
-;;                                                     (princ-to-string v))))
-;;          (response (yason:parse
-;;                    (flexi-streams:octets-to-string
-;;                     (drakma:http-request (format nil +telegram-api-format+ *telegram-token* method)
-;;                                          :method :post
-;;                                          :parameters params
-;;                                          :external-format-out :utf8)
-;;                     :external-format :utf8)
-;;                    :object-as :alist)))
-;;     (unless (aget "ok" response)
-;;       (error (aget "description" response)))
-;;     (aget "result" response)))
-
 (defun %telegram-api-call (method &optional args)
   (let* ((params (loop for (k . v) in args collect (cons
                                                     (princ-to-string k)
@@ -28,17 +12,17 @@
          (timeout (+ 5 (or (cdr (assoc :timeout args))
                            *telegram-timeout*)))
          (response (yason:parse
-                    (flexi-streams:octets-to-string
-		     (handler-case
-			 (bordeaux-threads:with-timeout (timeout)
-			   (dex:post (format nil +telegram-api-format+ *telegram-token* method)
-				     :content params
-				     :use-connection-pool t
-				     :force-binary t))
-		       (bordeaux-threads:timeout (e)
-			 (declare (ignore e))
-			 (error "Timeout")))
-		       :external-format :utf8)
+                    (flexi-streams:make-flexi-stream
+                     (handler-case
+                         (bordeaux-threads:with-timeout (timeout)
+                           (drakma:http-request (format nil +telegram-api-format+
+                                                        *telegram-token* method)
+                                                :method :post
+                                                :parameters params
+                                                :external-format-out :utf8
+                                                :want-stream t :force-binary t :decode-content t))
+                       (bordeaux-threads:timeout () (error "Timeout")))
+                     :external-format :utf8)
                     :object-as :alist)))
     (unless (aget "ok" response)
       (error (aget "description" response)))

+ 19 - 16
vk.lisp

@@ -5,22 +5,25 @@
 (defun %vk-api-call (method &optional args)
   (handler-case
       (bordeaux-threads:with-timeout (5)
-	(let* ((params (loop for (k . v) in args
-			  when v
-			  collect (cons
-				   (princ-to-string k)
-				   (princ-to-string v))))
-	       (response (yason:parse
-			  (flexi-streams:octets-to-string
-			   (drakma:http-request (format nil +vk-api-url+ method)
-						:method :post
-						:parameters params
-						:external-format-out :utf8)
-			   :external-format :utf8)
-			  :object-as :alist)))
-	  (when (aget "error" response)
-	    (error (aget "error_msg" (aget "error" response))))
-	  (aget "response" response)))
+        (let* ((params (loop for (k . v) in args
+                          when v
+                          collect (cons
+                                   (princ-to-string k)
+                                   (princ-to-string v))))
+               (response (yason:parse
+                          (flex:make-flexi-stream
+                           (drakma:http-request (format nil +vk-api-url+ method)
+                                                :method :post
+                                                :parameters params
+                                                :external-format-out :utf-8
+                                                :force-binary t
+                                                :want-stream t
+                                                :decode-content t)
+                           :external-format :utf-8)
+                          :object-as :alist)))
+          (when (aget "error" response)
+            (error (aget "error_msg" (aget "error" response))))
+          (aget "response" response)))
     (bordeaux-threads:timeout (e)
       (declare (ignore e))
       (error "Timeout"))))