Twitter Update from PLT Scheme

I was contemplating doing some automated twittering from schemep3, possibly when I change albums. [For an automatic update on my progress through the 1,001 albums].

Anyway, I got it to work so here’s the function. No error checking or success fail notification.

#lang scheme

(provide twitter)

(require net/base64)
(require net/url)

(define (strip-crlf b64-bytes)
  (subbytes b64-bytes 0 (- (bytes-length b64-bytes) 2)))

;;; note that this uses the very insecure basic authorization method...
(define (twitter username password status)
  (let* ([basic-auth (strip-crlf (base64-encode (string->bytes/utf-8 (string-append username ":" password))))])
    (let ([url
             (make-path/param "statuses" (list))
             (make-path/param "update.xml" (list)))
             (cons 'status status))
      (let ([port
               (string-append "Authorization: Basic " (bytes->string/utf-8 basic-auth))))])
        (close-input-port port)))))

Leave a Reply

Your email address will not be published. Required fields are marked *