Skip to content

Instantly share code, notes, and snippets.

@LiberalArtist
Created February 20, 2019 02:35
Show Gist options
  • Save LiberalArtist/ede3aadda139c0e21acb066e3264e984 to your computer and use it in GitHub Desktop.
Save LiberalArtist/ede3aadda139c0e21acb066e3264e984 to your computer and use it in GitHub Desktop.
Simple OxGarage Query Script
#!/usr/bin/env racket
#lang racket/base
(require racket/file
racket/string
racket/port
net/url)
(module+ main
(require racket/cmdline)
(let ([properties #f]
[endpoint default-endpoint]
[mime-type default-mime-type]
[out-file #f])
(command-line
#:once-each
[("-o" "--out")
out.zip
("" " Write response to <out.zip> instead of standard output")
(set! out-file out.zip)]
#:once-any
[("--properties")
properties.xml
("" " Add the contents of <properties.xml> as a \"properties\" parameter")
(set! properties (call-with-input-file* properties.xml read-properties))]
[("--read-properties")
"Like --properties, but read from standard input"
(set! properties (read-properties))]
#:once-each
[("--url")
endpoint-url
("Send request to <endpoint-url> instead of the default"
" (n.b. <endpoint-url> should not contain a \"properties\" parameter)")
(set! endpoint (string->url endpoint-url))]
[("--mime-type")
type
("The MIME type of <file-to-convert>."
" Required unless <file-to-convert> is a .docx document.")
(set! mime-type (string->bytes/utf-8 type))]
#:usage-help
"Convert <file-to-convert> using OxGarage."
"The <file-to-convert> should be a .docx file"
"unless --url and --mime-type are given."
#:args (file-to-convert)
(main (maybe-add-properties endpoint properties)
file-to-convert
mime-type
out-file))))
(define (main endpoint file-to-convert mime-type maybe-out-file)
(define-values [status hs in]
(post-request endpoint file-to-convert mime-type))
(cond
[(not (regexp-match? #rx"200 OK" status))
(define-values [base name-pth dir?]
(split-path (find-system-path 'run-file)))
(error (string->symbol (path->string name-pth))
"OxGarage responded with an error\n status: ~e"
status)]
[maybe-out-file
(call-with-output-file* maybe-out-file
#:exists 'replace
(λ (out) (copy-port in out)))]
[else
(copy-port in (current-output-port))]))
(define (maybe-add-properties u properties)
(if properties
(struct-copy url u
[query `([properties . ,properties])])
u))
(define default-endpoint
(url
"https"
#f
"oxgarage-paderborn.tei-c.org"
#f
#t
(list
(path/param "ege-webservice" '())
(path/param "Conversions" '())
(path/param "docx:application:vnd.openxmlformats-officedocument.wordprocessingml.document" '())
(path/param "TEI:text:xml" '())
(path/param "conversion" '()))
'()
#f))
(define default-mime-type
#"application/vnd.openxmlformats-officedocument.wordprocessingml.document")
(define (post-request endpoint file-to-convert [mime-type default-mime-type])
(define-values [base name dir?]
(split-path file-to-convert))
(http-sendrecv/url
endpoint
#:method #"POST"
#:headers
'(#"Content-Type: multipart/form-data; boundary=---------------------------138629630414318950251157936893")
#:data
(bytes-append
#"\r\n-----------------------------138629630414318950251157936893\r\n"
#"Content-Disposition: form-data; name=\"fileToConvert\"; filename=\"" (path->bytes name) #"\"\r\n"
#"Content-Type: " mime-type #"\r\n"
#"\r\n"
(file->bytes file-to-convert)
#"\r\n-----------------------------138629630414318950251157936893--\r\n")))
(define (read-properties [in (current-input-port)])
(string-trim (port->string in)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment