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-url
            "http"
            #f
            "twitter.com"
            80
            #t
            (list
             (make-path/param "statuses" (list))
             (make-path/param "update.xml" (list)))
            (list
             (cons 'status status))
            #f)])
      (let ([port
             (post-impure-port
              url
              #""
              (list
               (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 *