Commit 27089c60 authored by ieslick's avatar ieslick

New repl api from cl-twit

darcs-hash:2bc75d47c4e9cbe90721e889ae6cdc711ad38ac8
parent 3e7a6547
......@@ -5,6 +5,7 @@
(defstruct (twitter-command (:conc-name "COMMAND-"))
name method return-type base-url description argmap)
(defmethod print-object ((cmd twitter-command) stream)
(format stream "#<TWITTER-CMD '~A'>" (command-name cmd)))
......@@ -49,15 +50,17 @@
(command-description v)))
*command-map*))
(defun command-help (command-name)
(defun command-help (&optional command-name)
"Interactive printing of command arguments"
(let* ((command (get-command command-name)))
(if command
(progn
(format t "~A~%~A~%~%Arguments:~%" command-name (command-description command))
(mapcar #'argument-help (command-argmap command))
t)
(format t "Command ~A not found~%" command-name))))
(if (null command-name)
(list-commands)
(let* ((command (get-command command-name)))
(if command
(progn
(format t "~A~%~A~%~%Arguments:~%" command-name (command-description command))
(mapcar #'argument-help (command-argmap command))
t)
(format t "Command ~A not found~%" command-name)))))
(defun argument-help (argument)
(format t " ~A: ~A~%" (car argument) (cdr argument)))
......
......@@ -82,7 +82,8 @@
(defun parse-element-record (type rec embedded unique-p)
"Generic parsing function"
(let* ((lisprec (twitter->lisp-alist rec))
(existing (funcall (lookup-fn-name type) lisprec)))
(existing (when (fboundp (lookup-fn-name type))
(funcall (lookup-fn-name type) lisprec))))
(if existing
(prog1 existing
(unless unique-p ;; maintains eq, but refreshes values
......@@ -329,9 +330,7 @@
(defun print-search-results (result)
(loop for ref in (search-results result) do
(format t "~A: ~A~%"
(search-ref-from-user ref)
(search-ref-text ref))))
(print-search-ref ref)))
(define-element search-ref ()
......@@ -342,6 +341,7 @@
(to-user-id "ID of receiver" nil)
(from-user "Screen name" nil)
(from-user-id "ID of sender" nil)
(source "Source of the result" nil)
(created-at "The date of message creation" nil)
(iso-language-code "Two character language code for content" nil)
(profile-image-url "The profile image of the sender"))
......@@ -352,5 +352,10 @@
(defun lookup-search-ref (rec)
(declare (ignore rec)))
(defun print-search-ref (ref)
(format t "~A: ~A~%"
(search-ref-from-user ref)
(search-ref-text ref)))
(defmethod register-twitter-object ((ref search-ref))
(declare (ignore ref)))
......@@ -8,11 +8,19 @@
#:authenticate-user
#:authenticated-user
#:*twitter-user*
#:send-tweet
#:latest-tweets
;; Updates
#:timeline
#:user-timeline
#:@replies
#:search
#:trends
;; Actions
#:update
#:reply-to
#:@reply-to
;; I/O
#:print-tweets
#:twitter-search
#:twitter-trends
#:get-tinyurl
;; Twitter command operations
#:twitter-op
......
......@@ -16,12 +16,12 @@
(multiple-value-bind (method url auth post-params)
(command-request-arguments command args)
(multiple-value-bind (response code)
(http-request url
:method method
:basic-authorization (or auth (user-http-auth *twitter-user*))
:parameters (plist->alist post-params)
:want-stream t
:additional-headers *twitter-client-headers*)
(apply #'http-request url
:method method
:basic-authorization (or auth (user-http-auth *twitter-user*))
:parameters (plist->alist post-params)
:want-stream t
:additional-headers *twitter-client-headers*)
(values (safe-decode-json response) code))))
(defun safe-decode-json (response)
......@@ -56,15 +56,59 @@
(defun authenticated-user ()
*twitter-user*)
(defun send-tweet (text &key (tiny-url-p t))
;;
;; Updates
;;
(defun timeline (&rest args)
(apply 'twitter-op :public-timeline args))
(defun user-timeline (&rest args)
(apply 'twitter-op :user-timeline args))
(defun @replies (&rest args)
(apply 'twitter-op :messages-received args))
(defun send-tweet (text &rest args &key (tiny-url-p t) &allow-other-keys)
(let ((newtext (if tiny-url-p (convert-to-tinyurl text) text)))
(if (< (length newtext) 139)
(twitter-op :tweet-update :status)
(apply 'twitter-op :tweet-update :status newtext
(rem-keywords args '(:tiny-url-p)))
(error "Tweet updates must be less than 140 characters"))))
(defun update (text &rest args)
(apply 'send-tweet text args))
(defun reply-to (status-ref text)
(send-tweet text :in-reply-to-status-id message
(defun @reply-to (status-ref text)
(let ((fmt (format nil "@~A ~A"
(user-screen-name (status-user (find-status id)))
text)))
(reply-to message text)))
(defun latest-tweets ()
(twitter-op :user-timeline))
;;
;; Messages
;;
(defun messages ()
(twitter-op :messages-received))
(defun sent-messages ()
(twitter-op :messages-sent))
(defun send-message (user message)
(twitter-op :message-new
:user (aif (get-user user)
(twitter-user-id it)
user)
:text message))
;;
;; Objects
;;
(defun print-tweets (tweets)
(dolist (tweet tweets)
......@@ -75,12 +119,12 @@
;; Search API
;;
(defun twitter-search (query &rest args)
(defun do-search (query &rest args)
(let ((result (apply 'twitter-op :search :q query args)))
(values (search-results result) result)))
(defun twitter-trends ()
(twitter-op :trends))
(defun trends (&rest args)
(apply 'twitter-op :trends args))
;;
;; Tiny URL API
......@@ -96,8 +140,8 @@
"Get a TinyURL for the given URL. Uses the TinyURL API service.
(c) by Chaitanaya Gupta via cl-twit"
(multiple-value-bind (body status-code)
(drakma:http-request *tinyurl-url*
:parameters `(("url" . ,url)))
(http-request *tinyurl-url*
:parameters `(("url" . ,url)))
(if (= status-code +http-ok+)
body
(error 'http-error
......
......@@ -52,6 +52,12 @@
(defun convert-to-twitter (string)
(intern (string-upcase (substitute #\_ #\- (copy-seq string))) :keyword))
(defun rem-keywords (list keywords)
"Remove keywords from a keylist"
(loop for (value indicator) on list by #'cddr
unless (member value keywords)
nconc (list value indicator)))
;;
;; Property list utilities
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment