Created
November 16, 2011 10:03
-
-
Save valvallow/1369736 to your computer and use it in GitHub Desktop.
mtran.scm via wasao
This file contains hidden or 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/local/bin/gosh | |
(use sxml.ssax) | |
(use sxml.sxpath) | |
(use rfc.http) | |
(use rfc.uri) | |
(use gauche.parseopt) | |
(use gauche.charconv) | |
(define api-key "SET YOUR API-KEY INTO THIS!") | |
(define (usage) | |
(print "Usage: mtran [options ...] text") | |
(print " - f|from : from lang") | |
(print " - t|to : to lang") | |
(print " - s|swap : swap 'from lang' to 'to lang'") | |
(print " - h|help : usage") | |
(exit 2)) | |
(define (print-languages) | |
(print "English - en") | |
(print "Japanese - ja") | |
(print "French - fr") | |
(exit 2)) | |
(define (call-translator-api text :optional (from "en")(to "ja")) | |
(receive (status head body) | |
(http-get "api.microsofttranslator.com" | |
(string-append | |
"/V2/Http.svc/Translate?appid=" api-key | |
"&from=" from | |
"&to=" to | |
"&text=" text)) | |
(ssax:xml->sxml (open-input-string body) '()))) | |
(define (main args) | |
(let-args (cdr args) | |
((from "f|from=s" "en") | |
(to "t|to=s" "ja") | |
(swap "s|swap") | |
(lang "l|languages" => print-languages) | |
(help "h|help" => usage) | |
(else (opt . _) | |
(print "Unknown option : " opt) | |
(usage)) | |
. rest) | |
(let ((text (if (null? rest) | |
(port->string (current-input-port)) | |
(car rest)))) | |
(when swap | |
(let1 temp from | |
(set! from to) | |
(set! to temp))) | |
(let1 ret ((sxpath "/*/text()") | |
(call-translator-api (uri-encode-string text) from to)) | |
(print (if (null? ret) | |
"something wrong." | |
(car ret))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment