-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathSubtitleSearch - assrt.as
294 lines (288 loc) · 8.38 KB
/
SubtitleSearch - assrt.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
subtitle search by assrt
*/
// 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 ServerCheck(string User, string Pass) -> server check
// string ServerLogin(string User, string Pass) -> login
// string GetLanguages() -> get support language
// string SubtitleWebSearch(string MovieFileName, dictionary MovieMetaData) -> search subtitle bu web browser
// array<dictionary> SubtitleSearch(string MovieFileName, dictionary MovieMetaData) -> search subtitle
// string SubtitleDownload(string id) -> download subtitle
string Token;
string convertLang(string lang)
{
if(lang.findFirst("简")>=0||lang.findFirst("chs")>=0){
return "zh-CN";
}else{
if(lang.findFirst("繁")>=0||lang.findFirst("cht")>=0){
return "zh-TW";
}
else{
if(lang.findFirst("英")>=0||lang.findFirst("eng")>=0){
return "en";
}else{
if(lang.findFirst("日")>=0||lang.findFirst("jap")>=0){
return "ja";
}else{
if(lang.findFirst("韩")>=0||lang.findFirst("ko")>=0){
return "ko";
}
}
}
}
}
return "";
}
string UrlComposeQuery(string &host, const string &in path, dictionary &in querys)
{
string ret = host + path + "?";
const array<string> keys = querys.getKeys();
for (int i = 0, len = keys.size(); i < len; i++){
if (i > 0 ){
ret += "&";
}
ret += keys[i] + "=" + HostUrlEncode(string(querys[keys[i]]));
}
return ret;
}
string HtmlSpecialCharsDecode(string str)
{
str.replace("&", "&");
str.replace(""", "\"");
str.replace("'", "'");
str.replace("<", "<");
str.replace(">", ">");
str.replace("’", "'");
return str;
}
string API_URL = "http://api.assrt.net";
array<array<string>> LangTable =
{
{ "en", "English" },
{ "zh-TW", "Chinese" },
{ "zh-CN", "Mandarin" }
};
string GetTitle()
{
return "射手(伪)";
}
string GetVersion()
{
return "2.1";
}
string GetDesc()
{
return "https://github.com/Exhen/PotplayerChineseSubs";
}
string GetLoginTitle()
{
return "Token";
}
string GetLoginDesc()
{
return "账户随便填,密码填assrt的token";;
}
string GetLanguages()
{
string ret = "";
for(int i = 0, len = LangTable.size(); i < len; i++)
{
string lang = LangTable[i][0];
if (!lang.empty())
{
if (ret.empty()) ret = lang;
else ret = ret + "," + lang;
}
}
return ret;
}
string ServerCheck(string User, string Pass)
{
return ServerLogin(User,Pass);
}
string ServerLogin(string User, string Pass)
{
Token = Pass;
string r = HostUrlGetString(API_URL+"/v1/sub/search?token=" + Pass + "&q=%E9%A2%90%E5%92%8C%E5%9B%AD");
JsonValue json;
JsonReader jsonR;
if (jsonR.parse(r,json))
{
// HostPrintUTF8("status: "+formatInt(json["status"].asInt()));
if(json["status"].asInt()==0)
{
return "成功:"+Token;
}
else{
return "错误:"+json["errmsg"].asString();
}
}
return "错误:无法连接";
}
string SubtitleWebSearch(string MovieFileName, dictionary MovieMetaData)
{
string title = HtmlSpecialCharsDecode(string(MovieMetaData["title"]));
if(MovieMetaData.exists("seasonNumber")){
string season=string(MovieMetaData["seasonNumber"]);
if(season.length()<2){
season='0'+season;
}
if(MovieMetaData.exists("episodeNumber")){
string episode=string(MovieMetaData["episodeNumber"]);
if(episode.length()<2){
episode='0'+episode;
}
title=title+" S"+season+'E'+episode;
}
else{
title=title+" S"+season;
}
}
string finalURL = UrlComposeQuery(API_URL, '/v1/sub/search', {
{"token", Token},
{"q", title},
{"filelist", '1'},
{"cnt", '15'},
{"no_muxer", '1'}
});
return finalURL;
}
array<dictionary> SubtitleSearch(string MovieFileName, dictionary MovieMetaData)
{
array<dictionary> ret;
// HostOpenConsole();
array<string> MovieFileNameSplit=MovieFileName.split(".");
if(MovieFileNameSplit[MovieFileNameSplit.length()-1]=="mpls"||MovieFileNameSplit[MovieFileNameSplit.length()-1]=="m2ts"){
return ret;
}
string finalURL = SubtitleWebSearch(MovieFileName, MovieMetaData);
for(int j=0;;j++){
string URL=finalURL+"&pos="+formatInt(j*15);
// HostPrintUTF8(URL);
string json = HostUrlGetString(URL);
JsonReader Reader;
JsonValue Root;
if (Reader.parse(json, Root) && Root.isObject())
{
if (Root["status"].isInt()){
int status = Root["status"].asInt();
if (status == 0) {
JsonValue subs = Root["sub"]["subs"];
if (subs.isArray()){
for(int i = 0, len = subs.size(); i < len; i++){
if(subs[i]["filelist"].size()>0){
for(int f=0,fLen=subs[i]["filelist"].size();f<fLen;f++){
dictionary item;
int id=subs[i]["id"].asInt();
item["id"]=formatInt(id)+"¥"+formatInt(f);
item["title"]=subs[i]["native_name"].asString();
string fileName=subs[i]["filelist"][f]["f"].asString();
array<string> fileNameSplit=fileName.split(".");
string subtype=fileNameSplit[fileNameSplit.length()-1];
string lang=fileNameSplit[fileNameSplit.length()-2];
item["fileName"]=fileName;
item["format"]=subtype;
item["lang"]=convertLang(lang);
item["url"]="http://assrt.net/xml/sub/"+formatInt(id/1000)+"/"+formatInt(id)+".xml";
ret.insertLast(item);
}
}
else{
dictionary item;
int id=subs[i]["id"].asInt();
item["id"]=formatInt(id);
item["title"]=subs[i]["native_name"].asString();
string fileName=subs[i]["filename"].asString();
array<string> fileNameSplit=fileName.split(".");
string subtype=fileNameSplit[fileNameSplit.length()-1];
string lang=fileNameSplit[fileNameSplit.length()-2];
item["fileName"]=fileName;
item["format"]=subtype;
item["lang"]=convertLang(lang);
item["url"]="http://assrt.net/xml/sub/"+formatInt(id/1000)+"/"+formatInt(id)+".xml";
ret.insertLast(item);
}
}
if(subs.size()<15){
break;
}
}
else{
break;
}
}
else{
break;
}
}
else{
break;
}
}
else{
break;
}
}
return ret;
}
string SubtitleDownload(string id)
{
array<string> idSplit=id.split("¥");
if(idSplit.size()>1){
int num=parseInt(idSplit[1]);
string api = UrlComposeQuery(API_URL, "/v1/sub/detail", {
{"token", Token},
{"id", idSplit[0]}
});
string downJson = HostUrlGetString(api);
JsonReader downReader;
JsonValue downRoot;
if (downReader.parse(downJson, downRoot) && downRoot.isObject())
{
if (downRoot["status"].isInt()){
// HostPrintUTF8(downRoot["status"].asString());
int downStatus = downRoot["status"].asInt();
if (downStatus == 0) {
JsonValue fSubs = downRoot["sub"]["subs"];
if (fSubs.isArray()){
JsonValue subDetail = fSubs[0];
if (subDetail.isObject()){
if(subDetail["filelist"].size()>num){
return HostUrlGetString(subDetail["filelist"][num]["url"].asString());
}
}
}
}
}
}
}else{
string api = UrlComposeQuery(API_URL, "/v1/sub/detail", {
{"token", Token},
{"id", idSplit[0]}
});
string downJson = HostUrlGetString(api);
JsonReader downReader;
JsonValue downRoot;
if (downReader.parse(downJson, downRoot) && downRoot.isObject())
{
if (downRoot["status"].isInt()){
// HostPrintUTF8(downRoot["status"].asString());
int downStatus = downRoot["status"].asInt();
if (downStatus == 0) {
JsonValue fSubs = downRoot["sub"]["subs"];
if (fSubs.isArray()){
JsonValue subDetail = fSubs[0];
if (subDetail.isObject()){
return HostUrlGetString(subDetail["url"].asString());
}
}
}
}
}
}
return "";
}