File size: 2,625 Bytes
9dce458
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

# -*- coding: utf-8 -*-
import uuid
import hashlib
import time
import aiohttp
import time

from .common import CommonTranslator, InvalidServerResponse, MissingAPIKeyException
from .keys import YOUDAO_APP_KEY, YOUDAO_SECRET_KEY

def sha256_encode(signStr):
    hash_algorithm = hashlib.sha256()
    hash_algorithm.update(signStr.encode('utf-8'))
    return hash_algorithm.hexdigest()

class YoudaoTranslator(CommonTranslator):
    _LANGUAGE_CODE_MAP = {
        'CHS': 'zh-CHS',
        'JPN': "ja",
        'ENG': 'en',
        'KOR': 'ko',
        'VIN': 'vi',
        'CSY': 'cs',
        'NLD': 'nl',
        'FRA': 'fr',
        'DEU': 'de',
        'HUN': 'hu',
        'ITA': 'it',
        'PLK': 'pl',
        'PTB': 'pt',
        'ROM': 'ro',
        'RUS': 'ru',
        'ESP': 'es',
        'TRK': 'tr',
        'THA': 'th',
        'IND': 'id'
    }
    _API_URL = 'https://openapi.youdao.com/api'

    def __init__(self):
        super().__init__()
        if not YOUDAO_APP_KEY or not YOUDAO_SECRET_KEY:
            raise MissingAPIKeyException('Please set the YOUDAO_APP_KEY and YOUDAO_SECRET_KEY environment variables before using the youdao translator.')

    async def _translate(self, from_lang, to_lang, queries):
        data = {}
        query_text = '\n'.join(queries)
        data['from'] = from_lang
        data['to'] = to_lang
        data['signType'] = 'v3'
        curtime = str(int(time.time()))
        data['curtime'] = curtime
        salt = str(uuid.uuid1())
        signStr = YOUDAO_APP_KEY + self._truncate(query_text) + salt + curtime + YOUDAO_SECRET_KEY
        sign = sha256_encode(signStr)
        data['appKey'] = YOUDAO_APP_KEY
        data['q'] = query_text
        data['salt'] = salt
        data['sign'] = sign
        #data['vocabId'] = "您的用户词表ID"

        result = await self._do_request(data)
        result_list = []
        if "translation" not in result:
            raise InvalidServerResponse(f'Youdao returned invalid response: {result}\nAre the API keys set correctly?')
        for ret in result["translation"]:
            result_list.extend(ret.split('\n'))
        return result_list

    def _truncate(self, q):
        if q is None:
            return None
        size = len(q)
        return q if size <= 20 else q[0:10] + str(size) + q[size - 10:size]

    async def _do_request(self, data):
        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        async with aiohttp.ClientSession() as session:
            async with session.post(self._API_URL, data=data, headers=headers) as resp:
                return await resp.json()