Created
September 4, 2015 12:17
-
-
Save iyahoo/4c8a25685c238071e8a6 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
#!/bin/sh | |
#|-*- mode:lisp -*-|# | |
#| | |
exec ros -Q -- $0 "$@" | |
|# | |
(unless (find-package :drakma) | |
(ql:quickload '(:drakma) :silent t)) | |
(unless (find-package :cl-json) | |
(ql:quickload '(:cl-json) :silent t)) | |
(unless (find-package :babel) | |
(ql:quickload '(:babel) :silent t)) | |
(unless (find-package :cl-html5-parser) | |
(ql:quickload '(:cl-html5-parser) :silent t)) | |
(defun respons-from (url &key method additional-headers parameters) | |
(babel:octets-to-string (drakma:http-request url :method method :additional-headers additional-headers :parameters parameters))) | |
(defun result-string (html) | |
(third (third (nth 3 html)))) | |
(defun main (&rest argv) | |
(when argv | |
(let ((json (respons-from "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13" | |
:method :post | |
:parameters '(("grant_type" . "client_credentials") | |
("client_id" . "client_id") | |
("client_secret" . "client_secret") | |
("scope" . "http://api.microsofttranslator.com"))))) | |
(let* ((access-token (cdr (assoc :access--token (json:decode-json-from-string json)))) | |
(auth (concatenate 'string "Bearer " access-token)) | |
(html (respons-from "http://api.microsofttranslator.com/V2/Http.svc/Translate" | |
:method :get | |
:additional-headers `(("Authorization" . ,auth)) | |
:parameters `(("from" . "en") | |
("to" . "ja") | |
("text" . ,(first argv)))))) | |
(format t "~a~%" | |
(result-string (html5-parser:parse-html5 html :dom :xmls))))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment