initial commit

This commit is contained in:
Brent Gordon 2020-01-25 20:52:55 -05:00
commit 50dd25a597
2 changed files with 51 additions and 0 deletions

47
bot.rkt Normal file
View File

@ -0,0 +1,47 @@
#lang racket/base
(require net/http-client json net/url racket/port web-server/private/timer)
(define server (getenv "SERVER"))
(define token(getenv "ACCESS_TOKEN"))
(define safebooru "safebooru.donmai.us")
(define limit "20")
(define page "2")
(define tags "mario_(series)")
(define pos (random 20))
(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) (port->bytes (get-pure-port (string->url url))))
#:exists 'replace))
(define (upload-image)
(define-values (status headers response)
(http-sendrecv server (string-append "/api/v1/media") #:ssl? #t #:method "POST" #:headers (list (string-append "Authorization: Bearer " token)) #:data (download-image)))
(define data (read-json response))
(displayln data))
(upload-image)
;define tm (start-timer-manager))
;define (loop)
;(start-timer tm 600 (upload-image))
;loop))
;loop)

4
info.rkt Normal file
View File

@ -0,0 +1,4 @@
#lang info
(define collection "mario-bot")
(define deps '("base"))