Skip to content

Commit

Permalink
[fix] More Bing auth token parse attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhymabekRoman committed Jun 24, 2022
1 parent b5fc64c commit c71b6c3
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions translatepy/translators/bing.py
Expand Up @@ -72,13 +72,18 @@ def __init__(self, request: Request, captcha_callback: Callable[[str], str] = No
self._parse_authorization_data()

def _parse_authorization_data(self):
_request = self.session.get("https://www.bing.com/translator")
_page = _request.text
_parsed_IG = re.findall('IG:"(.*?)"', _page)
_parsed_IID = re.findall('data-iid="(.*?)"', _page)
_parsed_helper_info = re.findall("params_RichTranslateHelper = (.*?);", _page)

if not _parsed_helper_info:
for _ in range(3):
_request = self.session.get("https://www.bing.com/translator")
_page = _request.text
_parsed_IG = re.findall('IG:"(.*?)"', _page)
_parsed_IID = re.findall('data-iid="(.*?)"', _page)
_parsed_helper_info = re.findall("params_RichTranslateHelper = (.*?);", _page)

if not _parsed_helper_info:
continue

break
else:
raise BingTranslateException(message="Can't parse the authorization data, try again later or use MicrosoftTranslate")

_normalized_key = json.loads(_parsed_helper_info[0])[0]
Expand Down

0 comments on commit c71b6c3

Please sign in to comment.