mirror of
https://github.com/HIllya51/LunaTranslator.git
synced 2025-01-15 08:53:53 +08:00
27 lines
900 B
Python
27 lines
900 B
Python
from translator.basetranslator import basetrans
|
|
import json
|
|
from language import Languages
|
|
|
|
|
|
class TS(basetrans):
|
|
|
|
def translate(self, query):
|
|
self.checkempty(["apikey"])
|
|
self.checkempty(["apiurl"])
|
|
apikey = self.multiapikeycurrent["apikey"]
|
|
url = self.config["apiurl"] + "/v3/translate?version=2018-05-01"
|
|
headers = {"Content-Type": "application/json"}
|
|
data = {"text": [query], "target": self.tgtlang}
|
|
if self.srclang != Languages.Auto:
|
|
data.update({"source": self.srclang})
|
|
|
|
response = self.proxysession.post(
|
|
url, auth=("apikey", apikey), headers=headers, data=json.dumps(data)
|
|
)
|
|
try:
|
|
result = response.json()
|
|
translation = result["translations"][0]["translation"]
|
|
return translation
|
|
except Exception as e:
|
|
raise Exception(response)
|