bot that uploads mario artwork to the fediverse every 30 mins
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

85 linhas
3.1KB

  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)%20-bowsette")
  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 hashtable mapping file-extensions and their 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. (displayln attachment)
  49. (define id (hash-ref attachment 'id))
  50. (define data
  51. (bytes-append
  52. (string->bytes/utf-8 (string-append boundary-line "Content-Disposition: form-data; name=\"media_ids[]\"" CRLF CRLF))
  53. (string->bytes/utf-8 id)
  54. (string->bytes/utf-8 (string-append CRLF "--" boundary "--" CRLF))))
  55. (define-values (status headers response)
  56. (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))
  57. (displayln status)
  58. (displayln (read-json response))
  59. (displayln "Uploaded post!"))
  60. ;run upload-attachment very 30 mins infinitely
  61. (define (loop)
  62. (sleep 1740)
  63. (upload-attachment)
  64. (loop))
  65. (loop)