Created
January 7, 2012 06:32
-
-
Save valvallow/1573997 to your computer and use it in GitHub Desktop.
twitter-friends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/gosh | |
(use rfc.json) | |
(use rfc.http) | |
(use util.list) | |
;; http://api.twitter.com/1/statuses/friends.json?cursor=-1&id=valvallow | |
(define (usage) | |
(print "Usage: twitter-friends <twitter-id>") | |
(exit 1)) | |
(define (query-compose query) | |
(string-join (map (cut string-join <> "=") query) "&")) | |
(define (twitter-api-get server url params) | |
(receive (status header body) | |
(http-get server | |
(string-append url "." "json" | |
"?" (query-compose params))) | |
(rlet1 r (parse-json-string body) | |
(when (and (not (vector? r)) | |
(string=? "error" (caar r))) | |
(raise "user not found.")) | |
))) | |
(define (twitter-api-friends id :optional (cursor "-1")) | |
(twitter-api-get "api.twitter.com" | |
"/1/statuses/friends" | |
`(("cursor" ,cursor) | |
("id" ,id)))) | |
(define (main args) | |
(if (null? (cdr args)) | |
(usage) | |
(let1 user (cadr args) | |
(let rec ((cursor -1)(count 0)(acc '())) | |
(if (zero? cursor) | |
(for-each print (apply append (reverse acc))) | |
(let* ((friends (twitter-api-friends user (x->string cursor))) | |
(users (vector->list (assoc-ref friends "users"))) | |
(next_cursor (assoc-ref friends "next_cursor")) | |
(friends-name (map (^l (assoc-ref l "screen_name")) | |
users))) | |
(with-output-to-port (current-error-port) | |
(^ _ (display " ") | |
(display count) | |
(display " users ...\r"))) | |
(rec next_cursor | |
(+ count (length friends-name)) | |
(cons friends-name acc)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment