bot that uploads mario artwork to the fediverse every 30 mins
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

83 Zeilen
3.0KB

  1. #lang racket/base
  2. (require net/http-client json net/url racket/port racket/file racket/bytes file/md5)
  3. (define server (getenv "SERVER"))
  4. (define token (getenv "TOKEN"))
  5. (define safebooru "safebooru.donmai.us")
  6. (define limit "20")
  7. (define tags "mario_(series)")
  8. (define CRLF "\r\n")
  9. (define boundary (bytes->string/utf-8 (md5 (number->string (current-seconds)))))
  10. (define boundary-line (string-append "--" boundary CRLF))
  11. ; a table to map file extensions to MIME types:
  12. (define ext=>mime-type
  13. #hash(("jpg" . "image/jpeg")
  14. ("png" . "image/png")
  15. ("gif" . "image/Gif")
  16. ("swf" . "application/x-shockwave-flash")
  17. ("mp4" . "video/mp4")
  18. ("webm" . "video/webm")))
  19. (define (search-safebooru)
  20. (define pos (random 19))
  21. (define page (number->string (random 1 350)))
  22. (define-values (status header response)(http-sendrecv safebooru (string-append "/posts.json?limit=" limit "&page=" page "&tags="tags ) #:ssl? #t))
  23. (define posts (read-json response))
  24. (list-ref posts pos))
  25. (define (attach-media)
  26. (displayln "Obtaining media id")
  27. (define post (search-safebooru))
  28. (define url (hash-ref post 'file_url))
  29. (define md5 (hash-ref post 'md5))
  30. (define file-ext (hash-ref post 'file_ext))
  31. (define mime-type (hash-ref ext=>mime-type file-ext))
  32. (define filename (string-append md5 "." file-ext))
  33. (displayln (format "Downloading ~a from ~a" filename url))
  34. (call-with-output-file filename
  35. (lambda (in) (display (port->bytes (get-pure-port (string->url url)))in))
  36. #:exists 'replace)
  37. (define data
  38. (bytes-append
  39. (string->bytes/utf-8 (string-append boundary-line "Content-Disposition: form-data; name=\"file\"; filename=" "\"" filename "\"" CRLF
  40. "Content-Type: " mime-type CRLF CRLF))
  41. (file->bytes filename)
  42. (string->bytes/utf-8 (string-append CRLF "--" boundary "--" CRLF))))
  43. (define-values (status headers response)
  44. (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))
  45. (read-json response))
  46. (define (upload-attachment)
  47. (define attachment (attach-media))
  48. (define id (hash-ref attachment 'id))
  49. (define data
  50. (bytes-append
  51. (string->bytes/utf-8 (string-append boundary-line "Content-Disposition: form-data; name=\"media_ids[]\"" CRLF CRLF))
  52. (string->bytes/utf-8 id)
  53. (string->bytes/utf-8 (string-append CRLF "--" boundary "--" CRLF))))
  54. (define-values (status headers response)
  55. (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))
  56. (displayln status)
  57. (displayln (read-json response))
  58. (displayln "Uploaded post!"))
  59. (define (loop)
  60. (sleep 480)
  61. (upload-attachment)
  62. (loop))
  63. (loop)