forked from qist/tvbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alist_open.js
275 lines (264 loc) · 9.73 KB
/
alist_open.js
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
import { _ } from './lib/cat.js';
import { findBestLCS } from './lib/similarity.js';
const http = async function (url, options = {}) {
if (options.method == 'POST' && options.data) {
options.body = JSON.stringify(options.data);
options.headers = Object.assign({ 'content-type': 'application/json' }, options.headers);
}
const res = await req(url, options);
res.json = () => (res.content ? JSON.parse(res.content) : null);
res.text = () => res.content;
return res;
};
['get', 'post'].forEach((method) => {
http[method] = function (url, options = {}) {
return http(url, Object.assign(options, { method: method.toUpperCase() }));
};
});
const __drives = {};
const __subtitle_cache = {};
async function get_drives_path(tid) {
const index = tid.indexOf('/', 1);
const name = tid.substring(1, index);
const path = tid.substring(index);
return { drives: await get_drives(name), path };
}
async function get_drives(name) {
const { settings, api, server } = __drives[name];
if (settings.v3 == null) {
//获取 设置
settings.v3 = false;
const data = (await http.get(server + '/api/public/settings')).json().data;
if (_.isArray(data)) {
settings.title = data.find((x) => x.key == 'title')?.value;
settings.v3 = false;
settings.version = data.find((x) => x.key == 'version')?.value;
settings.enableSearch = data.find((x) => x.key == 'enable search')?.value == 'true';
} else {
settings.title = data.title;
settings.v3 = true;
settings.version = data.version;
settings.enableSearch = false; //v3 没有找到 搜索配置
}
//不同版本 接口不一样
api.path = settings.v3 ? '/api/fs/list' : '/api/public/path';
api.file = settings.v3 ? '/api/fs/get' : '/api/public/path';
api.search = settings.v3 ? '/api/public/search' : '/api/public/search';
api.other = settings.v3 ? '/api/fs/other' : null;
}
return __drives[name];
}
let siteKey = '';
let siteType = 0;
function init(cfg) {
siteKey = cfg.skey;
siteType = cfg.stype;
cfg.ext.forEach(
(item) =>
(__drives[item.name] = {
name: item.name,
server: item.server.endsWith('/') ? item.server.substring(0, item.server.length - 1) : item.server,
startPage: item.startPage || '/', //首页
showAll: item.showAll === true, //默认只显示 视频和文件夹,如果想显示全部 showAll 设置true
params: item.params || {},
_path_param: item.params
? _.sortBy(Object.keys(item.params), function (x) {
return -x.length;
})
: [],
settings: {},
api: {},
getParams(path) {
const key = this._path_param.find((x) => path.startsWith(x));
return Object.assign({}, this.params[key], { path });
},
async getPath(path) {
const res = (await http.post(this.server + this.api.path, { data: this.getParams(path) })).json();
return this.settings.v3 ? res.data.content : res.data.files;
},
async getFile(path) {
const res = (await http.post(this.server + this.api.file, { data: this.getParams(path) })).json();
const data = this.settings.v3 ? res.data : res.data.files[0];
if (!this.settings.v3) data.raw_url = data.url; //v2 的url和v3不一样
return data;
},
async getOther(method, path) {
const data = this.getParams(path);
data.method = method;
const res = (await http.post(this.server + this.api.other, { data: data })).json();
return res;
},
isFolder(data) {
return data.type == 1;
},
isVideo(data) {
//判断是否是 视频文件
return this.settings.v3 ? data.type == 2 : data.type == 3;
},
isSubtitle(data) {
if (data.type == 1) return false;
const ext = ['.srt', '.ass', '.scc', '.stl', '.ttml'];
return ext.some((x) => data.name.endsWith(x));
},
getType(data) {
const isVideo = this.isVideo(data);
return this.isFolder(data) ? 0 : isVideo ? 10 : 1;
},
getPic(data) {
let pic = this.settings.v3 ? data.thumb : data.thumbnail;
return pic || (this.isFolder(data) ? 'http://img1.3png.com/281e284a670865a71d91515866552b5f172b.png' : '');
},
getSize(data) {
let sz = data.size || 0;
if (sz <= 0) return '';
let filesize = '';
if (sz > 1024 * 1024 * 1024 * 1024.0) {
sz /= 1024 * 1024 * 1024 * 1024.0;
filesize = 'TB';
} else if (sz > 1024 * 1024 * 1024.0) {
sz /= 1024 * 1024 * 1024.0;
filesize = 'GB';
} else if (sz > 1024 * 1024.0) {
sz /= 1024 * 1024.0;
filesize = 'MB';
} else {
sz /= 1024.0;
filesize = 'KB';
}
return sz.toFixed(2) + filesize;
},
getRemark(data) {
return '';
},
})
);
}
async function dir(dir, pg) {
for (const k in __subtitle_cache) {
delete __subtitle_cache[k];
}
pg = pg || 1;
if (pg == 0) pg == 1;
if (dir === '/' || dir === '') {
const result = _.map(__drives, function (d) {
return { name: d.name, path: '/' + d.name + d.startPage, type: 0, thumb: '' };
});
return JSON.stringify({
parent: '',
page: pg,
pagecount: pg,
list: result,
});
}
let { drives, path } = await get_drives_path(dir);
const id = dir.endsWith('/') ? dir : dir + '/';
const list = await drives.getPath(path);
let subtList = [];
let videos = [];
let allList = [];
list.forEach((item) => {
if (drives.isSubtitle(item)) subtList.push(item.name);
const isVideo = drives.isVideo(item);
if (!drives.showAll && !drives.isFolder(item) && !isVideo) return;
const file = {
name: item.name.replaceAll('$', '_').replaceAll('#', '_'),
path: id + item.name + (drives.isFolder(item) ? '/' : ''),
thumb: drives.getPic(item),
type: drives.getType(item),
size: drives.getSize(item),
remark: drives.getRemark(item),
};
if (drives.isVideo(item)) videos.push(file);
allList.push(file);
});
if (subtList.length > 0) {
videos.forEach((item) => {
var sbust = findBestLCS(item.name, subtList);
if (sbust.bestMatch) __subtitle_cache[item.path] = [id + sbust.bestMatch.target];
});
}
return JSON.stringify({
parent: id,
page: pg,
pagecount: pg,
list: allList,
});
}
async function file(file) {
let { drives, path } = await get_drives_path(file);
const item = await drives.getFile(path);
const subs = [];
if (__subtitle_cache[file]) {
for (const sub of __subtitle_cache[file]) {
try {
let subP = await get_drives_path(sub);
const subItem = await drives.getFile(subP.path);
subs.push(subItem.raw_url);
} catch (error) {}
}
}
if (item.provider === 'AliyundriveShare2Open' && drives.api.other) {
const urls = ['原画', item.raw_url];
try {
const res = await drives.getOther('video_preview', path);
for (const live of res.data.video_preview_play_info.live_transcoding_task_list) {
if (live.status === 'finished') {
urls.push(live.template_id);
urls.push(live.url);
}
}
} catch (error) {}
const result = {
name: item.name,
url: urls,
size: drives.getSize(item),
remark: drives.getRemark(item),
header: {},
extra: {
subt: subs,
},
};
return JSON.stringify(result);
} else if (item.provider === '123Pan') {
let url = item.raw_url;
try {
url = (await http.get(url)).json().data.redirect_url;
} catch (error) {}
const result = {
name: item.name,
url: url,
size: drives.getSize(item),
remark: drives.getRemark(item),
header: {},
extra: {
subt: subs,
},
};
return JSON.stringify(result);
} else {
const result = {
name: item.name,
url: item.raw_url,
size: drives.getSize(item),
remark: drives.getRemark(item),
header: {},
extra: {
subt: subs,
},
};
return JSON.stringify(result);
}
}
function search(wd) {
return JSON.stringify({
list: [],
});
}
export function __jsEvalReturn() {
return {
init: init,
dir: dir,
file: file,
search: search,
};
}