1
0

google.lisp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. (in-package :cl-user)
  2. (defpackage chatikbot.plugins.google
  3. (:use :cl :chatikbot.common))
  4. (in-package :chatikbot.plugins.google)
  5. (defparameter +google-search-url+ "http://www.google.com/search")
  6. (defun google-search (query)
  7. (loop
  8. for result across (clss:select ".g" (xml-request +google-search-url+
  9. :parameters (list (cons "q" query)
  10. (cons "hl" "en"))))
  11. for a = (ignore-errors (elt (clss:select ".r>a" result) 0))
  12. for uri = (and a (quri:uri (plump:get-attribute a "href")))
  13. for q = (and uri (aget "q" (quri:url-decode-params (quri:uri-query uri))))
  14. when (and q (equal (quri:uri-path uri) "/url"))
  15. collect (list
  16. (cons :url q)
  17. (cons :title (select-text a))
  18. (cons :desc (select-text result ".st")))))
  19. (defun google-format-search-results (results)
  20. (format nil "~{~A. ~A~^~%~}"
  21. (loop for i from 1
  22. for result in results
  23. append (list i
  24. (format nil "[~A](~A)~@[~% ~A~]"
  25. (aget :title result)
  26. (aget :url result)
  27. (replace-all (aget :desc result)
  28. '(#\Newline) ""))))))
  29. ;;; Hooks
  30. (def-message-cmd-handler handler-cmd-google (:google :g :г :q)
  31. (bot-send-message (google-format-search-results
  32. (subseq
  33. (google-search (format nil "~{~A~^ ~}" *args*))
  34. 0 3))
  35. :parse-mode "markdown"
  36. :disable-web-preview 1))