summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Okay <cbeok@protonmail.com>2020-04-14 22:25:49 -0500
committerColin Okay <cbeok@protonmail.com>2020-04-14 22:25:49 -0500
commitdb7dbb33b0cc73aeea9e6212479ad32af89290d5 (patch)
tree738edd50d805a329c23cf1dd9c3b084eab0bc68e
parent3f99fe15eb87c43de9c7df6514413d67099658f0 (diff)
support for some tenor links
-rw-r--r--posterbot.asd2
-rw-r--r--posterbot.lisp29
2 files changed, 27 insertions, 4 deletions
diff --git a/posterbot.asd b/posterbot.asd
index 942c399..0d243c2 100644
--- a/posterbot.asd
+++ b/posterbot.asd
@@ -6,6 +6,6 @@
:license "AGPL"
:version "0.0.1"
:serial t
- :depends-on (#:granolin #:cl-ppcre #:alexandria #:cl-fad #:drakma)
+ :depends-on (#:granolin #:cl-ppcre #:alexandria #:cl-fad #:drakma #:plump #:clss)
:components ((:file "package")
(:file "posterbot")))
diff --git a/posterbot.lisp b/posterbot.lisp
index 0af7d07..aee341c 100644
--- a/posterbot.lisp
+++ b/posterbot.lisp
@@ -72,6 +72,10 @@
(ppcre:create-scanner "https://giphy.com/gifs/([a-z0-9]+-)*([a-zA-Z0-9]+)"
:case-insensitive-mode t))
+(defparameter +tenor-link-regex+
+ (ppcre:create-scanner "https://tenor.com/view/.+"
+ :case-insensitive-mode t))
+
(defun download-link (link)
"Downloads the file at LINK to a temporary file. Returns the path to
the downloaded file. If there is an error thrown at any point, returns NIL."
@@ -81,9 +85,10 @@ the downloaded file. If there is an error thrown at any point, returns NIL."
(file-stream (drakma:http-request link :want-stream t)))
(loop :for bytes = (read-sequence buffer file-stream)
:while (plusp bytes) :do (write-sequence buffer out))))
- (error (c) (progn
- (format *error-output* "While downloading image file at ~a.~%Encountered Error: ~a~%~%" link c)
- nil))))
+ (error (c)
+ (format *error-output*
+ "While downloading image file at ~a.~%Encountered Error: ~a~%~%"
+ link c))))
(defun filename-from-link (link)
@@ -99,16 +104,34 @@ the downloaded file. If there is an error thrown at any point, returns NIL."
((equal type "svg") "svg+xml")
(t type)))))
+(defun fetch-link-from-tenor-page (page-uri)
+ "Scrapes an image link from a the HTML served up by tenor."
+ (handler-case
+ (let* ((dom (plump:parse (drakma:http-request page-uri)))
+ (elems (clss:select "#single-gif-container .Gif > img" dom)))
+ (and (plusp (length elems))
+ (plump:attribute (elt elems 0) "src")
+ (first (ppcre:split "\\\?" (plump:attribute (elt elems 0) "src")))))
+ (error (e)
+ (format *error-output*
+ "Error while processing tenor link ~a~%Error: ~a~%~%"
+ page-uri
+ e))))
(defun check-word-for-link (word)
(cond ((ppcre:scan-to-strings +image-link-regex+ word)
(ppcre:scan-to-strings +image-link-regex+ word))
+
((ppcre:scan-to-strings +giphy-link-regex+ word)
(multiple-value-bind (string matches)
(ppcre:scan-to-strings +giphy-link-regex+ word)
(declare (ignore string))
(when (plusp (length matches))
(format nil "https://media.giphy.com/media/~a/giphy.gif" (elt matches 1)))))
+
+ ((ppcre:scan-to-strings +tenor-link-regex+ word)
+ (fetch-link-from-tenor-page word))
+
(t nil)))
(defun handle-link-candiate (word)