-
-
Save KirinDave/e696d410b168ba507046 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 powerset.twitter | |
(:require [clojure.http.resourcefully :as resourcefully] | |
[clojure.contrib.json.read :as json]) | |
(:import [java.text SimpleDateFormat])) | |
(def *twitter-statuses-url* "http://twitter.com/statuses/") | |
(def *twitter-date-format* (SimpleDateFormat. "E MMM d H:m:s Z y")) | |
(def users (ref {})) | |
(defn- json-read [& args] | |
(binding [json/*json-keyword-keys* true] | |
(apply json/read-json args))) | |
(defn- surl | |
([space name kind] (str *twitter-statuses-url* space "/" name "." kind)) | |
([space name] (str *twitter-statuses-url* space "/" name ".json")) | |
([space] (str *twitter-statuses-url* space ".json"))) | |
(defn unique-user! [{id :id :as user}] | |
(dosync | |
(if (not (@users id)) | |
(alter users assoc id user))) | |
(@users id)) | |
(defn- prep-timeline-entry | |
[{user :user, created-at :created_at :as entry}] | |
(assoc entry :user (unique-user! user), | |
:created_at (.parse *twitter-date-format* created-at))) | |
(defn timeline-for-user [username] | |
(let [response (resourcefully/get (surl "user_timeline" username))] | |
(if (= (:code response) | |
200) | |
(map prep-timeline-entry (json-read (first (:body-seq response)))) ; body seq comes back with a sequence | |
[])))- | |
(defn public-timeline [] | |
(let [response (resourcefully/get (surl "public_timeline"))] | |
(if (= (:code response) | |
200) | |
(map prep-timeline-entry (json-read (first (:body-seq response)))) | |
[]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment