File size: 4,180 Bytes
d01fa51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b2a8ae
 
d01fa51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b2a8ae
 
 
 
d01fa51
 
 
 
 
 
 
 
 
 
0b2a8ae
d01fa51
 
0b2a8ae
d01fa51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c04c234
d01fa51
 
 
 
 
 
 
 
 
c04c234
d01fa51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292c906
d01fa51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
tencent translator API
"""

__copyright__ = "Copyright (C) 2020 Nidhal Baccouri"

import base64
import hashlib
import hmac
import os
import time
from typing import List, Optional

import requests

from deep_translator.base import BaseTranslator
from deep_translator.constants import (
    BASE_URLS,
    TENCENT_LANGUAGE_TO_CODE,
    TENCENT_SECRET_ID_ENV_VAR,
    TENCENT_SECRET_KEY_ENV_VAR,
)
from deep_translator.exceptions import (
    ApiKeyException,
    ServerException,
    TencentAPIerror,
    TranslationNotFound,
)
from deep_translator.validate import is_empty, is_input_valid


class TencentTranslator(BaseTranslator):
    """
    class that wraps functions, which use the TentCentTranslator translator
    under the hood to translate word(s)
    """

    def __init__(
        self,
        source: str = "en",
        target: str = "zh",
        secret_id: Optional[str] = os.getenv(TENCENT_SECRET_ID_ENV_VAR, None),
        secret_key: Optional[str] = os.getenv(
            TENCENT_SECRET_KEY_ENV_VAR, None
        ),
        **kwargs
    ):
        """
        @param secret_id: your tencent cloud api secret id.
        Get one here: https://console.cloud.tencent.com/capi
        @param secret_key: your tencent cloud api secret key.
        @param source: source language
        @param target: target language
        """
        if not secret_id:
            raise ApiKeyException(env_var=TENCENT_SECRET_ID_ENV_VAR)

        if not secret_key:
            raise ApiKeyException(env_var=TENCENT_SECRET_KEY_ENV_VAR)

        self.secret_id = secret_id
        self.secret_key = secret_key
        url = BASE_URLS.get("TENENT")
        super().__init__(
            base_url=url,
            source=source,
            target=target,
            languages=TENCENT_LANGUAGE_TO_CODE,
            **kwargs
        )

    def translate(self, text: str, **kwargs) -> str:
        """
        @param text: text to translate
        @return: translated text
        """
        if is_input_valid(text):
            if self._same_source_target() or is_empty(text):
                return text

            # Create the request parameters.
            translate_endpoint = self._base_url.replace("https://", "")
            params = {
                "Action": "TextTranslate",
                "Nonce": 11886,
                "ProjectId": 0,
                "Region": "ap-guangzhou",
                "SecretId": self.secret_id,
                "Source": self.source,
                "SourceText": text,
                "Target": self.target,
                "Timestamp": int(time.time()),
                "Version": "2018-03-21",
            }
            s = "GET" + translate_endpoint + "/?"
            query_str = "&".join(
                "%s=%s" % (k, params[k]) for k in sorted(params)
            )
            hmac_str = hmac.new(
                self.secret_key.encode("utf8"),
                (s + query_str).encode("utf8"),
                hashlib.sha1,
            ).digest()
            params["Signature"] = base64.b64encode(hmac_str)

            # Do the request and check the connection.
            try:
                response = requests.get(self._base_url, params=params)
            except ConnectionError:
                raise ServerException(503)
            # If the answer is not success, raise server exception.
            if response.status_code != 200:
                raise ServerException(response.status_code)
            # Get the response and check is not empty.
            res = response.json()
            if not res:
                raise TranslationNotFound(text)
            # Process and return the response.
            if "Error" in res["Response"]:
                raise TencentAPIerror(res["Response"]["Error"]["Code"])
            return res["Response"]["TargetText"]

    def translate_file(self, path: str, **kwargs) -> str:
        return self._translate_file(path, **kwargs)

    def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
        """
        @param batch: list of texts to translate
        @return: list of translations
        """
        return self._translate_batch(batch, **kwargs)