Flexible Lisp Blogware. Fork for personal use. Mirrored from https://github.com/kingcons/coleslaw originally.

twitter.lisp 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. (:eval-when (:compile-toplevel :load-toplevel)
  2. (ql:quickload :chirp))
  3. (defpackage :coleslaw-twitter
  4. (:use :cl)
  5. (:export #:enable))
  6. (in-package :coleslaw-twitter)
  7. (defvar *tweet-format* '("~A" (title post) (text content))
  8. "Controls what the tweet annoucing the post looks like. It is the made up of a format control string followed with the pertinent variables.")
  9. (defun enable (&key api-key api-secret access-token access-secret tweet-format)
  10. (if (and api-key api-secret access-token access-secret)
  11. (setf chirp:*oauth-api-key* api-key
  12. chirp:*oauth-api-secret* api-secret
  13. chirp:*oauth-access-token* access-token
  14. chirp:*oauth-access-secret* access-secret)
  15. (error 'plugin-conf-error :plugin "twitter"
  16. :message "Credentials missing.")
  17. ;; fallback to chirp for credential erros
  18. (chirp:account/verify-credentials))
  19. (when tweet-format
  20. (setf *tweet-format* tweet-format)))
  21. (defmethod render :after (post (eql (find-class 'coleslaw:post)))
  22. (format-post post))
  23. (defun format-post (post)
  24. "Take a post and return a string of 140 character length, at most. Urls have 23 len and are a must."
  25. (chirp:statuses/update (%format-post post)))
  26. (defun %format-post (offset post)
  27. "Garauntee that the tweet content is 140 chars at most."
  28. (let* ((content-prefix (subseq (render-tweet post) 0 (- 117 offset)))
  29. (content (format nil "~A ~A" content-prefix (coleslaw:page-url post)))
  30. (content-length (chirp:compute-status-length content)))
  31. (cond
  32. ((>= 140 content-length) content)
  33. ((< 140 content-length) (%format-post (1- offset) post)))))
  34. (defun render-tweet (post)
  35. "Sans the url, which is a must."
  36. (apply #'format (append '(nil) *tweet-format*)))