| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- (in-package #:chatikbot)
- (defparameter *fsq-checkins-url* "https://api.foursquare.com/v2/checkins/recent"
- "URL of recent checkins API")
- (defparameter *fsq-api-url* "https://api.foursquare.com/v2/~A"
- "Foursquare API URL")
- (defvar *fsq-access-token* nil "Access token for a user under which the process is run")
- (defvar *fsq-last-timestamp* nil "Timestamp of the last checkin. To only fetch latest")
- (defun %fsq-api-call (method &optional params)
- (let* ((resp
- (yason:parse
- (flexi-streams:octets-to-string
- (handler-case
- (bordeaux-threads:with-timeout (5)
- (drakma:http-request
- (format nil *fsq-api-url* method)
- :parameters
- (list*
- (cons "oauth_token" *fsq-access-token*)
- (cons "v" "20150811")
- params)))
- (bordeaux-threads:timeout (e)
- (declare (ignore e))
- (error "Timeout")))
- :external-format :utf-8)
- :object-as :alist))
- (meta (aget "meta" resp)))
- (when (not (= 200 (aget "code" meta)))
- (error (format nil "Foursquare API error, code ~A, errorType '~A', errorDetail '~A'"
- (aget "code" meta) (aget "errorType" meta) (aget "errorDetail" meta))))
- (aget "response" resp)))
- (defun fsq-fetch-checkins (&optional after-timestamp limit)
- (aget "recent"
- (%fsq-api-call "checkins/recent"
- (list (cons "afterTimestamp" (or after-timestamp "0"))
- (cons "limit" (or limit "20"))))))
- (defun fsq-fetch-new-checkins ()
- (let ((recent (fsq-fetch-checkins *fsq-last-timestamp*)))
- (when (first recent)
- (setf *fsq-last-timestamp* (princ-to-string (1+ (aget "createdAt" (first recent))))))
- recent))
- (defun fsq-fetch-friends (&optional offset)
- (list*
- (aget "user" (%fsq-api-call "users/self"))
- (aget "items"
- (aget "friends"
- (%fsq-api-call "users/self/friends"
- (list (cons "offset" (or offset "0"))))))))
- (defun fsq-format-checkin (checkin &optional with-dates)
- (when checkin
- (let ((user (aget "user" checkin))
- (venue (aget "venue" checkin)))
- (format nil "📍 ~@[~A~]~@[ ~A~]~@[ в ~A~]~@[ (~A)~]~@[ 📢 ~A~]~:[~; ~A~]"
- (aget "firstName" user) (aget "lastName" user)
- (aget "name" venue) (first (aget "formattedAddress" (aget "location" venue)))
- (aget "shout" checkin)
- with-dates (local-time:format-timestring
- nil
- (local-time:unix-to-timestamp (aget "createdAt" checkin))
- :format '(:year "-" (:month 2) "-" (:day 2) " " (:hour 2) ":" (:min 2)))))))
|