PasteRack.org
Paste # 59344
2020-02-13 10:20:47

Fork as a new paste.

Paste viewed 61 times.


Embed:

marioBot

#lang racket

(require net/http-client json net/url racket/port web-server/private/timer racket/file racket/bytes file/md5)

(define server (getenv "SERVER"))
(define token (getenv "ACCESS_TOKEN"))
(define safebooru "safebooru.donmai.us")
(define limit "20")
(define page (number->string (random 1 350)))
(define tags "mario_(series)")
(define pos (random 20))
(define CRLF "\r\n")
(define boundary (bytes->string/utf-8 (md5 (number->string (current-seconds)))))


(define (search-safebooru)
  (define-values (status header response)(http-sendrecv safebooru (string-append "/posts.json?limit=" limit "&page=" page "&tags="tags ) #:ssl? #t))
  (define posts (read-json response))
 (list-ref posts pos))
  

(define (download-image)
  (define post (search-safebooru))
  (define url (hash-ref post 'file_url))
  (define md5 (hash-ref post 'md5))
  (define file-ext (hash-ref post 'file_ext))
  (define filename (string-append md5 "." file-ext))
  (call-with-output-file filename
                        (lambda (in) (display (port->bytes (get-pure-port (string->url url)))in))
    #:exists 'replace)
  (define image (file->bytes filename))
(define data
   (string-append
      CRLF "--" boundary CRLF "Content-Disposition: form-data; name=\"file\"; filename=" (string-append "\"" filename "\"") CRLF (string-append "Content-Type: image/" (if (eq? file-ext "jpg") "jpeg" "png")  CRLF))) image (string-append CRLF "--" boundary "--" CRLF))
  


(define (upload-image)
  (define-values (status headers response)
    (http-sendrecv server (string-append "/api/v1/media") #:ssl? #t #:method #"POST"  #:headers (list (string-append "Content-Type: multipart/form-data; boundary=" boundary)(string-append "Authorization: Bearer " token))  #:data (download-image)))
  (displayln (read-json response))
  (displayln status))
 


(upload-image)