Skip to content

Instantly share code, notes, and snippets.

@stuhood
Created April 14, 2009 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuhood/95473 to your computer and use it in GitHub Desktop.
Save stuhood/95473 to your computer and use it in GitHub Desktop.
(import '(java.io BufferedReader IOException InputStream InputStreamReader OutputStreamWriter)
'(java.net URL URLConnection URLEncoder)
'(sun.misc BASE64Encoder))
(def update-url "https://twitter.com/statuses/update.xml")
(defn creds [username password]
(.trim (.encode (BASE64Encoder.) (.getBytes (str username ":" password)))))
(defn twitter [username password text]
(let [creds (creds username password)
con (doto (.openConnection (URL. update-url))
(.setDoInput true)
(.setDoOutput true)
(.setUseCaches false)
(.setRequestProperty "Authorization" (str "Basic " creds))
(.setRequestProperty "Content-Type" "application/x-www-form-urlencoded")
(.setRequestProperty "User-Agent" "clojurebot"))]
(println (.getRequestProperties con))
(with-open [wrt (OutputStreamWriter. (.getOutputStream con))]
(.write wrt (str "status=" (URLEncoder/encode text "UTF-8")))
(.flush wrt)
(with-open [rdr (BufferedReader. (InputStreamReader. (.getInputStream con)))]
(apply str (line-seq rdr))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment