Skip to content

Instantly share code, notes, and snippets.

@liiker
Last active February 8, 2017 13:40
Show Gist options
  • Select an option

  • Save liiker/3ee7fd02c9694def8124579b92b6225f to your computer and use it in GitHub Desktop.

Select an option

Save liiker/3ee7fd02c9694def8124579b92b6225f to your computer and use it in GitHub Desktop.

Revisions

  1. liiker revised this gist Feb 8, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion youdao.el
    Original file line number Diff line number Diff line change
    @@ -22,4 +22,3 @@
    (interactive)
    (let ((word (read-string "要查询的单词: ")))
    (translate word))))
    )
  2. liiker created this gist Feb 8, 2017.
    25 changes: 25 additions & 0 deletions youdao.el
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    ;; send http get request
    (defun get-url (url)
    (with-current-buffer (url-retrieve-synchronously url) (buffer-string)))

    ;; get word translate origin data
    (defun get-translate-info (word)
    (decode-coding-string (get-url (format "http://fanyi.youdao.com/openapi.do?keyfrom=emacs-fanyi&key=830729599&type=data&doctype=json&version=1.1&q=%s" word)) 'utf-8))

    ;; get chinese explain and send to the minibuffer
    (defun translate (word)
    (let (me-data pos-json json-str info)
    (setq me-data (get-translate-info word))
    (setq pos-json (string-match-p "{.*}" me-data))
    (setq json-str (substring me-data pos-json))
    (setq info (json-read-from-string json-str))
    (print (assoc 'explains (assoc 'basic info)))))

    ;; 绑定翻译快捷键
    (global-set-key
    (kbd "C-c t n")
    (lambda ()
    (interactive)
    (let ((word (read-string "要查询的单词: ")))
    (translate word))))
    )