1
0

foursquare.lisp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. (in-package #:chatikbot)
  2. (defparameter *fsq-checkins-url* "https://api.foursquare.com/v2/checkins/recent"
  3. "URL of recent checkins API")
  4. (defparameter *fsq-api-url* "https://api.foursquare.com/v2/~A"
  5. "Foursquare API URL")
  6. (defvar *fsq-access-token* nil "Access token for a user under which the process is run")
  7. (defun %fsq-api-call (method &optional params)
  8. (let* ((resp
  9. (json-request (format nil *fsq-api-url* method)
  10. :parameters (list*
  11. (cons "oauth_token" *fsq-access-token*)
  12. (cons "v" "20150811")
  13. params)))
  14. (meta (aget "meta" resp)))
  15. (when (not (= 200 (aget "code" meta)))
  16. (error (format nil "Foursquare API error, code ~A, errorType '~A', errorDetail '~A'"
  17. (aget "code" meta) (aget "errorType" meta) (aget "errorDetail" meta))))
  18. (aget "response" resp)))
  19. (defun fsq-fetch-checkins (&optional after-timestamp limit)
  20. (aget "recent"
  21. (%fsq-api-call "checkins/recent"
  22. (list (cons "afterTimestamp" (or after-timestamp "0"))
  23. (cons "limit" (or limit "20"))))))
  24. (defun fsq-fetch-friends (&optional offset)
  25. (list*
  26. (aget "user" (%fsq-api-call "users/self"))
  27. (aget "items"
  28. (aget "friends"
  29. (%fsq-api-call "users/self/friends"
  30. (list (cons "offset" (or offset "0"))))))))
  31. (defun fsq-format-checkin (checkin &optional with-dates)
  32. (when checkin
  33. (let ((user (aget "user" checkin))
  34. (venue (aget "venue" checkin)))
  35. (format nil "📍 ~@[~A~]~@[ ~A~]~@[ в ~A~]~@[ (~A)~]~@[ 📢 ~A~]~:[~; ~A~]"
  36. (aget "firstName" user) (aget "lastName" user)
  37. (aget "name" venue) (first (aget "formattedAddress" (aget "location" venue)))
  38. (aget "shout" checkin)
  39. with-dates
  40. (local-time:format-timestring
  41. nil
  42. (local-time:unix-to-timestamp (aget "createdAt" checkin))
  43. :format '(:year "-" (:month 2) "-" (:day 2) " " (:hour 2) ":" (:min 2)))))))