harry
commited on
Commit
·
5d6c522
1
Parent(s):
f920902
update baidu translator api
Browse files- deep_translator/baidu.py +16 -18
- deep_translator/constants.py +2 -2
- docs/README.rst +30 -0
- tests/test_baidu.py +22 -4
deep_translator/baidu.py
CHANGED
|
@@ -13,8 +13,8 @@ import requests
|
|
| 13 |
|
| 14 |
from deep_translator.base import BaseTranslator
|
| 15 |
from deep_translator.constants import (
|
| 16 |
-
|
| 17 |
-
|
| 18 |
BAIDU_LANGUAGE_TO_CODE,
|
| 19 |
BASE_URLS,
|
| 20 |
)
|
|
@@ -29,7 +29,7 @@ from deep_translator.validate import is_empty, is_input_valid
|
|
| 29 |
|
| 30 |
class BaiduTranslator(BaseTranslator):
|
| 31 |
"""
|
| 32 |
-
class that wraps functions, which use the
|
| 33 |
under the hood to translate word(s)
|
| 34 |
"""
|
| 35 |
|
|
@@ -37,25 +37,25 @@ class BaiduTranslator(BaseTranslator):
|
|
| 37 |
self,
|
| 38 |
source: str = "en",
|
| 39 |
target: str = "zh",
|
| 40 |
-
|
| 41 |
-
|
| 42 |
**kwargs
|
| 43 |
):
|
| 44 |
"""
|
| 45 |
-
@param
|
| 46 |
Get one here: https://fanyi-api.baidu.com/choose
|
| 47 |
-
@param
|
| 48 |
@param source: source language
|
| 49 |
@param target: target language
|
| 50 |
"""
|
| 51 |
-
if not
|
| 52 |
-
raise ApiKeyException(env_var=
|
| 53 |
|
| 54 |
-
if not
|
| 55 |
-
raise ApiKeyException(env_var=
|
| 56 |
|
| 57 |
-
self.
|
| 58 |
-
self.
|
| 59 |
super().__init__(
|
| 60 |
base_url=BASE_URLS.get("BAIDU"),
|
| 61 |
source=source,
|
|
@@ -76,13 +76,11 @@ class BaiduTranslator(BaseTranslator):
|
|
| 76 |
# Create the request parameters.
|
| 77 |
salt = random.randint(32768, 65536)
|
| 78 |
sign = hashlib.md5(
|
| 79 |
-
(self.
|
| 80 |
-
"utf-8"
|
| 81 |
-
)
|
| 82 |
).hexdigest()
|
| 83 |
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
| 84 |
payload = {
|
| 85 |
-
"appid": self.
|
| 86 |
"q": text,
|
| 87 |
"from": self.source,
|
| 88 |
"to": self.target,
|
|
@@ -125,7 +123,7 @@ class BaiduTranslator(BaseTranslator):
|
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
| 127 |
d = BaiduTranslator(
|
| 128 |
-
target="zh",
|
| 129 |
)
|
| 130 |
t = d.translate("Hello\nHow are you?")
|
| 131 |
print("text: ", t)
|
|
|
|
| 13 |
|
| 14 |
from deep_translator.base import BaseTranslator
|
| 15 |
from deep_translator.constants import (
|
| 16 |
+
BAIDU_APPID_VAR,
|
| 17 |
+
BAIDU_APPKEY_VAR,
|
| 18 |
BAIDU_LANGUAGE_TO_CODE,
|
| 19 |
BASE_URLS,
|
| 20 |
)
|
|
|
|
| 29 |
|
| 30 |
class BaiduTranslator(BaseTranslator):
|
| 31 |
"""
|
| 32 |
+
class that wraps functions, which use the BaiduTranslator translator
|
| 33 |
under the hood to translate word(s)
|
| 34 |
"""
|
| 35 |
|
|
|
|
| 37 |
self,
|
| 38 |
source: str = "en",
|
| 39 |
target: str = "zh",
|
| 40 |
+
appid: Optional[str] = os.getenv(BAIDU_APPID_VAR, None),
|
| 41 |
+
appkey: Optional[str] = os.getenv(BAIDU_APPKEY_VAR, None),
|
| 42 |
**kwargs
|
| 43 |
):
|
| 44 |
"""
|
| 45 |
+
@param appid: your baidu cloud api appid.
|
| 46 |
Get one here: https://fanyi-api.baidu.com/choose
|
| 47 |
+
@param appkey: your baidu cloud api appkey.
|
| 48 |
@param source: source language
|
| 49 |
@param target: target language
|
| 50 |
"""
|
| 51 |
+
if not appid:
|
| 52 |
+
raise ApiKeyException(env_var=BAIDU_APPID_VAR)
|
| 53 |
|
| 54 |
+
if not appkey:
|
| 55 |
+
raise ApiKeyException(env_var=BAIDU_APPKEY_VAR)
|
| 56 |
|
| 57 |
+
self.appid = appid
|
| 58 |
+
self.appkey = appkey
|
| 59 |
super().__init__(
|
| 60 |
base_url=BASE_URLS.get("BAIDU"),
|
| 61 |
source=source,
|
|
|
|
| 76 |
# Create the request parameters.
|
| 77 |
salt = random.randint(32768, 65536)
|
| 78 |
sign = hashlib.md5(
|
| 79 |
+
(self.appid + text + str(salt) + self.appkey).encode("utf-8")
|
|
|
|
|
|
|
| 80 |
).hexdigest()
|
| 81 |
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
| 82 |
payload = {
|
| 83 |
+
"appid": self.appid,
|
| 84 |
"q": text,
|
| 85 |
"from": self.source,
|
| 86 |
"to": self.target,
|
|
|
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
d = BaiduTranslator(
|
| 126 |
+
target="zh", appid="some-appid", appkey="some-appkey"
|
| 127 |
)
|
| 128 |
t = d.translate("Hello\nHow are you?")
|
| 129 |
print("text: ", t)
|
deep_translator/constants.py
CHANGED
|
@@ -7,8 +7,8 @@ LIBRE_ENV_VAR = "LIBRE_API_KEY"
|
|
| 7 |
MSFT_ENV_VAR = "MICROSOFT_API_KEY"
|
| 8 |
QCRI_ENV_VAR = "QCRI_API_KEY"
|
| 9 |
YANDEX_ENV_VAR = "YANDEX_API_KEY"
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
|
| 14 |
BASE_URLS = {
|
|
|
|
| 7 |
MSFT_ENV_VAR = "MICROSOFT_API_KEY"
|
| 8 |
QCRI_ENV_VAR = "QCRI_API_KEY"
|
| 9 |
YANDEX_ENV_VAR = "YANDEX_API_KEY"
|
| 10 |
+
BAIDU_APPID_VAR = "BAIDU_APPID"
|
| 11 |
+
BAIDU_APPKEY_VAR = "BAIDU_APPKEY"
|
| 12 |
|
| 13 |
|
| 14 |
BASE_URLS = {
|
docs/README.rst
CHANGED
|
@@ -665,6 +665,36 @@ Libre Translator
|
|
| 665 |
translated = LibreTranslator(source='auto', target='en').translate_file('path/to/file')
|
| 666 |
|
| 667 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
|
| 669 |
Proxy usage
|
| 670 |
-------------
|
|
|
|
| 665 |
translated = LibreTranslator(source='auto', target='en').translate_file('path/to/file')
|
| 666 |
|
| 667 |
|
| 668 |
+
BaiduTranslator
|
| 669 |
+
-----------------
|
| 670 |
+
|
| 671 |
+
.. note::
|
| 672 |
+
|
| 673 |
+
In order to use the BaiduTranslator translator, you need to generate a secret_id and a secret_key.
|
| 674 |
+
deep-translator supports both Pro and free APIs. Just check the examples below.
|
| 675 |
+
Visit http://api.fanyi.baidu.com/product/113 for more information on how to generate your Baidu appid
|
| 676 |
+
and appkey.
|
| 677 |
+
|
| 678 |
+
- Simple translation
|
| 679 |
+
|
| 680 |
+
.. code-block:: python
|
| 681 |
+
|
| 682 |
+
text = 'Hello world'
|
| 683 |
+
translated = BaiduTranslator(appid="your-appid", appkey="your-appkey" source="en", target="zh").translate(text)
|
| 684 |
+
|
| 685 |
+
- Translate batch of texts
|
| 686 |
+
|
| 687 |
+
.. code-block:: python
|
| 688 |
+
=
|
| 689 |
+
texts = ["Hello world", "How are you?"]
|
| 690 |
+
translated = BaiduTranslator(appid="your-appid", appkey="your-appkey" source="en", target="zh").translate_batch(texts)
|
| 691 |
+
|
| 692 |
+
- Translate from a file:
|
| 693 |
+
|
| 694 |
+
.. code-block:: python
|
| 695 |
+
|
| 696 |
+
translated = BaiduTranslator(appid="your-appid", appkey="your-appkey" source="en", target="zh").translate_file('path/to/file')
|
| 697 |
+
|
| 698 |
|
| 699 |
Proxy usage
|
| 700 |
-------------
|
tests/test_baidu.py
CHANGED
|
@@ -9,8 +9,8 @@ from deep_translator.exceptions import BaiduAPIerror
|
|
| 9 |
@patch("deep_translator.baidu.requests")
|
| 10 |
def test_simple_translation(mock_requests):
|
| 11 |
translator = BaiduTranslator(
|
| 12 |
-
|
| 13 |
-
|
| 14 |
source="en",
|
| 15 |
target="zh",
|
| 16 |
)
|
|
@@ -30,8 +30,8 @@ def test_simple_translation(mock_requests):
|
|
| 30 |
@patch("deep_translator.baidu.requests.get")
|
| 31 |
def test_wrong_api_key(mock_requests):
|
| 32 |
translator = BaiduTranslator(
|
| 33 |
-
|
| 34 |
-
|
| 35 |
source="en",
|
| 36 |
target="zh",
|
| 37 |
)
|
|
@@ -45,3 +45,21 @@ def test_wrong_api_key(mock_requests):
|
|
| 45 |
mock_requests.post.return_value = mock_response
|
| 46 |
with pytest.raises(BaiduAPIerror):
|
| 47 |
translator.translate("Hello")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
@patch("deep_translator.baidu.requests")
|
| 10 |
def test_simple_translation(mock_requests):
|
| 11 |
translator = BaiduTranslator(
|
| 12 |
+
appid="this-is-an-valid-appid",
|
| 13 |
+
appkey="this-is-an-valid-appkey",
|
| 14 |
source="en",
|
| 15 |
target="zh",
|
| 16 |
)
|
|
|
|
| 30 |
@patch("deep_translator.baidu.requests.get")
|
| 31 |
def test_wrong_api_key(mock_requests):
|
| 32 |
translator = BaiduTranslator(
|
| 33 |
+
appid="this-is-a-wrong-appid",
|
| 34 |
+
appkey="this-is-a-wrong-appkey",
|
| 35 |
source="en",
|
| 36 |
target="zh",
|
| 37 |
)
|
|
|
|
| 45 |
mock_requests.post.return_value = mock_response
|
| 46 |
with pytest.raises(BaiduAPIerror):
|
| 47 |
translator.translate("Hello")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# the remaining tests are actual requests to Baidu translator API and use appid and appkey
|
| 51 |
+
# if appid and appkey variable is None, they are skipped
|
| 52 |
+
|
| 53 |
+
appid = None
|
| 54 |
+
appkey = None
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@pytest.mark.skipif(
|
| 58 |
+
appid is None or appkey is None,
|
| 59 |
+
reason="appid or appkey is not provided",
|
| 60 |
+
)
|
| 61 |
+
def test_baidu_successful_post_onetarget():
|
| 62 |
+
posted = BaiduTranslator(
|
| 63 |
+
appid=appid, appkey=appkey, source="en", target="zh"
|
| 64 |
+
).translate("Hello! How are you?")
|
| 65 |
+
assert isinstance(posted, str)
|