Ken Ookami Horo has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/369826 )
Change subject: lib/mt/Youdao.js: Fix Youdao API usages. ...................................................................... lib/mt/Youdao.js: Fix Youdao API usages. Migrate to latest Youdao translate API on http://ai.youdao.com/docs/api.s . Change-Id: Ibed08ac9e05cf44c03ea09f5cb830e9f957099f0 --- M config.dev.yaml M lib/mt/Youdao.js 2 files changed, 88 insertions(+), 71 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/cxserver refs/changes/26/369826/1 diff --git a/config.dev.yaml b/config.dev.yaml index f171eef..33a2057 100644 --- a/config.dev.yaml +++ b/config.dev.yaml @@ -66,6 +66,8 @@ api: https://translate.yandex.net key: null youdao: - api: https://fanyi.youdao.com/paidapi/fanyiapi + api: https://openapi.youdao.com/api key: null - registry: ./registry.yaml + salt: null + secret: null + registry: ./registry.yaml \ No newline at end of file diff --git a/lib/mt/Youdao.js b/lib/mt/Youdao.js index 94cb4da..3dd1aa1 100644 --- a/lib/mt/Youdao.js +++ b/lib/mt/Youdao.js @@ -1,40 +1,42 @@ 'use strict'; var - util = require( 'util' ), - preq = require( 'preq' ), - LinearDoc = require( './../lineardoc' ), - BBPromise = require( 'bluebird' ), - MTClient = require( './MTClient.js' ), - youdaoLanguageNameMap; + util = require('util'), + preq = require('preq'), + LinearDoc = require('./../lineardoc'), + BBPromise = require('bluebird'), + MTClient = require('./MTClient.js'), + crypto = require('crypto'); +youdaoLanguageNameMap; // Youdao language codes differ from the language codes that we use. -youdaoLanguageNameMap = { - 'en>zh': 'EN2ZH_CN', // English to Chinese Simplified - 'simple>zh': 'EN2ZH_CN', // English to Chinese Simplified - 'en>zh-cn': 'EN2ZH_CN', // English to Chinese Simplified - 'simple>zh-cn': 'EN2ZH_CN', // English to Chinese Simplified - 'ja>zh-cn': 'JA2ZH_CN', // Japanese to Chinese Simplified, - 'ja>zh': 'JA2ZH_CN', // Japanese to Chinese Simplified, - 'ko>zh-cn': 'KR2ZH_CN', // Korean to Chinese Simplified - 'fr>zh-cn': 'FR2ZH_CN', // Korean to Chinese Simplified - 'ru>zh-cn': 'RU2ZH_CN', // Russian to Chinese Simplified - 'es>zh-cn': 'SP2ZH_CN', // Spanish to Chinese Simplified - 'zh>en': 'ZH_CN2EN', // Chinese Simplified/Traditional to English - 'zh>simple': 'ZH_CN2EN', // Chinese Simplified/Traditional to Simple English - 'zh>ja': 'ZH_CN2JA', // Chinese Simplified/Traditional to Japanese - 'zh>ko': 'ZH_CN2KR', // Chinese Simplified/Traditional to Korean - 'zh>fr': 'ZH_CN2FR', // Chinese Simplified/Traditional to French - 'zh>ru': 'ZH_CN2RU', // Chinese Simplified/Traditional to Russian - 'zh>es': 'ZH_CN2SP' // Chinese Simplified/Traditional to Spanish +// See Youdao's language list on http://ai.youdao.com/docs/api.s#id5 + + + +var youdaoLanguageNameMap = { + 'zh': 'zh-CHS', + 'zh-cn': 'zh-CHS', + 'ja': 'ja', + 'en': 'en', + 'simple': 'en', + 'ko': 'ko', + 'fr': 'fr', + 'ru': 'ru', + 'pt': 'pt', + 'es': 'es', }; -function Youdao( options ) { +function Youdao(options) { this.logger = options.logger; this.conf = options.conf; } -util.inherits( Youdao, MTClient ); +Youdao.prototype.md5 = function (text) { + return crypto.createHash('md5').update(text).digest('hex'); +}; + +util.inherits(Youdao, MTClient); /** * Translate marked-up text @@ -48,24 +50,24 @@ * @param {string} sourceHtml Source html * @return {Promise} promise: Translated html */ -Youdao.prototype.translateHtml = function ( sourceLang, targetLang, sourceHtml ) { +Youdao.prototype.translateHtml = function (sourceLang, targetLang, sourceHtml) { var i, len, targetDoc, chain = [], self = this; - this.buildSourceDoc( sourceHtml ); + this.buildSourceDoc(sourceHtml); // Clone and adapt sourceDoc - targetDoc = new LinearDoc.Doc( this.sourceDoc.wrapperTag ); + targetDoc = new LinearDoc.Doc(this.sourceDoc.wrapperTag); - function translateItemDeferred( item ) { - if ( item.type !== 'textblock' ) { - return BBPromise.resolve( item ); + function translateItemDeferred(item) { + if (item.type !== 'textblock') { + return BBPromise.resolve(item); } return self.translateText( sourceLang, targetLang, item.item.getPlainText() - ).then( function ( translated ) { + ).then(function (translated) { var newTextBlock; newTextBlock = item.item.translateTags( @@ -76,55 +78,59 @@ type: 'textblock', item: newTextBlock }; - } ); + }); } - for ( i = 0, len = this.sourceDoc.items.length; i < len; i++ ) { - chain.push( translateItemDeferred( this.sourceDoc.items[ i ] ) ); + for (i = 0, len = this.sourceDoc.items.length; i < len; i++) { + chain.push(translateItemDeferred(this.sourceDoc.items[i])); } - return BBPromise.all( chain ).then( function ( results ) { + return BBPromise.all(chain).then(function (results) { targetDoc.items = results; return targetDoc.getHtml(); - } ); + }); }; -Youdao.prototype.translateText = function ( sourceLang, targetLang, sourceText ) { +Youdao.prototype.translateText = function (sourceLang, targetLang, sourceText) { var self = this, key, postData; - key = this.conf.mt.youdao.key; - if ( key === null ) { - return BBPromise.reject( new Error( 'Youdao service is misconfigured' ) ); + var appKey = this.conf.mt.youdao.key; + var appSecret = this.conf.mt.youdao.secret; + + if ((appKey === null) || (appSecret === null)) { + return BBPromise.reject(new Error('Youdao service is misconfigured')); } - if ( sourceText.length > 10000 ) { - return BBPromise.reject( new Error( 'Source text too long: ' + - sourceLang + '-' + targetLang ) ); + if (sourceText.length > 10000) { + return BBPromise.reject(new Error('Source text too long: ' + + sourceLang + '-' + targetLang)); } postData = { uri: this.conf.mt.youdao.api, proxy: this.conf.proxy, body: { - key: key, + appKey: appKey, type: 'data', doctype: 'json', q: sourceText, - l: youdaoLanguageNameMap[ sourceLang + '>' + targetLang ], - transtype: 'translate' + from: youdaoLanguageNameMap[sourceLang], + to: youdaoLanguageNameMap[targetLang], + salt: this.conf.mt.youdao.salt, + sign: self.md5(appKey + sourceText + this.conf.mt.youdao.salt + appSecret) } }; - return preq.post( postData ).then( function ( response ) { - if ( response.body.errorCode === 0 ) { - return response.body.translation[ 0 ]; + return preq.post(postData).then(function (response) { + if (response.body.errorCode === '0') { + return response.body.translation[0]; } else { - throw new Error( 'Translation with Youdao failed. Error: ' + - self.getErrorName( response.body.errorCode ) + - ' ' + sourceLang + '>' + targetLang ); + throw new Error('Translation with Youdao failed. Error: ' + + self.getErrorName(response.body.errorCode) + + ' ' + sourceLang + '>' + targetLang); } - } ); + }); }; /** @@ -133,28 +139,37 @@ * @param {number} code Error code * @return {string} */ -Youdao.prototype.getErrorName = function ( code ) { +Youdao.prototype.getErrorName = function (code) { var errormap = { - 10: 'Some sentence in source text is too long', - 11: 'No dictionay result', - 20: 'Source text too long', - 30: 'Server down', - 40: 'Unsupported language code', - 50: 'Invalid key', - 52: 'IP of the request is invalid', - 60: 'Reaching the spending limit for today', - 70: 'Insufficinent balance' + 101: 'Missing required arguments', + 102: 'Unsupported language code', + 103: 'Source text too long', + 104: 'Unsupported API type', + 105: 'Unsupported sign type', + 106: 'Unsupported response type', + 107: 'Unsupported transport encryption type', + 108: 'Invaild appKey', + 109: 'Invaild batchLog format', + 110: 'No vaild instance associated to this service', + 111: 'Invaild developer account', + 201: 'Decrypt failed', + 202: 'Signature check failed', + 203: 'Not in accessible IP list.', + 301: 'Failed to query dictionary', + 302: 'Failed to check translation service', + 303: 'Server-side exception', + 401: 'Insufficinent balance' }; - if ( code in errormap ) { - return errormap[ code ]; + if (code in errormap) { + return errormap[code]; } return 'Unknown error'; }; Youdao.prototype.requiresAuthorization = function () { - return true; + return false; }; -module.exports = Youdao; +module.exports = Youdao; \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/369826 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ibed08ac9e05cf44c03ea09f5cb830e9f957099f0 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/cxserver Gerrit-Branch: master Gerrit-Owner: Ken Ookami Horo <h...@yoitsu.moe> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits