-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
49 lines (38 loc) · 1.16 KB
/
util.py
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
import hashlib
import logging
from PIL import Image
from googletrans import Translator
from httpx import Timeout
logger = logging.getLogger(__name__)
# logging.basicConfig(level=logging.INFO)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("[%(asctime)s | %(levelname)s | %(filename)s:%(lineno)d]: %(message)s", "%Y-%m-%d %H:%M:%S")
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
def has_chs(title: str):
for i in title:
if u'\u4e00' <= i <= u'\u9fa5':
return True
return False
def get_uuid(raw: str) -> str:
harsher = hashlib.md5()
harsher.update(str(raw).encode("utf-8"))
return harsher.hexdigest()
translator = Translator(service_urls=[
'translate.google.com',
'translate.google.co.kr',
],
timeout=Timeout(10.0)
)
def translate_to_chinese(plain_text: str) -> str:
retry_times = 0
while True:
try:
translated = translator.translate(plain_text, src='auto', dest='zh-cn').text
return translated
except Exception as e:
retry_times += 1
if retry_times > 5:
break
continue