This repository has been archived by the owner on Dec 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
SubtitleTranslate - googleFix.as
219 lines (185 loc) · 5.44 KB
/
SubtitleTranslate - googleFix.as
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
real time subtitle translate for PotPlayer using google API
*/
// string GetTitle() -> get title for UI
// string GetVersion -> get version for manage
// string GetDesc() -> get detail information
// string GetLoginTitle() -> get title for login dialog
// string GetLoginDesc() -> get desc for login dialog
// string ServerLogin(string User, string Pass) -> login
// string ServerLogout() -> logout
// array<string> GetSrcLangs() -> get source language
// array<string> GetDstLangs() -> get target language
// string Translate(string Text, string &in SrcLang, string &in DstLang) -> do translate !!
string JsonParseNew(string json)
{
JsonReader Reader;
JsonValue Root;
string ret = "";
if (Reader.parse(json, Root) && Root.isObject())
{
JsonValue data = Root["data"];
if (data.isObject())
{
JsonValue translations = data["translations"];
if (translations.isArray())
{
for (int j = 0, len = translations.size(); j < len; j++)
{
JsonValue child1 = translations[j];
if (child1.isObject())
{
JsonValue translatedText = child1["translatedText"];
if (translatedText.isString()) ret = ret + translatedText.asString();
}
}
}
}
}
return ret;
}
string JsonParse_for_openapi(string json)
{
JsonReader Reader;
JsonValue Root;
string err_msg = "translate fail :(";
// HostPrintUTF8(json);
if (Reader.parse(json, Root))
{
string sub_json = Root[0][2].asString();
if (Reader.parse(sub_json, Root))
{
string ans ="";
JsonValue translations = Root[1][0][0][5];
for (int i = 0, len = translations.size(); i < len; i++)
{
ans += translations[i][0].asString();
}
return ans;
}
return err_msg;
}
return err_msg;
}
array<string> LangTable =
{
"af", "sq", "am", "ar", "hy", "az", "eu", "be", "bn", "bh", "bs", "bg", "my", "ca", "ceb",
"zh-CN","zh-TW","hr","cs","da","nl","en","eo","et","tl","fi","fr","gl","ka","de","el","gu",
"ht","ha","iw","hi","hmn","hu","is","ig","id","ga","it","ja","jw","kn","kk","km","ko","ku",
"ky","lo","la","lv","lt","mk","ms","ml","mt","mi","mr","mn","ne","no","ps","fa","pl","pt",
"pa","ro","ru","sr","sd","st","si","sk","sl","so","es","sw","sv","sw","tg","ta","te","th",
"tr","uk","ur","uz","vi","cy","xh","yi","yo","zu"
};
string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36";
string TranslateHost = "translate.google.com";
string RPC_ID = 'MkEWBc';
string GetTitle()
{
return "{$CP949=구글 번역Fix$}{$CP950=Google 翻譯Fix$}{$CP0=Google translateFix$}";
}
string GetVersion()
{
return "1";
}
string GetDesc()
{
return "https://translate.google.com";
}
string GetLoginTitle()
{
return "Input google API key";
}
string GetLoginDesc()
{
return "Input google API key to user name";
}
string api_key;
string ServerLogin(string User, string Pass)
{
api_key = User;
if (api_key.empty()) return "fail";
return "200 ok";
}
void ServerLogout()
{
api_key = "";
}
array<string> GetSrcLangs()
{
array<string> ret = LangTable;
ret.insertAt(0, ""); // empty is auto
return ret;
}
array<string> GetDstLangs()
{
array<string> ret = LangTable;
return ret;
}
array<string> split(string str, string delimiter)
{
array<string> parts;
int startPos = 0;
while (true) {
int index = str.findFirst(delimiter, startPos);
if ( index == -1 ) {
parts.insertLast( str.substr(startPos) );
break;
}
else {
parts.insertLast( str.substr(startPos, index - startPos) );
startPos = index + delimiter.length();
}
}
return parts;
}
string Translate(string Text, string &in SrcLang, string &in DstLang)
{
// HostOpenConsole(); // for debug
if (SrcLang.length() <= 0) SrcLang = "auto";
SrcLang.MakeLower();
// use user's api_key
if (api_key.length() > 0)
{
string enc = HostUrlEncode(Text);
string url = "https://translation.googleapis.com/language/translate/v2?target=" + DstLang + "&q=" + enc;
if (!SrcLang.empty() && SrcLang != "auto") url = url + "&source=" + SrcLang;
url = url + "&key=" + api_key;
string text = HostUrlGetString(url, UserAgent);
string ret = JsonParseNew(text);
if (ret.length() > 0)
{
SrcLang = "UTF8";
DstLang = "UTF8";
return ret;
}
}
// use open api(for free)
string rpc_url = "https://"+TranslateHost+"/_/TranslateWebserverUi/data/batchexecute?rpcids="+RPC_ID+"&bl=boq_translate-webserver_20221005.09_p0&soc-app=1&soc-platform=1&soc-device=1&rt=c";
string post_data1 = "[[[\"MkEWBc\",\"[[\\\"";
string post_data2 = "\\\",\\\""+SrcLang+"\\\",\\\""+DstLang+"\\\",true],[null]]\",null,\"generic\"]]]";
Text.replace("\\","\\\\");
Text.replace("\"","\\\"");
Text.replace("\\","\\\\");
Text.replace("\"","\\\"");
Text.replace("\n","\\\\n");
Text.replace("\r","\\\\r");
Text.replace("\t","\\\\t");
string enc_text = Text;
string post_data = "f.req="+HostUrlEncode(post_data1+enc_text+post_data2);
string SendHeader = "Content-Type: application/x-www-form-urlencoded";
string text = HostUrlGetString(rpc_url, UserAgent, SendHeader, post_data);
text.replace("\n","");
int start_pos = text.findFirst("[[", 0);
int end_pos = text.findLast("]]", -1);
text=text.substr(start_pos, end_pos - start_pos);
end_pos = text.findLast("]]", -1);
text=text.substr(0, end_pos+2);
string ret = JsonParse_for_openapi(text);
if (ret.length() > 0)
{
SrcLang = "UTF8";
DstLang = "UTF8";
return ret;
}
return ret;
}