gist/racket/fedi/upload.rkt

37 lines
1.5 KiB
Racket

;; String String Bytes -> String
;; Uploads some media, returns the media ID
(define (upload-media filename mime-type data-bytes)
(define MULTIPART-BOUNDARY "hashlanguwu")
(define s->b string->bytes/utf-8)
;; aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
(define multipart-data
(bytes-append
#"--" (s->b MULTIPART-BOUNDARY) #"\r\n"
#"Content-Disposition: form-data; name=\"file\"; filename=\"" (s->b filename) #"\"\r\n"
#"Content-Type: " (s->b mime-type) #"\r\n"
#"\r\n"
data-bytes
#"\r\n"
#"--" (s->b MULTIPART-BOUNDARY) #"--\r\n"))
;; send it
(define-values (status headers rsp)
(http-sendrecv/url (make-api-url '("media"))
#:method "POST"
#:headers (list
(make-header "Authorization" (get-authorization))
(make-header "Content-Type"
(string-append "multipart/form-data; boundary="
MULTIPART-BOUNDARY)))
#:data multipart-data))
(hash-ref (string->jsexpr (port->string rsp)) 'id))
(define (bot-run)
(define thank (file->bytes "thank.png"))
(define thank-id (upload-media "thank.png" "image/png" thank))
(define toot (make-toot "test post pls ignore"
#:cw "bot post, no caption"
#:media (list thank-id)))
(displayln toot)
(send-toot toot)
(void))