From a47086c73cff10ae3a3e9718be2bdc90f4f30f73 Mon Sep 17 00:00:00 2001 From: ZhymabekRoman Date: Thu, 7 Mar 2024 16:01:30 +0500 Subject: [PATCH] Prepare for release Promt --- translatepy/translators/promt.py | 46 +++++++++++++++++++------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/translatepy/translators/promt.py b/translatepy/translators/promt.py index c433064..64236fc 100644 --- a/translatepy/translators/promt.py +++ b/translatepy/translators/promt.py @@ -7,6 +7,7 @@ import uuid import hashlib import xml.etree.ElementTree as ET +from warnings import warn from translatepy.language import Language from translatepy.translators.base import BaseTranslateException, BaseTranslator @@ -113,7 +114,6 @@ class PromtHelper: Urdu = Slid(1071, "ur", "ur", "ur_PK") Welsh = Slid(1072, "cy", "cy", "cy_CY") - def _get_supported_languages(self): _languages_list = {Slid._index_rfc[rfc].rfc_prefix for rfc in Slid._index_rfc} _languages_list.add("auto") @@ -132,12 +132,12 @@ def _parse_language_pair(language_pair: str): destination_language_code = match.group(3) if match.group(3) else match.group(4) else: raise ValueError(f"Invalid language pair: {language_pair}") - + source_language = Slid.from_prefix(source_language_code).rfc_prefix destination_language = Slid.from_prefix(destination_language_code).rfc_prefix - + return source_language, destination_language - + class PromtTranslate(BaseTranslator): """ @@ -159,8 +159,6 @@ def __init__(self, request: Request = Request()): if not self._language_pair: self._generate_lang_pair() - print(self._supported_languages) - def _initialize(self) -> str: headers = { 'Host': 'www.translate.ru', @@ -177,6 +175,8 @@ def _initialize(self) -> str: def _set_req_id(self, content: str) -> str: root = ET.fromstring(content) req_id_element = root.find('.//reqId') + if not req_id_element.text: + return self.session_req_id = req_id_element.text return req_id_element.text @@ -212,7 +212,7 @@ def _get_service(self) -> str: def _translate(self, text: str, destination_language: str, source_language: str) -> str: if source_language == "auto": source_language = "a" - + destination_language = Slid.from_rfc_prefix(destination_language).prefix language_pair = f"{source_language}{destination_language}" else: @@ -229,6 +229,7 @@ def _translate(self, text: str, destination_language: str, source_language: str) 'SOAPAction': '"http://tempuri.org/Translate"', }} + # TODO: refactor text = text.replace("&", "&") text = text.replace("<", "<") text = text.replace(">", ">") @@ -238,18 +239,25 @@ def _translate(self, text: str, destination_language: str, source_language: str) payload = f"\t\t{language_pair}\tGeneral\t{text}\tword<params><param><id>1</id><name>Roaming</name><value>off</value></param><param><id>2</id><name>MinimizeTrafficInRoaming</name><value>off</value></param><param><id>3</id><name>TextSource</name><value>TEXT</value></param></params>\ten\t" request = self.session.post(self._soap_endpoint_url, headers=headers, data=payload) self._set_req_id(request.content) - print(request.text) - - root = ET.fromstring(request.text) - c = root.find('.//strResult').text - cdata = ET.fromstring(c) - translation = cdata.findall(".//translation")[0].find("result").text - - translation = translation.replace("&", "&") - translation = translation.replace("<", "<") - translation = translation.replace(">", ">") - translation = translation.replace(""", "\"") - translation = translation.replace("'", "'") + + try: + root = ET.fromstring(request.text) + c = root.find('.//strResult').text + + try: + cdata = ET.fromstring(c) + translation = cdata.findall(".//translation")[0].find("result").text + except Exception as ex: + translation = c + + translation = translation.replace("&", "&") + translation = translation.replace("<", "<") + translation = translation.replace(">", ">") + translation = translation.replace(""", "\"") + translation = translation.replace("'", "'") + except Exception as ex: + warn(f"Can't parse result. Exception: {ex}. Response: {request.text}. Try to set source language manually instead of automatic") + translation = None if source_language == "a": source_language = "auto"