deep-wine-runner/trans/__init__.py
2022-12-07 10:21:50 +08:00

53 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import os
import json
import requests
import traceback
class Trans():
isTrans = False
unCloudTrans = True
word = {}
fileName = ""
def __init__(self, lang="zh_CN", fileName=f"trans.json") -> None:
self.fileName = fileName
self.isTrans = (lang != "zh_CN")
if self.isTrans:
try:
if not os.path.exists(fileName):
with open(fileName, "w") as file:
file.write("{}")
with open(fileName, "r") as file:
self.word = json.loads(file.read())
except:
traceback.print_exc()
self.isTrans = False
def transe(self, temp, text) -> str:
if not self.isTrans:
return text
try:
return self.word[text].replace("", "(").replace("", ")")
except:
if self.unCloudTrans:
return text
# 机翻
data = { 'doctype': 'json', 'type': 'auto','i': text}
jsonReturn = requests.post("http://fanyi.youdao.com/translate", data=data).json()["translateResult"]
transText = ""
for i in jsonReturn:
print(i[0])
transText += f'{i[0]["tgt"]}\n'
if "\n" in text:
transText = transText.replace("\n\n", "\n")[:-1]
else:
transText = transText[:-1]
self.word[text] = transText.replace("", "(").replace("", ")")
try:
with open(self.fileName, "w") as file:
file.write(json.dumps(self.word, ensure_ascii=False))
except:
traceback.print_exc()
print(f"{text}=>{transText}")
return transText