1
0
فهرست منبع

Google search

Innocenty Enikeew 9 سال پیش
والد
کامیت
9072d99267
4فایلهای تغییر یافته به همراه42 افزوده شده و 2 حذف شده
  1. 1 0
      chatikbot.asd
  2. 16 0
      chatikbot.lisp
  3. 21 0
      google.lisp
  4. 4 2
      utils.lisp

+ 1 - 0
chatikbot.asd

@@ -32,4 +32,5 @@
                (:file "foursquare")
                (:file "rss")
                (:file "nalunch")
+               (:file "google")
                (:file "chatikbot")))

+ 16 - 0
chatikbot.lisp

@@ -80,6 +80,8 @@
               (:lastrss (handle-cmd-last-rss chat-id id args))
               (:wall (handle-cmd-wall chat-id id args))
               (:nalunch (handle-cmd-nalunch chat-id id args))
+              (:g (handle-cmd-google chat-id id args))
+              (:г (handle-cmd-google chat-id id args))
               (otherwise (handle-admin-cmd from-id chat-id text cmd args))))
           (send-dont-understand chat-id (preprocess-input text))))
     (when location
@@ -588,6 +590,20 @@
           (setf *nalunch-last-result* result)))
     (error (e) (log:error "~A" e))))
 
+(defun handle-cmd-google (chat-id message-id args)
+  (log:info "handle-cmd-google" chat-id message-id args)
+  (handler-case
+      (telegram-send-message chat-id
+                             (google-format-search-results
+                              (subseq
+                               (google-search (format nil "~{~A~^ ~}" args))
+                               0 3))
+                             :parse-mode "markdown"
+                             :disable-web-preview 1)
+    (error (e)
+           (log:error "~A" e)
+           (telegram-send-message chat-id (format nil "~A" e)))))
+
 (defun process-watchdog ()
   (ignore-errors
     (close

+ 21 - 0
google.lisp

@@ -0,0 +1,21 @@
+(in-package #:chatikbot)
+
+(defparameter +google-search-url+ "https://www.google.com/search")
+
+(defun google-search (query)
+  (loop for result across (clss:select "ol > div" (xml-request +google-search-url+
+                                                               :parameters (list (cons "q" query))))
+     collect (list
+              (cons :url (select-text "cite" result))
+              (cons :title (select-text "h3" result))
+              (cons :desc (select-text ".st" result)))))
+
+(defun google-format-search-results (results)
+  (format nil "~{~A. ~A~^~%~}"
+          (loop for i from 1
+             for result in results
+             append (list i
+                          (format nil "[~A](~A)~@[~%   ~A~]"
+                                  (aget :title result)
+                                  (aget :url result)
+                                  (substitute #\Space #\Newline (aget :desc result)))))))

+ 4 - 2
utils.lisp

@@ -17,7 +17,7 @@
              (sleep backoff)
              (setf backoff (min *backoff-max*
                                 (* 2 backoff))))
-	   (bordeaux-threads:timeout (e)
+           (bordeaux-threads:timeout (e)
              (log:error e)
              (log:info "Backing off for" backoff)
              (sleep backoff)
@@ -92,9 +92,11 @@ is replaced with replacement."
      nil)))
 
 ;; XML processing
-(defun xml-request (url &optional encoding)
+(defun xml-request (url &key encoding parameters)
   (multiple-value-bind (raw-body status headers uri http-stream)
       (drakma:http-request (http-default url)
+                           :parameters parameters
+                           :external-format-out :utf-8
                            :force-binary t
                            :decode-content t)
     (declare (ignore status http-stream))