Skip to content

Commit

Permalink
[fix] Google changed once more the response format [playground] testi…
Browse files Browse the repository at this point in the history
…ng batchexecute
  • Loading branch information
Animenosekai committed Mar 22, 2021
1 parent 2a1291c commit d5f0357
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
65 changes: 65 additions & 0 deletions playground/a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# heavily inspired by ssut/googletrans and https://kovatch.medium.com/deciphering-google-batchexecute-74991e4e446c

import json

src = ""
data = ""
resp = ""

post_data = json.dumps([[
[
'MkEWBc',
json.dumps([["text", "src", "destination", True],[None]], separators=(',', ':')),
None,
'generic',
],
]], separators=(',', ':'))


# broken json parsing
for line in data.split('\n'):
token_found = token_found or '"MkEWBc"' in line[:30]
if not token_found:
continue

opened_square_bracket = 0
closed_square_bracket = 0

is_in_string = False
for index, char in enumerate(line):
if char == '\"' and line[max(0, index - 1)] != '\\':
is_in_string = not is_in_string # flip the bool value
if not is_in_string:
if char == '[':
opened_square_bracket += 1
elif char == ']':
closed_square_bracket += 1

resp += line
if opened_square_bracket == closed_square_bracket:
break

# retrieving the info

parsed = json.loads(json.loads(resp)[0][2])
translated = (' ' if parsed[1][0][0][3] else '').join([part[0] for part in parsed[1][0][0][5]])

if src == 'auto':
try:
src = parsed[2]
except: pass

if src == 'auto':
try:
src = parsed[0][2]
except: pass

origin_pronunciation = None
try:
origin_pronunciation = parsed[0][0]
except: pass

pronunciation = None
try:
pronunciation = parsed[1][0][0][1]
except: pass
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
setup(
name = "translatepy",
packages = ["translatepy"],
version = "1.5.1",
version = "1.5.2",
license = "GNU General Public License v3 (GPLv3)",
description = "Translate, transliterate, get the language of texts in no time with the help of multiple APIs!",
author = "Anime no Sekai",
author_email = "[email protected]",
url = "https://github.com/Animenosekai/translate",
download_url = "https://github.com/Animenosekai/translate/archive/v1.5.1.tar.gz",
download_url = "https://github.com/Animenosekai/translate/archive/v1.5.2.tar.gz",
keywords = ['python', 'translate', 'translation', 'google-translate', 'yandex-translate', 'bing-translate', 'reverso', 'transliteration', 'detect-language'],
install_requires = ['safeIO>=1.2', 'requests', 'beautifulsoup4', 'typing; python_version<"3.5"'],
classifiers = ['Development Status :: 5 - Production/Stable', 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.2', 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9'],
Expand Down
2 changes: 1 addition & 1 deletion translatepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
__copyright__ = 'Copyright 2021, translate'
__credits__ = ['animenosekai']
__license__ = 'GNU General Public License v3 (GPLv3)'
__version__ = 'translatepy v1.5.1'
__version__ = 'translatepy v1.5.2'
__maintainer__ = 'Anime no Sekai'
__email__ = '[email protected]'
__status__ = 'Stable'
2 changes: 1 addition & 1 deletion translatepy/translate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
translatepy v1.5.1 (Stable)
translatepy v1.5.2 (Stable)
© Anime no Sekai — 2021
"""
Expand Down
5 changes: 4 additions & 1 deletion translatepy/translators/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ def translate(self, text, destination_language, source_language="auto") -> Union
request = get("https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=" + str(source_language) + "&tl=" + str(destination_language) + "&q=" + text, headers=HEADERS)
if request.status_code < 400:
data = loads(request.text)
return data[0][0][2], "".join(sentence for sentence in data[0][0][0][0])
try:
return data['ld_result']["srclangs"][0], "".join((sentence["trans"] if "trans" in sentence else "") for sentence in data["sentences"])
except:
return data[0][0][2], "".join(sentence for sentence in data[0][0][0][0])
else:
return None, None
except:
Expand Down

0 comments on commit d5f0357

Please sign in to comment.