-
Notifications
You must be signed in to change notification settings - Fork 0
/
trans.py
132 lines (122 loc) · 2.82 KB
/
trans.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
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
import requests
import json
LANG = {
# main: 主となる言語名ひとつ(国名そのままなど)
# sub: 別名(そのまま記載)
"BG": {
"main": "ブルガリア"
},
"CS": {
"main": "チェコ"
},
"DA": {
"main": "デンマーク"
},
"DE": {
"main": "ドイツ"
},
"EL": {
"main": "ギリシャ"
},
"EN-GB": {
"sub": ["イギリス英語", "イギリス"]
},
"EN-US": {
"sub": ["アメリカ英語", "アメリカ", "英語"]
},
"ES": {
"main": "スペイン"
},
"ET": {
"main": "エストニア"
},
"FI": {
"main": "フィンランド"
},
"FR": {
"main": "フランス"
},
"HU": {
"main": "ハンガリー"
},
"IT": {
"main": "イタリア"
},
"JA": {
"main": "日本"
},
"LT": {
"main": "リトアニア"
},
"LV": {
"main": "ラトビア"
},
"NL": {
"main": "オランダ"
},
"PL": {
"main": "ポーランド"
},
"PT-PT": {
"main": "ポルトガル"
},
"PT-BR": {
"sub": ["ブラジル"]
},
"RO": {
"main": "ルーマニア"
},
"RU" : {
"main": "ロシア"
},
"SK" : {
"main": "スロバキア"
},
"SL": {
"main": "スロベニア"
},
"SV": {
"main": "スウェーデン"
},
"ZH": {
"main": "中国"
}
}
class TransMocho:
def __init__(self, auth):
self.authkey = auth
self.LANGDIC = {}
for k, v in LANG.items():
if "main" in v:
self.LANGDIC[v["main"]] = k
self.LANGDIC[v["main"]+"語"] = k
if "sub" in v:
for l in v["sub"]:
self.LANGDIC[l] = k
def apisend(self, lang, text):
if lang not in self.LANGDIC:
return ""
data = {
'auth_key': self.authkey,
'text': text,
'target_lang': self.LANGDIC[lang]
}
print("Translate Request:", data)
response = requests.post(
'https://api-free.deepl.com/v2/translate',
data=data
)
print(response.status_code, response.text)
if response.status_code >= 400:
return ""
json_resp = json.loads(response.text)
return json_resp["translations"][0]["text"]
def mocho_response(self, lang, text):
res = self.apisend(lang, text)
if res != "":
return f"\"{res}\" だよ!!(o・▽・o)"
else:
return "わかんない!(o・▽・o)"
if __name__ == "__main__":
t = TransMocho("auth")
print(t.mocho_response("日本語", "はれはれゆかい"))