mirror of
https://gitee.com/gfdgd-xi/deep-wine-runner
synced 2025-12-14 11:02:04 +08:00
初步精简
This commit is contained in:
53
trans/__init__.py
Normal file
53
trans/__init__.py
Normal file
@@ -0,0 +1,53 @@
|
||||
#!/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
|
||||
Reference in New Issue
Block a user