(in-package :cl-user) (defpackage chatikbot.plugins.tescort (:use :cl :chatikbot.common :chatikbot.scraping :eager-future2)) (in-package :chatikbot.plugins.tescort) (defvar *chat-cookie-jars* (make-hash-table)) (defparameter +blacklists+ '((:tescort (:scrape (:request . ("http://www.tescort.com/panel/client-blacklist" :parameters (("client_criterias" . q) ("search" . "search")))) (:items . ".black_list_table tbody tr") (:info (:date . ("td:nth-of-type(1)")) (:name . ("td:nth-of-type(2)")) (:city . ("td:nth-of-type(3)")) (:phone . ("td:nth-of-type(4)")) (:comment . ("td:nth-of-type(5)")))) (:init . (("http://www.tescort.com/private/signin") ".signin-box form" (("username" . tescort-login) ("password" . tescort-pass)))) (:validate . "#header .sitemenu-logged")) (:annonce (:scrape (:request . ("https://www.6annonce.com/private-v2/client-blacklist" :parameters (("client_criterias" . q) ("search" . "search")))) (:items . ".black_list_table tbody tr") (:info (:date . ("td:nth-of-type(1)")) (:name . ("td:nth-of-type(2)")) (:city . ("td:nth-of-type(3)")) (:phone . ("td:nth-of-type(4)")) (:comment . ("td:nth-of-type(5)")) (:photos . ("td:nth-of-type(5) a" :attr :href :multiple t)))) (:init . (("https://www.6annonce.com/private/signin") ".signin-box form" (("username" . annonce-login) ("password" . annonce-pass)))) (:validate . "#header .sitemenu-logged")))) (defvar *tescort-login*) (defvar *tescort-pass*) (defvar *annonce-login*) (defvar *annonce-pass*) (defun get-common-context () `((tescort-login . ,*tescort-login*) (tescort-pass . ,*tescort-pass*) (annonce-login . ,*annonce-login*) (annonce-pass . ,*annonce-pass*))) (defun search-blacklists (query) (let ((context (append (get-common-context) `((q . ,query))))) (labels ((f (info) (destructuring-bind (name . params) info (cons name (ignore-errors (scrape-list params context)))))) (pmapcar #'f +blacklists+)))) (defun merge-results (results) (loop for (name . res) in results append res)) (defun format-blacklist (item) (format nil "~A, ~A: ~A *~A*~%~A~@[~%~A~]" (agets item :date) (agets item :city) (agets item :name) (agets item :phone) (agets item :comment) (when (agets item :photos) (format nil "~{[pic~A](~A)~^, ~}" (loop for i from 1 for p in (agets item :photos) append (list i p)))))) (defun handle-blacklist (chat-id query) (with-chat-cookies (chat-id *chat-cookie-jars*) (telegram-send-chat-action chat-id "typing") (let ((results (merge-results (search-blacklists query)))) (bot-send-message chat-id (if results (text-chunks (mapcar #'format-blacklist results) :pre-pre "" :pre-post "") "Not found") :parse-mode "markdown")))) (def-message-cmd-handler handler-cmd-blacklist (:blacklist :bl) (cond ((null args) (bot-send-message chat-id "Enter query") (on-next-message chat-id (handle-blacklist chat-id text))) (:otherwise (handle-blacklist chat-id (spaced args)))))