main.ts
import * as https from 'https' ;
import * as querystring from 'querystring' ;
import md5 = require ( 'md5' ) ;
import { appId, appSecret} from './private' ;
type ErrorMap = {
[ key: string] : string
}
const errorMap: ErrorMap = {
52003 : '用户认证失败' ,
52004 : 'error2' ,
'unknown' : '服务器繁忙'
}
export const translate = ( word: string) => {
console. log ( word)
const salt = Math. random ( )
const sign = md5 ( appId + word + salt + appSecret)
let from , to
if ( /[a-zA-Z]/ . test ( word[ 0 ] ) ) {
from = 'en'
to = 'zh'
} else {
from = 'zh'
to = 'en'
}
const query: string = querystring. stringify ( {
q: word,
appid: appId,
from ,
to,
salt,
sign
} )
const options = {
hostname: 'api.fanyi.baidu.com' ,
port: 443 ,
path: '/api/trans/vip/translate?' + query,
method: 'GET'
} ;
const request = https. request ( options, ( response) => {
let chunks: Buffer[ ] = [ ]
response. on ( 'data' , ( chunk) => {
chunks. push ( chunk)
} ) ;
response. on ( 'end' , ( ) => {
const string = Buffer. concat ( chunks) . toString ( )
type BaiduResult = {
error_code? : string,
error_msg? : string
from : string,
to: string,
trans_result: {
src: string,
dst: string
} [ ]
}
const object: BaiduResult = JSON . parse ( string)
if ( object. error_code) {
console. error ( errorMap[ object. error_code] || object. error_msg)
process. exit ( 2 )
}
else {
object. trans_result. map ( obj => {
console. log ( obj. dst)
} )
process. exit ( 0 )
}
} )
} ) ;
request. on ( 'error' , ( e) => {
console. error ( e) ;
} ) ;
request. end ( ) ;
}
cli.ts
#! / usr/ bin/ env node
"use strict"
import * as commander from 'commander' ;
import { translate} from './main' ;
const program = new commander. Command ( )
program
. version ( '0.0.1' )
. name ( 'fy' )
. usage ( '<English>' )
. arguments ( '<English>' )
. action ( function ( English) {
translate ( English)
} )
program. parse ( process. argv)