-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Azin Zahraei
committed
Apr 25, 2021
1 parent
737afc4
commit 3e78df0
Showing
3 changed files
with
44 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env python | ||
# encoding: utf-8 | ||
import requests | ||
import json | ||
|
||
from .base import BaseProvider | ||
from ..constants import TRANSLATION_FROM_DEFAULT | ||
from ..exceptions import TranslationError | ||
|
||
|
||
class DeeplProvider(BaseProvider): | ||
''' | ||
@DeeplProvider: This is a integration with DeepL Translator API. | ||
Website: https://www.deepl.com | ||
Documentation: https://www.deepl.com/docs-api | ||
''' | ||
name = 'Deepl' | ||
base_url = 'https://api-free.deepl.com/v2/translate' | ||
|
||
def _make_request(self, text): | ||
params = { | ||
'auth_key': self.secret_access_key, | ||
'target_lang': self.to_lang, | ||
'text': text | ||
} | ||
|
||
if self.from_lang != TRANSLATION_FROM_DEFAULT: | ||
params['source_lang'] = self.from_lang | ||
|
||
response = requests.post(self.base_url, params=params, headers=self.headers, json=[{}]) | ||
return json.loads(response.text) | ||
|
||
def get_translation(self, text): | ||
data = self._make_request(text) | ||
|
||
if "error" in data: | ||
raise TranslationError(data["error"]["message"]) | ||
|
||
return data["translations"][0]["text"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters