Created
July 2, 2018 07:58
-
-
Save xuchunyang/78cfd53709e51d44d05841a17a76bac4 to your computer and use it in GitHub Desktop.
Youdao Dictionary Console Version in Racket
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
;;; ydcv.rkt --- Youdao Dictionary Console Version in Racket | |
;; Author: Xu Chunyang <[email protected]> | |
;; Created: Mon Jul 2 15:19:31 CST 2018 | |
;; Usage: | |
;; | |
;; $ racket ydcv.rkt word | |
;; n. [语] 单词;话语;消息;诺言;命令 | |
;; vt. 用言辞表达 | |
;; n. (Word)人名;(英)沃德 | |
;;; Code: | |
#lang racket/base | |
(require net/uri-codec) ; alist->form-urlencoded | |
(require net/http-client) ; http-sendrecv | |
(require json) | |
(define host "fanyi.youdao.com") | |
(define (uri query) | |
(string-append | |
"/openapi.do?" | |
(alist->form-urlencoded | |
`((keyfrom . "YouDaoCV") | |
(key . "659600698") | |
(type . "data") | |
(doctype . "json") | |
(version . "1.1") | |
(q . ,query))))) | |
(module+ test | |
(require rackunit) | |
(check-equal? | |
(string-append "http://" host (uri "hello")) | |
"http://fanyi.youdao.com/openapi.do?keyfrom=YouDaoCV&key=659600698&type=data&doctype=json&version=1.1&q=hello")) | |
(define (explains word) | |
(define-values (status headers port) (http-sendrecv host (uri word))) | |
(define ht (read-json port)) | |
(define explains (hash-ref (hash-ref ht 'basic) 'explains)) | |
explains) | |
(module+ main | |
(if (< (vector-length (current-command-line-arguments)) 1) | |
(displayln "Error: missing argument for query") | |
(let ([word (vector-ref (current-command-line-arguments) 0)]) | |
(for ([i (in-list (explains word))]) | |
(displayln i))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment