Sfoglia il codice sorgente

move patmatch to common. return tumblr

Innocenty Enikeew 9 anni fa
parent
commit
f8aa582cc0
5 ha cambiato i file con 57 aggiunte e 95 eliminazioni
  1. 1 0
      chatikbot.asd
  2. 0 0
      patmatch.lisp
  3. 2 41
      plugins/huiza.lisp
  4. 18 4
      plugins/tumblr.lisp
  5. 36 50
      utils.lisp

+ 1 - 0
chatikbot.asd

@@ -23,6 +23,7 @@
                #:yason)
   :serial t
   :components ((:file "package")
+               (:file "patmatch")
                (:file "utils")
                (:file "db")
                (:file "telegram")

+ 0 - 0
plugins/patmatch.lisp → patmatch.lisp


+ 2 - 41
plugins/huiza.lisp

@@ -1,8 +1,5 @@
 (in-package #:chatikbot)
 
-;; Load pattern matching facility
-(load "plugins/patmatch.lisp")
-
 (defparameter *fuck-off*
   '((отъебись) (мне похуй) (ебаный ты нахуй!))
   "Fuck-off responses")
@@ -11,7 +8,7 @@
 (defun prep-p (i) (member i '(в на)))
 (defun dirty-p (i) (member i '(лох хуй мудак ебалай пидор сука уебок мудило еблан говнюк уебище ебантяй пидрила)))
 
-(defparameter *eliza-rules*
+(defparameter *huiza-rules*
   `(
     (((?is ?g goto-p) (?is ?prep prep-p) (?* ?х))
      (а самому не пойти ?prep ?х ?) (мамку свою посылай ?prep ?х) (покомандуй тут еще) ,@*fuck-off*)
@@ -46,44 +43,8 @@
      (:sticker . "BQADAgADEgADOoERAAFtU3uF9HvtQgI") ;; Бухнём?
      (:sticker . "BQADBAADQAEAAnscSQABqWydSKTnASoC"))))
 
-
-(defun punctuation-p (char)
-  (find char ".,;:'!?#-()\\\""))
-
-(defun read-from-string-no-punct (input)
-  "Read from an input string, ignoring punctuation."
-  (let ((*package* (find-package 'chatikbot)))
-    (read-from-string
-     (concatenate 'string "(" (substitute-if #\space #'punctuation-p input) ")"))))
-
-(defun print-with-spaces (list)
-  (format nil "~@(~{~a~^ ~}~)" list))
-
-(defun switch-viewpoint (words)
-  "Change I to you and vice versa, and so on."
-  (sublis '((I . you) (you . I) (me . you) (am . are)
-            (я ты) (ты я) (меня тебя) (тебя меня))
-          words))
-
-(defun use-eliza-rules (input)
-  "Find some rule with which to transform the input."
-  (rule-based-translator input *eliza-rules*
-                         :action #'(lambda (bindings responses)
-                                     (sublis (switch-viewpoint bindings)
-                                             (random-elt responses)))))
-
-(defun eliza (input)
-  (let ((r (use-eliza-rules
-            (read-from-string-no-punct input))))
-    (cond
-      ((null r) nil)
-      ((and (consp (car r)) (eq 'function (caar r)))
-       (apply (cadar r) (cdr r)))
-      ((keywordp (car r)) r)
-      (t (print-with-spaces (flatten r))))))
-
 (defun send-dont-understand (chat-id &optional text reply-id)
-  (let ((resp (eliza text)))
+  (let ((resp (eliza text *huiza-rules*)))
     (log:info text resp)
     (when resp
       (send-response chat-id resp reply-id))))

+ 18 - 4
plugins/tumblr.lisp

@@ -6,8 +6,6 @@
 (defparameter *boobs-roll*
   '("http://bbt12.tumblr.com"
     "http://boobsarethegreatest.tumblr.com"
-    "http://neverending-boobs.tumblr.com"
-    "http://titsandeyes.tumblr.com"
     "http://perfectbreasts.tumblr.com")
   "Tumblr roll with boobs")
 
@@ -16,6 +14,17 @@
     "http://greatestassonearth.tumblr.com")
   "Tumblr roll with asses")
 
+(defparameter *tumblr-rules*
+  `((((?* ?a) (?or сиськи сисяндры титьки буфера boobs tits) (?is ?n numberp))
+     (#'tumblr-random-photo ,*boobs-roll* ?n))
+    (((?* ?a) (?or сиськи сисяндры титьки буфера boobs tits) (?* ?b))
+     (#'tumblr-random-photo ,*boobs-roll*))
+    (((?* ?a) (?or жопа жопы ягодицы зад зады ass asses) (?is ?n numberp))
+     (#'tumblr-random-photo ,*ass-roll* ?n))
+    (((?* ?a) (?or жопа жопы ягодицы зад зады ass asses) (?* ?b))
+     (#'tumblr-random-photo ,*ass-roll*))
+    (((?* ?a) (?or тёлка телка телку тёлку баба бабу сука суку сучка сучку babe bitch) (?* ?b))
+     (#'tumblr-random-photo ,*ass-roll*) (#'tumblr-random-photo ,*boobs-roll*))))
 
 (defun lo-string (val)
   (string-downcase (princ-to-string val)))
@@ -39,11 +48,16 @@
   (when roll
     (let* ((domain (random-elt roll))
            (total (aget "posts-total" (tumblr-read domain :num 1 :type type)))
-	   (start (random total))
+           (start (random total))
            (res (tumblr-read domain :num num :type type :start start)))
       (log:info "Post" start "from" domain)
       (loop for post in (aget "posts" res)
-	   collect (aget "photo-url-1280" post)))))
+         collect (aget "photo-url-1280" post)))))
 
 (defun tumblr-random-photo (&optional (roll *tumblr-roll*) (num 1))
   (tumblr-random-post :roll roll :type :photo :num num))
+
+(def-message-handler handle-tumblr (message)
+  (alexandria:when-let ((resp (eliza (preprocess-input text) *tumblr-rules*)))
+    (log:info resp)
+    (send-response chat-id resp)))

+ 36 - 50
utils.lisp

@@ -98,6 +98,42 @@ is replaced with replacement."
           (preprocess-input (subseq text (1+ first-space)))
           (replace-all text *bot-name* "ты")))))
 
+(defun punctuation-p (char)
+  (find char ".,;:'!?#-()\\\""))
+
+(defun read-from-string-no-punct (input)
+  "Read from an input string, ignoring punctuation."
+  (let ((*package* (find-package 'chatikbot)))
+    (read-from-string
+     (concatenate 'string "(" (substitute-if #\space #'punctuation-p input) ")"))))
+
+(defun print-with-spaces (list)
+  (format nil "~@(~{~a~^ ~}~)" list))
+
+(defun switch-viewpoint (words)
+  "Change I to you and vice versa, and so on."
+  (sublis '((I . you) (you . I) (me . you) (am . are)
+            (я ты) (ты я) (меня тебя) (тебя меня))
+          words))
+
+(defun use-eliza-rules (input rules)
+  "Find some rule with which to transform the input."
+  (rule-based-translator input rules
+                         :action #'(lambda (bindings responses)
+                                     (sublis (switch-viewpoint bindings)
+                                             (random-elt responses)))))
+
+(defun eliza (input rules)
+  (let ((r (use-eliza-rules
+            (read-from-string-no-punct input)
+            rules)))
+    (cond
+      ((null r) nil)
+      ((and (consp (car r)) (eq 'function (caar r)))
+       (apply (cadar r) (cdr r)))
+      ((keywordp (car r)) r)
+      (t (print-with-spaces (flatten r))))))
+
 (defun parse-cmd (text)
   (let* ((args (split-sequence:split-sequence #\Space (subseq text 1) :remove-empty-subseqs t))
          (cmd (subseq (car args) 0 (position #\@ (car args)))))
@@ -201,56 +237,6 @@ is replaced with replacement."
     ((< seconds (* 60 60 24 7 54)) (format nil "~A weeks" (round seconds (* 60 60 24 7))))
     (:otherwise (format nil "~A years" (smart-f (/ seconds (* 60 60 24 365.25)) 1)))))
 
-(defun google-tts (text &key (lang "en"))
-  (let ((path #P"google_tts.mp3"))
-    (with-open-file (s path :direction :output
-                       :element-type '(unsigned-byte 8)
-                       :if-exists :supersede
-                       :if-does-not-exist :create)
-      (write-sequence
-       (drakma:http-request
-        "http://translate.google.com/translate_tts"
-        :parameters `(("ie" . "UTF-8")
-                      ("client" . "t")
-                      ("tl" . ,lang)
-                      ("q" . ,text))
-        :user-agent "stagefright/1.2 (Linux;Android 5.0)"
-        :additional-headers '((:referer . "http://translate.google.com/"))
-        :external-format-out :utf-8
-        :force-binary t)
-       s)
-      path)))
-
-(defun say-it (lang words)
-  (cons :voice
-        (google-tts (print-with-spaces words) :lang lang)))
-
-(defun yit-info ()
-  (labels ((get-rows (url)
-             (rest (get-by-tag (plump:get-element-by-id (xml-request url) "apartmentList") "tr")))
-           (row-data (row)
-             (mapcar (lambda (e) (string-trim '(#\Newline #\Space) (plump:text e)))
-                     (get-by-tag row "td")))
-           (format-data (data)
-             (format nil "~{~A~^ ~}" (mapcar (lambda (n) (nth n data)) '(1 2 3 4 7 6))))
-           (get-intresting (rows)
-             (loop for row in rows
-                for data = (row-data row)
-                for rooms = (parse-integer (nth 2 data))
-                for area = (parse-float:parse-float (replace-all (nth 3 data) "," "."))
-                when (= rooms 3)
-                when (< 65 area 75)
-                collect data))
-           (format-apts (url)
-             (let ((apts (get-intresting (get-rows url))))
-               (format nil "~A~%~{~A~^~%~}~%~A/~A" url (mapcar #'format-data apts)
-                       (length (remove "забронировано" apts :test #'equal :key #'(lambda (r) (nth 7 r)) ))
-                       (length apts)))))
-    (format nil "~{~A~^~%~%~}"
-            (mapcar #'format-apts
-                    '("http://www.yitspb.ru/yit_spb/catalog/apartments/novoorlovskiy-korpus-1-1-1"
-                      "http://www.yitspb.ru/yit_spb/catalog/apartments/novoorlovskiy-korpus-1-1-2")))))
-
 (defmacro def-message-handler (name (message) &body body)
   `(progn
      (defun ,name (,message)