Skip to content

Instantly share code, notes, and snippets.

@iyahoo iyahoo/cltrans.lisp
Created Sep 4, 2015

Embed
What would you like to do?
コマンドラインで翻訳するやつ
#!/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
You can’t perform that action at this time.