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

twitter.lisp 2.1KB

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