| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- (in-package :cl-user)
- (defpackage chatikbot.plugins.google
- (:use :cl :chatikbot.common))
- (in-package :chatikbot.plugins.google)
- (defparameter +google-search-url+ "http://www.google.com/search")
- (defun google-search (query)
- (loop
- for result across (clss:select ".g" (xml-request +google-search-url+
- :parameters (list (cons "q" query)
- (cons "hl" "en"))))
- for a = (ignore-errors (elt (clss:select ".r>a" result) 0))
- for uri = (and a (quri:uri (plump:get-attribute a "href")))
- for q = (and uri (aget "q" (quri:url-decode-params (quri:uri-query uri))))
- when (and q (equal (quri:uri-path uri) "/url"))
- collect (list
- (cons :url q)
- (cons :title (select-text a))
- (cons :desc (select-text result ".st")))))
- (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)
- (replace-all (aget :desc result)
- '(#\Newline) ""))))))
- ;;; Hooks
- (def-message-cmd-handler handler-cmd-google (:google :g :г :q)
- (bot-send-message (google-format-search-results
- (subseq
- (google-search (format nil "~{~A~^ ~}" *args*))
- 0 3))
- :parse-mode "markdown"
- :disable-web-preview 1))
|