Last active
February 5, 2020 15:04
-
-
Save kiyayo/bcdae7bcac4fc5aa92b4598fd2b8715e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env racket | |
#lang racket/base | |
(require racket/port net/url net/http-client json racket/system) | |
(define os (system-type 'os)) | |
(define setfattr (find-executable-path "setfattr")) | |
(define xattr (find-executable-path "xattr")) | |
(define safebooru "safebooru.donmai.us") | |
(define args (current-command-line-arguments)) | |
(define id (substring (vector-ref args 0) 33 41)) | |
(define (search-safebooru) | |
(define-values (status header response) | |
(http-sendrecv safebooru (string-append "/posts/" id ".json")#:ssl? #t)) | |
(read-json response)) | |
(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 tags (hash-ref post 'tag_string)) | |
(define filename (string-append md5 "."file-ext)) | |
(call-with-output-file filename | |
(lambda (in) (display (port->bytes (get-pure-port (string->url url)))in)) | |
#:exists 'replace) | |
(cond | |
[(eq? os 'unix) (system* setfattr "-n" "user.tags" "-v" tags filename)] | |
[(eq? os 'macosx) (system* xattr (string-append "-w" "user.tags" tags filename))])) | |
(displayln "Downloading image") | |
(download-image) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment