coleslaw/plugins/twitter.lisp

55 lines
2.1 KiB
Common Lisp
Raw Normal View History

2014-05-07 01:37:27 -05:00
(:eval-when (:compile-toplevel :load-toplevel)
(ql:quickload :chirp))
(defpackage :coleslaw-twitter
(:use :cl)
2014-05-07 13:53:59 -05:00
(:import-from :coleslaw
:*config*
:publish)
2014-05-07 01:37:27 -05:00
(:export #:enable))
(in-package :coleslaw-twitter)
2014-05-07 04:18:00 -05:00
(defvar *tweet-format* '("~A by ~A" coleslaw::post-title coleslaw::post-author)
"Controls what the tweet annoucing the post looks like. It contains a format
control string followed with the accesors to evaluate for post.")
2014-05-07 01:37:27 -05:00
(defun enable (&key api-key api-secret access-token access-secret tweet-format)
(if (and api-key api-secret access-token access-secret)
(setf chirp:*oauth-api-key* api-key
chirp:*oauth-api-secret* api-secret
chirp:*oauth-access-token* access-token
chirp:*oauth-access-secret* access-secret)
(error 'plugin-conf-error :plugin "twitter"
:message "Credentials missing.")
;; fallback to chirp for credential erros
(chirp:account/verify-credentials))
(when tweet-format
(setf *tweet-format* tweet-format)))
2014-05-07 13:53:59 -05:00
(defmethod publish :after (post (eql (find-class 'coleslaw:post)))
2014-05-07 01:37:27 -05:00
(format-post post))
(defun format-post (post)
"Take a post and return a string of 140 character length, at most. Urls have 23 len and are a must."
(chirp:statuses/update (%format-post post)))
(defun %format-post (offset post)
"Garauntee that the tweet content is 140 chars at most."
(let* ((content-prefix (subseq (render-tweet post) 0 (- 117 offset)))
2014-05-07 07:52:29 -05:00
(content (format nil "~A ~A/~A" content-prefix
(coleslaw::domain *config*)
(coleslaw:page-url post)))
2014-05-07 01:37:27 -05:00
(content-length (chirp:compute-status-length content)))
(cond
((>= 140 content-length) content)
((< 140 content-length) (%format-post (1- offset) post)))))
(defun render-tweet (post)
"Sans the url, which is a must."
2014-05-07 04:18:00 -05:00
(apply #'format `(nil ,(car *tweet-format*)
,@(loop
:for accesor in (cdr *tweet-format*)
:collect (funcall accesor post)))))