Created
September 11, 2011 23:40
-
-
Save fredhsu/1210294 to your computer and use it in GitHub Desktop.
Twitter streaming api to mongodb in clojure
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
(ns twitterstream.core | |
(:require | |
[http.async.client :as client] | |
[org.danlarkin.json :as json]) | |
(:use somnium.congomongo)) | |
(mongo! :db "twitterdb") ; Mongo database called twitterdb | |
(def url "https://stream.twitter.com/1/statuses/sample.json") | |
(def cred {:user "username" :password "password"}) | |
(with-open [client (client/create-client)] ;; Create client | |
(let [resp (client/stream-seq client :get url :auth cred) ; The http response | |
strings (client/string resp)] ; Turn the response into a string | |
(doseq [part strings] ; Since this is a stream, will keep adding new tweets | |
(insert! :tweets (json/decode-from-str part))))) ; insert the tweet into table called tweets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment