Skip to content

Commit

Permalink
Merge pull request #73 from ZhymabekRoman/main
Browse files Browse the repository at this point in the history
fix: Try to fix Yandex & Fix Bing Translate
  • Loading branch information
Animenosekai authored Jan 15, 2023
2 parents 952e2c2 + dee954d commit 490767c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ on:

jobs:
test-py35:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.5
uses: actions/setup-python@v2
with:
python-version: "3.5"
python-version: "3.5.4"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand All @@ -27,21 +27,21 @@ jobs:
python3 setup.py install
- name: Test with pytest
run: |
pytest --cov-config=.coveragerc --cov-report xml:coverage.xml --cov=translatepy -vv tests/
pytest --cov-config=.coveragerc --cov-report xml:coverage.xml --cov=translatepy -vvs tests/
- name: Upload Coverage report
uses: codecov/codecov-action@v2
with:
files: ./coverage.xml

test-py37:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
needs: test-py35
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.7
uses: actions/setup-python@v2
with:
python-version: "3.7"
python-version: "3.7.1"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand All @@ -50,10 +50,10 @@ jobs:
python3 setup.py install
- name: Test with pytest
run: |
pytest -vv
pytest -vvs
test-py39:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
needs: test-py37
steps:
- uses: actions/checkout@v2
Expand All @@ -69,10 +69,10 @@ jobs:
python3 setup.py install
- name: Test with pytest
run: |
pytest -vv
pytest -vvs
test-py310:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
needs: test-py39
steps:
- uses: actions/checkout@v2
Expand All @@ -88,4 +88,4 @@ jobs:
python3 setup.py install
- name: Test with pytest
run: |
pytest -vv tests/
pytest -vvs tests/
38 changes: 25 additions & 13 deletions tests/test_translators.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from translatepy.exceptions import UnsupportedMethod
from translatepy.translators.base import (BaseTranslator, BaseTranslateException)
from translatepy.translators.base import BaseTranslator
from translatepy.translators.bing import (BingTranslate, BingTranslateException)
from translatepy.translators.deepl import (DeeplTranslate, DeeplTranslateException)
from translatepy.translators.google import GoogleTranslateV1, GoogleTranslateV2
from translatepy.translators.mymemory import (MyMemoryTranslate, MyMemoryException)
from translatepy.translators.reverso import ReversoTranslate
from translatepy.translators.translatecom import TranslateComTranslate
from translatepy.translators.yandex import YandexTranslate
from translatepy.translators.yandex import (YandexTranslate, YandexTranslateException)
from translatepy.translators.microsoft import MicrosoftTranslate

IGNORED_EXCEPTIONS = (UnsupportedMethod, DeeplTranslateException, BingTranslateException, MyMemoryException) # DeepL's and Bing's rate limit is way too sensitive
IGNORED_EXCEPTIONS = (UnsupportedMethod, DeeplTranslateException, BingTranslateException, MyMemoryException, YandexTranslateException) # DeepL's and Bing's rate limit is way too sensitive


class TestAllTranslators:
Expand All @@ -32,11 +32,15 @@ def setup(self):
if not isinstance(service, BaseTranslator):
try:
self.services_list.append(service())
except IGNORED_EXCEPTIONS:
pass
except IGNORED_EXCEPTIONS as ex:
self.report_exception("setup", service, ex)
else:
self.services_list.append(service)

def report_exception(self, test: str, service: str, exception: Exception):
print("\n")
print("::warning::During the test, in function '{test}', while testing '{service}', '{exception_name}({exception_info})' exception was caught. Ignoring...".format(test=test, service=str(service), exception_name=exception.__class__.__name__, exception_info=str(exception)))

def test_service_translate(self):
translation_args_list = [("Hello, how are you?", "ja")]

Expand All @@ -45,7 +49,8 @@ def test_service_translate(self):
try:
result = service.translate(*args)
assert result
except IGNORED_EXCEPTIONS:
except IGNORED_EXCEPTIONS as ex:
self.report_exception("translate", service, ex)
continue

def test_service_transliterate(self):
Expand All @@ -56,7 +61,8 @@ def test_service_transliterate(self):
try:
result = service.transliterate(*args)
assert result
except IGNORED_EXCEPTIONS:
except IGNORED_EXCEPTIONS as ex:
self.report_exception("transliterate", service, ex)
continue

def test_service_spellcheck(self):
Expand All @@ -67,7 +73,8 @@ def test_service_spellcheck(self):
try:
result = service.spellcheck(*args)
assert result
except IGNORED_EXCEPTIONS:
except IGNORED_EXCEPTIONS as ex:
self.report_exception("spellcheck", service, ex)
continue

def test_service_example(self):
Expand All @@ -78,7 +85,8 @@ def test_service_example(self):
try:
result = service.example(*args)
assert result
except IGNORED_EXCEPTIONS:
except IGNORED_EXCEPTIONS as ex:
self.report_exception("example", service, ex)
continue

def test_service_dictionary(self):
Expand All @@ -89,7 +97,8 @@ def test_service_dictionary(self):
try:
result = service.dictionary(*args)
assert result
except IGNORED_EXCEPTIONS:
except IGNORED_EXCEPTIONS as ex:
self.report_exception("dictionary", service, ex)
continue

def test_service_language(self):
Expand All @@ -100,7 +109,8 @@ def test_service_language(self):
try:
result = service.language(*args)
assert result
except IGNORED_EXCEPTIONS:
except IGNORED_EXCEPTIONS as ex:
self.report_exception("language", service, ex)
continue

def test_service_text_to_speech(self):
Expand All @@ -111,7 +121,8 @@ def test_service_text_to_speech(self):
try:
result = service.text_to_speech(*args)
assert result
except IGNORED_EXCEPTIONS:
except IGNORED_EXCEPTIONS as ex:
self.report_exception("text_to_speech", service, ex)
continue

def test_service_translate_html(self):
Expand All @@ -122,5 +133,6 @@ def test_service_translate_html(self):
try:
result = service.translate_html(*args)
assert result
except IGNORED_EXCEPTIONS:
except IGNORED_EXCEPTIONS as ex:
self.report_exception("translate_html", service, ex)
continue
2 changes: 1 addition & 1 deletion translatepy/translators/bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def _parse_authorization_data(self):
_page = _request.text
_parsed_IG = re.findall('IG:"(.*?)"', _page)
_parsed_IID = re.findall('data-iid="(.*?)"', _page)
_parsed_helper_info = re.findall("params_RichTranslateHelper = (.*?);", _page)
_parsed_helper_info = re.findall("params_AbusePreventionHelper = (.*?);", _page)

if not _parsed_helper_info:
continue
Expand Down
9 changes: 5 additions & 4 deletions translatepy/translators/yandex.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _transliterate(self, text: str, destination_language: str, source_language:
request = self.session.post(url, data=data)

if request.status_code != 200:
raise YandexTranslateException(request.status_code, request.text)
raise YandexTranslateException(request.status_code)

return source_language, request.text[1:-1]

Expand All @@ -107,7 +107,8 @@ def _spellcheck(self, text: str, source_language: str) -> str:
response = request.json()

if request.status_code != 200:
raise YandexTranslateException(request.status_code, request.text)
raise YandexTranslateException(request.status_code)

for correction in response:
if correction["s"]:
word = correction['word']
Expand Down Expand Up @@ -136,7 +137,7 @@ def _example(self, text: str, destination_language: str, source_language: str):
request = self.session.get(url, params=params)

if request.status_code != 200:
raise YandexTranslateException(request.status_code, request.text)
raise YandexTranslateException(request.status_code)

response = request.json()

Expand All @@ -159,7 +160,7 @@ def _dictionary(self, text: str, destination_language: str, source_language: str
request = self.session.get(url, params=params)

if request.status_code != 200:
raise YandexTranslateException(request.status_code, request.text)
raise YandexTranslateException(request.status_code)
response = request.json()

_result = []
Expand Down

0 comments on commit 490767c

Please sign in to comment.