adamwynne/twitter-api — Issue #4: How to collect tweets from a stream into a lazy seq for further processing?
Author: joachimdb
Created: 2012-01-10 16:02:42+00:00
State: open

Hi, me again.

I've made some progress, but I am stuck again and I am also not sure if I am on the right track.

I wrote some clojure code for launching and shutting down a twitter statusses-filter with given credentials, track-string and handler function as follows:

(defn bodypart-text
[response baos]
(:text (json/read-json (.toString baos) true)))

(def statusses-filter (atom nil))

(defn lounch-statusses-filter
"Launches a twitter statusses-filter and pipes the body-part :text
fields to a handler function."
[credentials track-string handler]
(if @statusses-filter
(println "It appears that statusses-filter is already running?")
(reset! statusses-filter
(statuses-filter
:params {:track track-string}
:oauth-creds credentials
:callbacks (AsyncStreamingCallback.
(comp handler bodypart-text)
(comp println response-return-everything)
exception-print)))))

(defn shutdown-statusses-filter []
((:cancel (meta @statusses-filter)))
(reset! statusses-filter nil))

Here are my questions:

1) The reason why I define the atom statusses-filter is because I presume that I need the return value of the statusses-filter for shutting it down, is that correct?

2) With appropriately initialized *creds*, I can now print out all tweets about, say, Obama as follows:

(twitter/lounch-statusses-filter +creds+ "Obama" println)

This works fine, but it is not clear to me how to provide another handlers besides println. For instance, I would like to have a handler that packs the incoming tweets in a (lazy) sequence for further handling, so that I can map over them as they come in, e.g. (I'm improvising here):

(defn mappin-handler [fn]
#(map fn (to-seq %)))

(twitter/lounch-statusses-filter +creds+ "Obama" (mapping-handler println))

Does this make sense at all?

Thanks in advance!
Joachim.