修改VNR源码接入新版有道中文翻译API

最近在玩兰斯10.

嗯  有道的日中翻译甩百度十条街,个人认为

但VNR用的是老版的接入口,翻译质量惨不忍睹,远远不如百度

所以就自己动手改造了

文件是

Visual Novel Reader\Library\Frameworks\Sakura\py\libs\youdao\youdaofanyi.py

# coding=utf8
import httplib
import md5
import urllib
import random
import json


if __name__ == '__main__':
    import sys


    reload(sys)
    sys.setdefaultencoding('UTF-8')


def translate(text, to='zh-CHS', fr='ja'):
    """Translate from/into simplified Chinese.
    @param  text  unicode not None
    @param  fr  unicode not None
    @param  to  unicode not None
    @return  unicode or None
    """
    appkey = '你的应用id'
    secretkey = '你的应用密匙'
    myurl = '/api'
    salt = random.randint(1, 65536)

    sign = appkey + text + str(salt) + secretkey
    m1 = md5.new()
    m1.update(sign)
    sign = m1.hexdigest()
    myurl = myurl + '?appKey=' + appkey + '&q=' + urllib.quote(
        text.encode('utf-8')) + '&from=' + fr + '&to=' + to + '&salt=' + str(salt) + '&sign=' + sign

    try:
        httpClient = httplib.HTTPConnection('openapi.youdao.com')
        httpClient.request('GET', myurl)

        # response是HTTPResponse对象
        response = httpClient.getresponse()
        result = json.loads(response.read())['translation'][0]
        return result
    except Exception, e:
        print e
    finally:
        if httpClient:
            httpClient.close()


if __name__ == "__main__":

    s = u"じゃあ、今日からここで住むんだ。わーーーい!"
    t = translate(s, to='zh-CHS', fr='ja')

    print t

实际上也有不试用API的方法,但经常要改,我怕麻烦,而且个人使用的话有道智云并不怎么花钱,还有100块试用

修改前后对比


你可能感兴趣的:(修改VNR源码接入新版有道中文翻译API)