-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogleapi.py
More file actions
133 lines (106 loc) · 4.94 KB
/
Copy pathgoogleapi.py
File metadata and controls
133 lines (106 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import logging
import os
import sys
import traceback
from google.cloud import translate
class Interlayer:
json_key = ""
project_name = ""
translator: translate.TranslationServiceClient
lang_list = {}
class BadTrgLangException(Exception):
pass
class TooManyRequestException(Exception):
pass
class EqualLangsException(Exception):
pass
class BadSrcLangException(Exception):
pass
class TooLongMsg(Exception):
pass
class UnkTransException(Exception):
pass
class LangDetectException(Exception):
pass
class UnknownLang(Exception):
pass
@staticmethod
def init_dialog_api(config):
keypath = input("Please, write path to your JSON Google API Key (optional, key.json as default): ")
if keypath == "":
keypath = "../key.json"
config.add_section("Interlayer")
config.set("Interlayer", "keypath", keypath)
return config
def api_init(self, config):
version = "1.3.1 for googleapi 3.8.4"
build_date = "24.05.2023"
version_polyglot = "1.4.4"
logging.info("Interlayer version {} build date {}".format(version, build_date))
logging.info("Compatible with version of Polyglot {}".format(version_polyglot))
try:
self.json_key = config["Interlayer"]["keypath"]
except KeyError:
self.json_key = "../key.json"
logging.warning("path for JSON file not found! Reset to default (key.json)")
if not os.path.isfile(self.json_key):
logging.error("JSON file wasn't found! Bot will close!")
sys.exit(1)
try:
self.project_name = "projects/" + json.load(open(self.json_key, 'r')).get("project_id")
except Exception as e:
logging.error("Project name isn't readable from JSON! Bot will close!")
logging.error(str(e) + "\n" + traceback.format_exc())
sys.exit(1)
return config
def translate_init(self):
try:
self.translator = translate.TranslationServiceClient.from_service_account_json(self.json_key)
except Exception as e:
logging.error("Translator object wasn't created successful! Bot will close! "
"Please check your JSON key or Google Cloud settings.")
logging.error(str(e) + "\n" + traceback.format_exc())
sys.exit(1)
def extract_lang(self, text):
try:
return self.translator.detect_language(parent=self.project_name,
content=text, timeout=10).languages[0].language_code
except Exception as e:
logging.error(str(e) + "\n" + traceback.format_exc())
raise self.LangDetectException
def list_of_langs(self):
try:
lang_buffer = self.translator.get_supported_languages(parent=self.project_name,
display_language_code="en", timeout=10)
except Exception as e:
logging.error(str(e) + "\n" + traceback.format_exc())
logging.error("Fatal error of creating language list! Bot will be closed!")
sys.exit(1)
for lang in lang_buffer.languages:
self.lang_list.update({lang.language_code: lang.display_name})
def get_translate(self, input_text: str, target_lang: str, distorting=False, src_lang=None):
try:
trans_result = self.translator.translate_text(parent=self.project_name, contents=[input_text],
target_language_code=target_lang,
source_language_code=src_lang,
mime_type="text/plain",
timeout=10).translations[0].translated_text
except Exception as e:
if "400 Target language is invalid." in str(e):
raise self.BadTrgLangException
elif "400 Target language is unsupported." in str(e):
raise self.BadTrgLangException
elif "400 Target language can't be equal to source language." in str(e):
raise self.EqualLangsException
elif "400 Source language is unsupported." in str(e):
raise self.BadSrcLangException
elif "400 Source language is invalid." in str(e):
raise self.BadSrcLangException
else:
logging.error(str(e) + "\n" + traceback.format_exc())
raise self.UnkTransException
if len(trans_result) > 4096 and distorting is False:
logging.warning("too long message for sending.")
raise self.TooLongMsg
return trans_result