Implementation sans test
This commit is contained in:
parent
5e236e6ef1
commit
0be9a17cfa
1 changed files with 45 additions and 0 deletions
45
plugins/twitter.lisp
Normal file
45
plugins/twitter.lisp
Normal file
|
@ -0,0 +1,45 @@
|
|||
(:eval-when (:compile-toplevel :load-toplevel)
|
||||
(ql:quickload :chirp))
|
||||
|
||||
(defpackage :coleslaw-twitter
|
||||
(:use :cl)
|
||||
(:export #:enable))
|
||||
|
||||
(in-package :coleslaw-twitter)
|
||||
|
||||
(defvar *tweet-format* '("~A" (title post) (text content))
|
||||
"Controls what the tweet annoucing the post looks like. It is the made up of a format control string followed with the pertinent variables.")
|
||||
|
||||
(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)))
|
||||
|
||||
(defmethod render :after (post (eql (find-class 'coleslaw:post)))
|
||||
(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)))
|
||||
(content (format nil "~A ~A" content-prefix (coleslaw:page-url post)))
|
||||
(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."
|
||||
(apply #'format (append '(nil) *tweet-format*)))
|
Loading…
Add table
Reference in a new issue