1
0

foursquare.lisp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. (in-package #:chatikbot)
  2. (defparameter *fsq-checkins-url* "https://api.foursquare.com/v2/checkins/recent"
  3. "URL of recent checkins API")
  4. (defvar *fsq-access-token* nil "Access token for a user under which the process is run")
  5. (defvar *fsq-last-timestamp* nil "Timestamp of the last checkin. To only fetch latest")
  6. (defun fsq-fetch-checkins (&optional after-timestamp)
  7. (let* ((resp
  8. (yason:parse
  9. (flexi-streams:octets-to-string
  10. (handler-case
  11. (bordeaux-threads:with-timeout (5)
  12. (drakma:http-request
  13. *fsq-checkins-url*
  14. :parameters
  15. (list
  16. (cons "oauth_token" *fsq-access-token*)
  17. (cons "afterTimestamp" (or after-timestamp "0"))
  18. (cons "v" "20150811"))))
  19. (bordeaux-threads:timeout (e)
  20. (declare (ignore e))
  21. (error "Timeout")))
  22. :external-format :utf-8)
  23. :object-as :alist))
  24. (meta (aget "meta" resp)))
  25. (when (not (= 200 (aget "code" meta)))
  26. (error (format nil "Foursquare API error, code ~A, errorType '~A', errorDetail '~A'"
  27. (aget "code" meta) (aget "errorType" meta) (aget "errorDetail" meta))))
  28. (aget "recent" (aget "response" resp))))
  29. (defun fsq-fetch-new-checkins ()
  30. (let ((recent (fsq-fetch-checkins *fsq-last-timestamp*)))
  31. (when (first recent)
  32. (setf *fsq-last-timestamp* (princ-to-string (1+ (aget "createdAt" (first recent))))))
  33. recent))
  34. (defun fsq-format-checkin (checkin)
  35. (when checkin
  36. (let ((user (aget "user" checkin))
  37. (venue (aget "venue" checkin)))
  38. (format nil "📍 ~@[~A~]~@[ ~A~]~@[ в ~A~]~@[ (~A)~]~@[ 📢~A~]"
  39. (aget "firstName" user) (aget "lastName" user)
  40. (aget "name" venue) (first (aget "formattedAddress" (aget "location" venue)))
  41. (aget "shout" checkin)))))