bot that uploads mario artwork to the fediverse every 30 mins
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

83 рядки
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 page (number->string (random 1 350)))
  8. (define tags "mario_(series)%20-bowsette")
  9. (define pos (random 19))
  10. (define CRLF "\r\n")
  11. (define boundary (bytes->string/utf-8 (md5 (number->string (current-seconds)))))
  12. (define boundary-line (string-append "--" boundary CRLF))
  13. ; a table to map file extensions to MIME types:
  14. (define ext=>mime-type
  15. #hash(("jpg" . "image/jpeg")
  16. ("png" . "image/png")
  17. ("gif" . "image/Gif")
  18. ("swf" . "application/x-shockwave-flash")
  19. ("mp4" . "video/mp4")
  20. ("webm" . "video/webm")))
  21. (define (search-safebooru)
  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. (define post (search-safebooru))
  27. (define url (hash-ref post 'file_url))
  28. (define md5 (hash-ref post 'md5))
  29. (define file-ext (hash-ref post 'file_ext))
  30. (define mime-type (hash-ref ext=>mime-type file-ext))
  31. (define filename (string-append md5 "." file-ext))
  32. (displayln (format "Downloading ~a from ~a" filename url))
  33. (call-with-output-file filename
  34. (lambda (in) (display (port->bytes (get-pure-port (string->url url)))in))
  35. #:exists 'replace)
  36. (define data
  37. (bytes-append
  38. (string->bytes/utf-8 (string-append boundary-line "Content-Disposition: form-data; name=\"file\"; filename=" "\"" filename "\"" CRLF
  39. "Content-Type: " mime-type CRLF CRLF))
  40. (file->bytes filename)
  41. (string->bytes/utf-8 (string-append CRLF "--" boundary "--" CRLF))))
  42. (define-values (status headers response)
  43. (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))
  44. (displayln status)
  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. ;sleep for ten minutes
  59. (define (loop)
  60. (sleep 600)
  61. (upload-attachment)
  62. (loop))
  63. (loop)