Skip to content

Instantly share code, notes, and snippets.

@fredhsu
Created September 11, 2011 23:40
Show Gist options
  • Save fredhsu/1210294 to your computer and use it in GitHub Desktop.
Save fredhsu/1210294 to your computer and use it in GitHub Desktop.
Twitter streaming api to mongodb in clojure
(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