Advertisement
Guest User

MarioBot

a guest
Feb 29th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 3.04 KB | None | 0 0
  1.  
  2. #lang racket/base
  3.  
  4. (require net/http-client json net/url racket/port racket/file racket/bytes file/md5)
  5.  
  6. (define server (getenv "SERVER"))
  7. (define token (getenv "TOKEN"))
  8. (define safebooru "safebooru.donmai.us")
  9. (define limit "20")
  10. (define page (number->string (random 1 350)))
  11. (define tags "mario_(series)")
  12. (define pos (random 19))
  13. (define CRLF "\r\n")
  14. (define boundary (bytes->string/utf-8 (md5 (number->string (current-seconds)))))
  15. (define boundary-line (string-append "--" boundary CRLF))
  16. ; a table to map file extensions to MIME types:
  17. (define ext=>mime-type
  18.   #hash(("jpg" . "image/jpeg")
  19.         ("png"  . "image/png")
  20.         ("gif"  . "image/Gif")
  21.         ("swf"  . "application/x-shockwave-flash")
  22.         ("mp4"  . "video/mp4")
  23.         ("webm" . "video/webm")))                                        
  24.                
  25.                                        
  26.  
  27.  
  28. (define (search-safebooru)
  29.   (define-values (status header response)(http-sendrecv safebooru (string-append "/posts.json?limit=" limit "&page=" page "&tags="tags ) #:ssl? #t))
  30.   (define posts (read-json response))
  31.  (list-ref posts pos))
  32.  
  33.  
  34. (define (attach-media)
  35.   (displayln "Obtaining media id")
  36.   (define post (search-safebooru))
  37.   (define url (hash-ref post 'file_url))
  38.   (define md5 (hash-ref post 'md5))
  39.   (define file-ext (hash-ref post 'file_ext))
  40.   (define mime-type (hash-ref ext=>mime-type file-ext))
  41.   (define filename (string-append md5 "." file-ext))
  42.   (displayln (format "Downloading ~a from ~a" filename url))
  43.   (call-with-output-file filename
  44.     (lambda (in) (display (port->bytes (get-pure-port (string->url url)))in))
  45.     #:exists 'replace)
  46.  
  47.  (define data
  48.   (bytes-append
  49.    (string->bytes/utf-8 (string-append boundary-line "Content-Disposition: form-data; name=\"file\"; filename=" "\"" filename "\"" CRLF
  50.                                        "Content-Type: " mime-type CRLF CRLF))
  51.                     (file->bytes filename)
  52.    (string->bytes/utf-8 (string-append CRLF "--" boundary "--" CRLF))))
  53.  (define-values (status headers response)
  54.   (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 data))
  55.   (read-json response))
  56.  
  57.  
  58.  
  59.  
  60. (define (upload-attachment)
  61.   (displayln "Uploading attachment")
  62. (define attachment (attach-media))
  63.   (define id (hash-ref attachment 'id))
  64.    (define data
  65.   (bytes-append
  66.    (string->bytes/utf-8 (string-append boundary-line "Content-Disposition: form-data; name=\"media_ids[]\"" CRLF CRLF))
  67.    (string->bytes/utf-8 id)
  68.    (string->bytes/utf-8 (string-append CRLF "--" boundary "--" CRLF))))
  69.   (define-values (status headers response)
  70.     (http-sendrecv server (string-append "/api/v1/statuses") #:ssl? #t #:method #"POST" #:headers (list (string-append "Content-Type: multipart/form-data; boundary=" boundary) (string-append "Authorization: Bearer " token)) #:data data))
  71.     (displayln status)
  72.   (displayln (read-json response)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement