http.async.client & Twitter Streaming API
http.async.client is a Clojure library based on Asynchronous Http Client for Java which runs on top of Netty Project.
So much for introduction, lets see some sample code that uses Twitter streaming api.
Importing:
(ns twitter-sample
(:require [http.async.client :as c]
[org.danlarkin.json :as j]))
Settings and helper to print twits as they fly by:
(def u "username")
(def p "password")
(defn print-user-and-text [s]
(let [twit (j/decode-from-str s)
user (:screen_name (:user twit))
text (:text twit)]
(println user "=>" text)))
Lets read statuses/sample:
(doseq [twit-str (c/string
(c/stream-seq :get "http://stream.twitter.com/1/statuses/sample.json"
:auth {:user u :password p}))]
(print-user-and-text twit-str))
Or statuses/filter:
(doseq [twit-str (c/string
(c/stream-seq :post "http://stream.twitter.com/1/statuses/filter.json"
:body {"track" "basketball,football,baseball,footy,soccer"}
:auth {:user u :password p}))]
(print-user-and-text twit-str))
Those two samples show also how to consume HTTP Streams as clojure.core/seq, a new feature that was introduced in v0.2.0.
You can also handle sequences via callbacks.
Requesting HTTP resources that are not streams is even simpler:
(let [response (c/GET "http://github.com/neotyk/http.async.client/")]
(c/await response)
(c/string response))
There is extensive documentation available on the github pages of the project, you can also find Changelog and API.
Big thanks go to Amsterdam Clojurians, this library received so much love and help from this group.
Kudos to Jeanfrancois Arcand for starting async-http-client recently reaching the 1.1.0 release.
I would love to hear from you what you think of this library.