Skip to content

Instantly share code, notes, and snippets.

@fanannan
Created July 11, 2014 03:06
Show Gist options
  • Save fanannan/b0c15c931a23e78da9ad to your computer and use it in GitHub Desktop.
Save fanannan/b0c15c931a23e78da9ad to your computer and use it in GitHub Desktop.
Search tweets without logging in Twitter
(require '[clojure.data.json :as json])
(require '[clj-http.client :as client])
(def url "https://twitter.com/i/search/timeline")
(defn build-params [query, current_time, cursor]
{:f "realtime",
:include_available_feature 1,
:include_entities 1,
:last_note_ts current_time,
:q query,
:scroll_cursor cursor,
:src "typd"})
(defn search [query, cursor]
(let [current_time (int (/ (.. System currentTimeMillis) 1000))
response (client/get url {:query-params (build-params query current_time cursor)})
json-str (:body response)]
(json/read-str json-str)))
(defn twitter-search [query]
(loop [cursor ""
last-cursor nil
results []]
(let [json-map (search query cursor)
next_cursor (get json-map "scroll_cursor")]
(if (or (not (get json-map "has_more_items"))(= cursor last-cursor))
results
(recur next_cursor
cursor
(conj results (get json-map "items_html")))))))
(defn -main [& args]
; the output is a vector of html strings.
; to extract the tweets you need to parse the strings.
(println (twitter-search "#tokyoclj")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment