~metalune/simplytranslate-devel

simplytranslate_engines: [PATCH] Adding Advanced GoogleTranslate

Details
Message ID
<VI1P193MB04933897904C83FA48E2E33ADF5F9@VI1P193MB0493.EURP193.PROD.OUTLOOK.COM>
DKIM signature
pass
Download raw message
From: ManeraKai <manerakai@protonmail.com>

---
 simplytranslate_engines/googletranslate.py | 131 ++++++++++++++++++++-
 1 file changed, 129 insertions(+), 2 deletions(-)

diff --git a/simplytranslate_engines/googletranslate.py b/simplytranslate_engines/googletranslate.py
index c4680c4..0838f44 100644
--- a/simplytranslate_engines/googletranslate.py
+++ b/simplytranslate_engines/googletranslate.py
@@ -1,7 +1,9 @@
 import lxml.html as lxml
-from urllib.parse import urlencode
+from urllib.parse import urlencode, quote
 import requests
 import json
+import re
+
 
 class GoogleTranslateEngine:
     name = "google"
@@ -180,7 +182,7 @@ class GoogleTranslateEngine:
 
         return ""
     """
-
+
     def translate(self, text, to_language, from_language="auto"):
         r = requests.get(
             "https://translate.google.com/m",
@@ -197,7 +199,132 @@ class GoogleTranslateEngine:
 
         return ""
 
+    def translate_advanced(self, text, to_language, from_language="auto"):
+        myMap = {}
+        try:
+            url = "https://translate.google.com/_/TranslateWebserverUi/data/batchexecute?rpcids=MkEWBc&rt=c"
+
+            req = json.dumps(
+                [[text, from_language, to_language, True], [None]])
+            req = [[["MkEWBc", req, None, "generic"]]]
+            req = "f.req=" + quote(json.dumps(req))  # URL encode this
+
+            r = requests.post(url,
+                            headers={
+                                'Content-Type': 'application/x-www-form-urlencoded'},
+                            data=req)
+
+            num_match = re.search(r"\n(\d+)\n", r.text)
+            front_pad = num_match.span()[1]
+            end_num = front_pad + int(num_match.groups()[0]) - 1
+
+            data = json.loads(r.text[front_pad:end_num])
+            data = data[0][2]
+            data = json.loads(data)
+
+            translation_data = data[1][0][0]
+            translation_data = translation_data[len(translation_data)-1]
+
+            myList = []
+            for item in translation_data:
+                myList.append(item[0])
+
+            translation = " ".join(myList).replace("\n ", "\n")
+
+            myMap["translated-text"] = translation
+            try:
+                myMap["definitions"] = {}
+                for x in range(0, len(data[3][1][0])):
+                    definition_type = data[3][1][0][x][0]
+                    myMap["definitions"][definition_type] = []
+                    for i in range(0, len(data[3][1][0][x][1])):
+                        myMap["definitions"][definition_type].append({})
+                        definition_box = data[3][1][0][x][1][i]
+
+                        try:
+                            dictionary = definition_box[4][0][0]
+                            myMap["definitions"][definition_type][i]["dictionary"] = dictionary
+                        except:
+                            pass
+
+                        try:
+                            definition = definition_box[0]
+                            myMap["definitions"][definition_type][i]["definition"] = definition
+                        except:
+                            pass
+
+                        try:
+                            use_in_sentence = definition_box[1]
+                            if use_in_sentence is not None:
+                                myMap["definitions"][definition_type][i]["use-in-sentence"] = use_in_sentence
+                        except:
+                            pass
+
+                        try:
+                            synonyms = definition_box[5]
+                            myMap["definitions"][definition_type][i]["synonyms"] = {}
+                            for synonym_box in synonyms:
+                                synonym_type = ""
+                                try:
+                                    synonym_type = synonym_box[1][0][0]
+                                except:
+                                    pass
+                                myMap["definitions"][definition_type][i]["synonyms"][synonym_type] = [
+                                ]
+
+                                try:
+                                    synonym_list = synonym_box[0]
+                                    for synonym_type_word in synonym_list:
+                                        try:
+                                            synonym_type_word_string = synonym_type_word[0]
+                                            myMap["definitions"][definition_type][i]["synonyms"][synonym_type].append(
+                                                synonym_type_word_string)
+                                        except:
+                                            pass
+                                except:
+                                    pass
+
+                        except:
+                            pass
+            except:
+                pass
+
+            try:
+                translation_box = data[3][5][0]
+                myMap["translations"] = {}
+                for x in range(0, len(translation_box)):
+                    try:
+                        translation_type = translation_box[x][0]
+                        myMap["translations"][translation_type] = {}
+                        translation_names_box = translation_box[x][1]
+                        for i in range(0, len(translation_names_box)):
+                            myMap["translations"][translation_type][translation_names_box[i][0]] = {}
+                            frequency = str(translation_names_box[i][3])
+                            if frequency == "3":
+                                frequency = "1"
+                            elif frequency == "1":
+                                frequency = "3"
+
+                            myMap["translations"][translation_type][translation_names_box[i][0]]["words"] = []
+                            for z in range(0, len(translation_names_box[i][2])):
+                                myMap["translations"][translation_type][translation_names_box[i][0]]["words"].append(
+                                    translation_names_box[i][2][z])
+
+                            myMap["translations"][translation_type][translation_names_box[i]
+                                                                                [0]]["frequency"] = frequency+"/3"
+                    except:
+                        pass
+
+            except:
+                pass
+        except :
+            pass
+
+        return myMap
 
 if __name__ == "__main__":
     print(GoogleTranslateEngine().translate("Hello Weird World!!\n\n\nHi!", "fr", "en"))
+
+    data  = GoogleTranslateEngine().translate_advanced("House", "fr", "en")
+    json_object = json.dumps(data, ensure_ascii=False, indent=4)
+    print(json_object)
\ No newline at end of file
-- 
2.25.1
Reply to thread Export thread (mbox)