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

twitter.lisp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. (:eval-when (:compile-toplevel :load-toplevel)
  2. (ql:quickload :chirp))
  3. (defpackage :coleslaw-twitter
  4. (:use :cl)
  5. (:import-from :coleslaw #:*config*
  6. #:deploy
  7. #:get-updated-files
  8. #:find-content-by-path
  9. #:title-of
  10. #:author-of
  11. #:page-url
  12. #:plugin-conf-error)
  13. (:export #:enable))
  14. (in-package :coleslaw-twitter)
  15. (defvar *tweet-format* '(:title "by" :author)
  16. "Controls what the tweet annoucing the post looks like.")
  17. (defvar *tweet-format-fn* nil "Function that expects an instance of
  18. coleslaw:post and returns the tweet content.")
  19. (defvar *tweet-format-dsl-mapping*
  20. '((:title . title-of)
  21. (:author . author-of)))
  22. (define-condition malformed-tweet-format (error)
  23. ((item :initarg :item :reader item))
  24. (:report
  25. (lambda (condition stream)
  26. (format stream "Malformed tweet format. Can't proccess: ~A"
  27. (item condition)))))
  28. (defun compile-tweet-format (tweet-format)
  29. (multiple-value-bind
  30. (fmt-ctrl-str accesors) (%compile-tweet-format tweet-format nil nil)
  31. (let
  32. ((fmt-ctrl-str (format nil "~{~A~^ ~}" (reverse fmt-ctrl-str)))
  33. (accesors (reverse accesors)))
  34. (lambda (post)
  35. (apply #'format nil fmt-ctrl-str
  36. (loop
  37. :for accesor :in accesors
  38. :collect (funcall accesor post)))))))
  39. (defun %compile-tweet-format (tweet-format fmt-ctrl-str accesors)
  40. "Transform tweet-format into a format control string and a list of values."
  41. (if (null tweet-format)
  42. (values fmt-ctrl-str accesors)
  43. (let ((next (car tweet-format)))
  44. (cond
  45. ((keywordp next)
  46. (if (assoc next *tweet-format-dsl-mapping*)
  47. (%compile-tweet-format
  48. (cdr tweet-format)
  49. (cons "~A" fmt-ctrl-str)
  50. (cons (cdr (assoc next *tweet-format-dsl-mapping*))
  51. accesors))
  52. (error 'malformed-tweet-format :item next)))
  53. ((stringp next)
  54. (%compile-tweet-format (cdr tweet-format)
  55. (cons next fmt-ctrl-str)
  56. accesors))
  57. (t (error 'malformed-tweet-format :item next))))))
  58. (setf *tweet-format-fn* (compile-tweet-format *tweet-format*))
  59. (defun enable (&key api-key api-secret access-token access-secret tweet-format)
  60. (if (and api-key api-secret access-token access-secret)
  61. (setf chirp:*oauth-api-key* api-key
  62. chirp:*oauth-api-secret* api-secret
  63. chirp:*oauth-access-token* access-token
  64. chirp:*oauth-access-secret* access-secret)
  65. (error 'plugin-conf-error :plugin "twitter"
  66. :message "Credentials missing."))
  67. ;; fallback to chirp for credential erros
  68. (chirp:account/verify-credentials)
  69. (when tweet-format
  70. (setf *tweet-format* tweet-format)))
  71. (defmethod deploy :after (staging)
  72. (declare (ignore staging))
  73. (loop :for (state file) :in (get-updated-files)
  74. :when (and (string= "A" state) (string= "post" (pathname-type file)))
  75. :do (tweet-new-post file)))
  76. (defun tweet-new-post (file)
  77. "Retrieve content matching FILE from in memory DB and publish it."
  78. (let ((post (find-content-by-path file)))
  79. (chirp:statuses/update (%format-post 0 post))))
  80. (defun %format-post (offset post)
  81. "Guarantee that the tweet content is 140 chars at most. The 117 comes from
  82. the spaxe needed for a space and the url."
  83. (let* ((content-prefix (subseq (render-tweet post) 0 (- 117 offset)))
  84. (content (format nil "~A ~A/~A" content-prefix
  85. (coleslaw::domain *config*)
  86. (page-url post)))
  87. (content-length (chirp:compute-status-length content)))
  88. (cond
  89. ((>= 140 content-length) content)
  90. ((< 140 content-length) (%format-post (1- offset) post)))))
  91. (defun render-tweet (post)
  92. "Sans the url, which is a must."
  93. (funcall *tweet-format-fn* post))