Skip to content

Instantly share code, notes, and snippets.

@hiredman
Created April 14, 2009 20:12
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 hiredman/95395 to your computer and use it in GitHub Desktop.
Save hiredman/95395 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"))]
(with-open [wrt (OutputStreamWriter. (.getOutputStream con))
rdr (BufferedReader. (InputStreamReader. (.getInputStream con)))]
(.write wrt (str "status=" (URLEncoder/encode text "UTF-8")))
(apply str (line-seq rdr)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment