Skip to content

Instantly share code, notes, and snippets.

@iyahoo
Created September 4, 2015 12:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iyahoo/4c8a25685c238071e8a6 to your computer and use it in GitHub Desktop.
Save iyahoo/4c8a25685c238071e8a6 to your computer and use it in GitHub Desktop.
コマンドラインで翻訳するやつ
#!/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