Created
June 12, 2015 16:38
-
-
Save zarkone/d9a3d84ce0b37746ec15 to your computer and use it in GitHub Desktop.
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 lightusers.core | |
(:require [clojure.data.json :as json] | |
[clojure.pprint :refer [pprint]])) | |
(def json-pprint (comp pprint json/write-str)) | |
(def input (json/read-str (slurp "example.json"))) | |
(defn extract | |
([tweet paths] (extract tweet paths [])) | |
([tweet paths users] | |
(if (empty? paths) | |
{:light-users-tweet tweet :extracted-users users} | |
(let [path (first paths) tail (next paths)] | |
(if-let [user (get-in tweet path)] | |
(extract | |
(update-in | |
tweet path select-keys ["name" "id_str"]) | |
tail (conj users user)) | |
(extract tweet tail users)))))) | |
(def paths [["user"] | |
["retweeted_status" "user"] | |
["retweeted_status" "quoted_status" "user"] | |
["quoted_status" "user"]]) | |
(let [output (extract input paths)] | |
(println "EXTRACTED USERS:") | |
(map json-pprint (:extracted-users output)) | |
(println "WHOLE TWEET WITH LITE USERS:") | |
(json-pprint (:light-users-tweet output))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment