alist.js 23.7 KB
Newer Older
H
测试  
hjdhnx 已提交
1
// import _ from 'https://underscorejs.org/underscore-esm-min.js'
H
hjdhnx 已提交
2 3
// import {distance} from 'https://unpkg.com/fastest-levenshtein@1.0.16/esm/mod.js'
import {distance} from 'https://gitcode.net/qq_32394351/dr_py/-/raw/master/libs/mod.js'
H
hjdhnx 已提交
4
import {sortListByCN} from 'https://gitcode.net/qq_32394351/dr_py/-/raw/master/libs/sortName.js'
H
测试  
hjdhnx 已提交
5 6 7 8 9 10 11 12

/**
 * alist js
 * 配置设置 {"key":"Alist","name":"Alist","type":3,"api":"http://xxx.com/alist.js","searchable":0,"quickSearch":0,"filterable":0,"ext":"http://xxx.com/alist.json"}
 * alist.json [{
				name:'名称',
				server:'地址',
				startPage:'/',		 //启动文件夹
H
 
hjdhnx 已提交
13
				showAll: false ,	//是否显示全部文件,默认false只显示 音视频和文件夹
H
hjdhnx 已提交
14
 				search: true, // 启用小雅的搜索,搜索只会搜第一个开启此开关的磁盘
H
测试  
hjdhnx 已提交
15 16 17 18 19 20 21 22 23 24 25 26
				params:{ 			//对应文件夹参数 如设置对应文件夹的密码
					'/abc':{ password : '123' },
					'/abc/abc':{ password : '123' },
				}
		}]
 * 提示 想要加载文件夹里面全部视频到详情(看剧可以自动播放下一集支持历史记录)
 *		需要改软件才能支持,,建议长按文件夹时添加判断 tag == folder 时跳转 DetailActivity
 */
String.prototype.rstrip = function (chars) {
	let regex = new RegExp(chars + "$");
	return this.replace(regex, "");
};
H
hjdhnx 已提交
27
var showMode = 'single';
H
hjdhnx 已提交
28
var searchDriver = '';
H
hjdhnx 已提交
29
var limit_search_show = 200;
H
hjdhnx 已提交
30
var search_type = '';
H
hjdhnx 已提交
31
var detail_order = 'name';
H
hjdhnx 已提交
32
var playRaw = 1; // 播放直链获取,默认0直接拼接/d 填1可以获取阿里oss链接。注意,有时效性
H
hjdhnx 已提交
33
const request_timeout = 5000;
H
hjdhnx 已提交
34
const VERSION = 'alist v2/v3 20221223';
H
hjdhnx 已提交
35
const UA = 'Mozilla/5.0'; //默认请求ua
H
测试  
hjdhnx 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/**
 * 打印日志
 * @param any 任意变量
 */
function print(any){
	any = any||'';
	if(typeof(any)=='object'&&Object.keys(any).length>0){
		try {
			any = JSON.stringify(any);
			console.log(any);
		}catch (e) {
			// console.log('print:'+e.message);
			console.log(typeof(any)+':'+any.length);
		}
	}else if(typeof(any)=='object'&&Object.keys(any).length<1){
		console.log('null object');
	}else{
		console.log(any);
	}
}

H
hjdhnx 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/*** js自封装的方法 ***/

/**
 * 获取链接的host(带http协议的完整链接)
 * @param url 任意一个正常完整的Url,自动提取根
 * @returns {string}
 */
function getHome(url){
	if(!url){
		return ''
	}
	let tmp = url.split('//');
	url = tmp[0] + '//' + tmp[1].split('/')[0];
	try {
		url = decodeURIComponent(url);
	}catch (e) {}
	return url
}

H
测试  
hjdhnx 已提交
76 77 78 79 80
const http = 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);
	}
H
hjdhnx 已提交
81
	options.timeout = request_timeout;
H
hjdhnx 已提交
82 83 84 85 86 87 88
	if(!options.headers){
		options.headers = {};
	}
	let keys = Object.keys(options.headers).map(it=>it.toLowerCase());
	if(!keys.includes('referer')){
		options.headers['Referer'] = getHome(url);
	}
H
hjdhnx 已提交
89 90 91
	if(!keys.includes('user-agent')){
		options.headers['User-Agent'] = UA;
	}
H
hjdhnx 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105
	try {
		const res = req(url, options);
		res.json = () => res&&res.content ? JSON.parse(res.content) : null;
		res.text = () => res&&res.content ? res.content:'';
		return res
	}catch (e) {
		return {
			json() {
				return null
			}, text() {
				return ''
			}
		}
	}
H
测试  
hjdhnx 已提交
106 107 108 109 110 111 112 113 114
};
["get", "post"].forEach(method => {
    http[method] = function (url, options = {}) {
        return http(url, Object.assign(options, {method: method.toUpperCase()}));
    }
});

const __drives = {};

H
hjdhnx 已提交
115
function isMedia(file){
H
hjdhnx 已提交
116
	return /\.(dff|dsf|mp3|aac|wav|wma|cda|flac|m4a|mid|mka|mp2|mpa|mpc|ape|ofr|ogg|ra|wv|tta|ac3|dts|tak|webm|wmv|mpeg|mov|ram|swf|mp4|avi|rm|rmvb|flv|mpg|mkv|m3u8|ts|3gp|asf)$/.test(file.toLowerCase());
H
hjdhnx 已提交
117 118
}

H
测试  
hjdhnx 已提交
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
function get_drives_path(tid) {
	const index = tid.indexOf('$');
	const name = tid.substring(0, index);
	const path = tid.substring(index + 1);
	return { drives: get_drives(name), path };
}

function get_drives(name) {
	const { settings, api, server } = __drives[name];
	if (settings.v3 == null) { //获取 设置
		settings.v3 = false;
		const data = http.get(server + '/api/public/settings').json().data;
		if (Array.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';
	}
	return __drives[name]
}

function init(ext) {
H
hjdhnx 已提交
151
	console.log("当前版本号:"+VERSION);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	let data;
	if (typeof ext == 'object'){
		data = ext;
		print('alist ext:object');
	} else if (typeof ext == 'string') {
		if (ext.startsWith('http')) {
			let alist_data = ext.split(';');
			let alist_data_url = alist_data[0];
			limit_search_show = alist_data.length>1?Number(alist_data[1])||limit_search_show:limit_search_show;
			search_type = alist_data.length>2?alist_data[2]:search_type;
			print(alist_data_url);
			data = http.get(alist_data_url).json(); // .map(it=>{it.name='🙋丫仙女';return it})
		} else {
			print('alist ext:json string');
			data = JSON.parse(ext);
		}
	}

H
hjdhnx 已提交
170 171
	// print(data); // 测试证明壳子标题支持emoji,是http请求源码不支持emoji
	let drives = [];
H
alist  
hjdhnx 已提交
172
	if(Array.isArray(data) && data.length > 0 && data[0].hasOwnProperty('server') && data[0].hasOwnProperty('name')){
H
hjdhnx 已提交
173 174 175 176 177 178
		drives = data;
	}else if(!Array.isArray(data)&&data.hasOwnProperty('drives')&&Array.isArray(data.drives)){
		drives = data.drives.filter(it=>(it.type&&it.type==='alist')||!it.type);
	}
	print(drives);
	searchDriver = (drives.find(x=>x.search)||{}).name||'';
H
alist  
hjdhnx 已提交
179
	if(!searchDriver && drives.length > 0){
H
hjdhnx 已提交
180 181 182 183
		searchDriver = drives[0].name;
	}
	print(searchDriver);
	drives.forEach(item => {
H
测试  
hjdhnx 已提交
184 185 186 187 188 189
		let _path_param = [];
		if(item.params){
			_path_param = Object.keys(item.params);
			// 升序排列
			_path_param.sort((a,b)=>(a.length-b.length));
		}
H
hjdhnx 已提交
190 191 192 193 194 195 196 197 198 199 200
		if(item.password){
			let pwdObj = {
				password: item.password
			};
			if(!item.params){
				item.params = {'/':pwdObj};
			}else{
				item.params['/'] = pwdObj;
			}
			_path_param.unshift('/');
		}
H
测试  
hjdhnx 已提交
201 202 203 204 205
		__drives[item.name] = {
			name: item.name,
			server: item.server.endsWith("/") ? item.server.rstrip("/") : item.server,
			startPage: item.startPage || '/', //首页
			showAll: item.showAll === true, //默认只显示 视频和文件夹,如果想显示全部 showAll 设置true
H
hjdhnx 已提交
206
			search: !!item.search, //是否支持搜索,只有小丫的可以,多个可搜索只取最前面的一个
H
测试  
hjdhnx 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219
			params: item.params || {},
			_path_param: _path_param,
			settings: {},
			api: {},
			getParams(path) {
				const key = this._path_param.find(x => path.startsWith(x));
				return Object.assign({}, this.params[key], { path });
			},
			getPath(path) {
				const res = http.post(this.server + this.api.path, { data: this.getParams(path) }).json();
				return this.settings.v3 ? res.data.content : res.data.files
			},
			getFile(path) {
H
hjdhnx 已提交
220 221
				let raw_url = this.server+'/d'+path;
				raw_url = encodeURI(raw_url);
H
hjdhnx 已提交
222
				let data = {raw_url:raw_url,raw_url1:raw_url};
H
hjdhnx 已提交
223
				if(playRaw===1){
H
hjdhnx 已提交
224 225 226 227 228 229 230 231 232 233
					try {
						const res = http.post(this.server + this.api.file, { data: this.getParams(path) }).json();
						data = this.settings.v3 ? res.data : res.data.files[0];
						if (!this.settings.v3) {
							data.raw_url = data.url; //v2 的url和v3不一样
						}
						data.raw_url1 = raw_url;
						return data
					}catch (e) {
						return data
H
hjdhnx 已提交
234 235
					}
				}else{
H
hjdhnx 已提交
236
					return data
H
hjdhnx 已提交
237
				}
H
测试  
hjdhnx 已提交
238 239 240
			},
			isFolder(data) { return data.type === 1 },
			isVideo(data) { //判断是否是 视频文件
H
hjdhnx 已提交
241
				// return this.settings.v3 ? data.type === 2 : data.type === 3
H
hjdhnx 已提交
242 243
				// 增加音乐识别 视频,其他,音频
				return this.settings.v3 ? (data.type === 2||data.type===0||data.type===3) : (data.type === 3||data.type===0||data.type === 4)
H
测试  
hjdhnx 已提交
244 245 246 247 248 249 250 251 252 253 254 255
			},
			is_subt(data) {
				if (data.type === 1) {
					return false;
				}
				const ext = /\.(srt|ass|scc|stl|ttml)$/;  // [".srt", ".ass", ".scc", ".stl", ".ttml"];
				// return ext.some(x => data.name.endsWith(x));
				return ext.test(data.name);
			},
			getPic(data) {
				let pic = this.settings.v3 ? data.thumb : data.thumbnail;
				return pic || (this.isFolder(data) ? "http://img1.3png.com/281e284a670865a71d91515866552b5f172b.png" : '');
H
hjdhnx 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
			},
			getTime(data,isStandard) {
				isStandard = isStandard||false;
				try {
					let tTime = data.updated_at || data.time_str || data.modified || "";
					let date = '';
					if(tTime){
						tTime = tTime.split("T");
						date = tTime[0];
						if(isStandard){
							date = date.replace(/-/g,"/");
						}
						tTime = tTime[1].split(/Z|\./);
						date += " " + tTime[0];
					}
					return date;
				}catch (e) {
					// print(e.message);
					// print(data);
					return ''
				}
			},
H
测试  
hjdhnx 已提交
278 279 280 281 282 283 284 285 286 287 288 289
	}
	}
	);
	print('init执行完毕');
}

function home(filter) {
	let classes = Object.keys(__drives).map(key => ({
		type_id: `${key}$${__drives[key].startPage}`,
		type_name: key,
		type_flag: '1',
	}));
H
hjdhnx 已提交
290
	let filter_dict = {};
H
hjdhnx 已提交
291
	let filters = [{'key': 'order', 'name': '排序', 'value': [{'n': '名称⬆️', 'v': 'vod_name_asc'}, {'n': '名称⬇️', 'v': 'vod_name_desc'},
H
hjdhnx 已提交
292
			{'n': '中英⬆️', 'v': 'vod_cn_asc'}, {'n': '中英⬇️', 'v': 'vod_cn_desc'},
H
hjdhnx 已提交
293
			{'n': '时间⬆️', 'v': 'vod_time_asc'}, {'n': '时间⬇️', 'v': 'vod_time_desc'},
H
hjdhnx 已提交
294
			{'n': '大小⬆️', 'v': 'vod_size_asc'}, {'n': '大小⬇️', 'v': 'vod_size_desc'},{'n': '', 'v': 'none'}]},
H
hjdhnx 已提交
295 296
			{'key': 'show', 'name': '播放展示', 'value': [{'n': '单集', 'v': 'single'},{'n': '全集', 'v': 'all'}]}
	];
H
hjdhnx 已提交
297 298 299
	classes.forEach(it=>{
		filter_dict[it.type_id] = filters;
	});
H
测试  
hjdhnx 已提交
300 301
	print("----home----");
	print(classes);
H
hjdhnx 已提交
302
	return JSON.stringify({ 'class': classes,'filters': filter_dict});
H
测试  
hjdhnx 已提交
303 304 305
}

function homeVod(params) {
H
hjdhnx 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	let _post_data = {"pageNum":0,"pageSize":100};
	let _post_url = 'https://pbaccess.video.qq.com/trpc.videosearch.hot_rank.HotRankServantHttp/HotRankHttp';
	let data = http.post(_post_url,{ data: _post_data }).json();
	let _list = [];
	try {
		data = data['data']['navItemList'][0]['hotRankResult']['rankItemList'];
		// print(data);
		data.forEach(it=>{
			_list.push({
				vod_name:it.title,
				vod_id:'msearch:'+it.title,
				vod_pic:'https://avatars.githubusercontent.com/u/97389433?s=120&v=4',
				vod_remarks:it.changeOrder,
			});
		});
	}catch (e) {
		print('Alist获取首页推荐发送错误:'+e.message);
	}
	return JSON.stringify({ 'list': _list });
H
测试  
hjdhnx 已提交
325 326 327
}

function category(tid, pg, filter, extend) {
H
hjdhnx 已提交
328 329 330
	let orid = tid.replace(/#all#|#search#/g,'');
	let { drives, path } = get_drives_path(orid);
	const id = orid.endsWith('/') ? orid : orid + '/';
H
测试  
hjdhnx 已提交
331 332 333 334
	const list = drives.getPath(path);
	let subList = [];
	let vodFiles = [];
	let allList = [];
H
hjdhnx 已提交
335 336 337 338
	let fl = filter?extend:{};
	if(fl.show){
		showMode = fl.show;
	}
H
测试  
hjdhnx 已提交
339 340 341 342 343 344 345
	list.forEach(item => {
		if (drives.is_subt(item)) {
			subList.push(item.name);
		}
		if (!drives.showAll && !drives.isFolder(item) && !drives.isVideo(item)) {
			return //只显示视频文件和文件夹
		}
H
hjdhnx 已提交
346 347 348
		let vod_time = drives.getTime(item);
		let vod_size = get_size(item.size);
		let remark = vod_time.split(' ')[0].substr(3)+'\t'+vod_size;
H
hjdhnx 已提交
349 350 351 352 353
		let vod_id = id + item.name + (drives.isFolder(item) ? '/' : '');
		if(showMode==='all'){
			vod_id+='#all#';
		}
		print(vod_id);
H
测试  
hjdhnx 已提交
354
		const vod = {
H
hjdhnx 已提交
355
			'vod_id': vod_id,
H
测试  
hjdhnx 已提交
356 357
			'vod_name': item.name.replaceAll("$", "").replaceAll("#", ""),
			'vod_pic': drives.getPic(item),
H
hjdhnx 已提交
358 359
			'vod_time':vod_time ,
			'vod_size':item.size ,
H
测试  
hjdhnx 已提交
360 361
			'vod_tag': drives.isFolder(item) ? 'folder' : 'file',
			'vod_remarks': drives.isFolder(item) ? remark + ' 文件夹' : remark
H
hjdhnx 已提交
362
		};
H
测试  
hjdhnx 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
		if (drives.isVideo(item)) {
			vodFiles.push(vod);
		}
		allList.push(vod);
	});

	if (vodFiles.length === 1 && subList.length > 0) { //只有一个视频 一个或者多个字幕 取相似度最高的
		// let sub = subList.length === 1 ? subList[0] : _.chain(allList).sortBy(x => (x.includes('chs') ? 100 : 0) + levenshteinDistance(x, vodFiles[0].vod_name)).last().value();
		let sub; // 字幕文件名称
		if(subList.length === 1){
			sub = subList[0];
		}else {
			let subs = JSON.parse(JSON.stringify(subList));
			subs.sort((a,b)=>{
				// chs是简体中文字幕
				let a_similar = (a.includes('chs') ? 100 : 0) + levenshteinDistance(a, vodFiles[0].vod_name);
				let b_similar = (b.includes('chs') ? 100 : 0) + levenshteinDistance(b, vodFiles[0].vod_name);
				if(a_similar>b_similar) { // 按相似度正序排列
					return 1;
				}else{ //否则,位置不变
					return -1;
				}
			});
			sub = subs.slice(-1)[0];
		}
		vodFiles[0].vod_id += "@@@" + sub;
H
hjdhnx 已提交
389 390
		// vodFiles[0].vod_remarks += " 有字幕";
		vodFiles[0].vod_remarks += "🏷️";
H
测试  
hjdhnx 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403
	} else {
		vodFiles.forEach(item => {
			const lh = 0;
			let sub;
			subList.forEach(s => {
				//编辑距离相似度
				const l = levenshteinDistance(s, item.vod_name);
				if (l > 60 && l > lh) {
					sub = s;
				}
			});
			if (sub) {
				item.vod_id += "@@@" + sub;
H
hjdhnx 已提交
404 405
				// item.vod_remarks += " 有字幕";
				item.vod_remarks += "🏷️";
H
测试  
hjdhnx 已提交
406 407 408
			}
		});
	}
H
hjdhnx 已提交
409

H
hjdhnx 已提交
410 411 412 413 414
	if(fl.order){
		// print(fl.order);
		let key = fl.order.split('_').slice(0,-1).join('_');
		let order = fl.order.split('_').slice(-1)[0];
		print(`排序key:${key},排序order:${order}`);
H
hjdhnx 已提交
415
		if(key.includes('name')){
H
hjdhnx 已提交
416
			detail_order = 'name';
H
hjdhnx 已提交
417
			allList = sortListByName(allList,key,order);
H
hjdhnx 已提交
418 419 420
		}else if(key.includes('cn')){
			detail_order = 'cn';
			allList = sortListByCN(allList,'vod_name',order);
H
hjdhnx 已提交
421
		}else if(key.includes('time')){
H
hjdhnx 已提交
422
			detail_order = 'time';
H
hjdhnx 已提交
423 424
			allList = sortListByTime(allList,key,order);
		}else if(key.includes('size')){
H
hjdhnx 已提交
425
			detail_order = 'size';
H
hjdhnx 已提交
426
			allList = sortListBySize(allList,key,order);
H
hjdhnx 已提交
427 428 429
		}else if(fl.order.includes('none')){
			detail_order = 'none';
			print('不排序');
H
hjdhnx 已提交
430
		}
H
hjdhnx 已提交
431
	}else{
H
hjdhnx 已提交
432 433 434 435
		// 没传order是其他地方调用的,自动按名称正序排序方便追剧,如果传了none进去就不排序,假装云盘里本身文件顺序是正常的
		if(detail_order!=='none'){
			allList = sortListByName(allList,'vod_name','asc');
		}
H
hjdhnx 已提交
436
	}
H
hjdhnx 已提交
437

H
hjdhnx 已提交
438
	print("----category----"+`tid:${tid},detail_order:${detail_order},showMode:${showMode}`);
H
hjdhnx 已提交
439
	// print(allList);
H
测试  
hjdhnx 已提交
440 441 442 443 444 445 446 447 448
	return JSON.stringify({
		'page': 1,
		'pagecount': 1,
		'limit': allList.length,
		'total': allList.length,
		'list': allList,
	});
}

H
hjdhnx 已提交
449
function getAll(otid,tid,drives,path){
H
hjdhnx 已提交
450
	try {
H
测试  
hjdhnx 已提交
451
		const content = category(tid, null, false, null);
H
hjdhnx 已提交
452
		const isFile = isMedia(otid.replace(/#all#|#search#/g,'').split('@@@')[0]);
H
测试  
hjdhnx 已提交
453 454 455 456
		const { list } = JSON.parse(content);
		let vod_play_url = [];
		list.forEach(x => {
			if (x.vod_tag === 'file'){
H
hjdhnx 已提交
457 458
				let vid = x.vod_id.replace(/#all#|#search#/g,'');
				vod_play_url.push(`${x.vod_name}$${vid.substring(vid.indexOf('$') + 1)}`);
H
测试  
hjdhnx 已提交
459 460
			}
		});
H
hjdhnx 已提交
461 462 463 464 465 466 467 468
		const pl = path.split("/").filter(it=>it);
		let vod_name = pl[pl.length - 1] || drives.name;
		if(vod_name === drives.name){
			print(pl);
		}
		if(otid.includes('#search#')){
			vod_name+='[搜]';
		}
H
测试  
hjdhnx 已提交
469
		let vod = {
H
hjdhnx 已提交
470 471
			// vod_id: tid,
			vod_id: otid,
H
测试  
hjdhnx 已提交
472 473 474 475 476 477 478 479 480 481 482 483
			vod_name: vod_name,
			type_name: "文件夹",
			vod_pic: "https://avatars.githubusercontent.com/u/97389433?s=120&v=4",
			vod_content: tid,
			vod_tag: 'folder',
			vod_play_from: drives.name,
			vod_play_url: vod_play_url.join('#'),
			vod_remarks: drives.settings.title,
		}
		print("----detail1----");
		print(vod);
		return JSON.stringify({ 'list': [vod] });
H
hjdhnx 已提交
484 485
	}catch (e) {
		print(e.message);
H
hjdhnx 已提交
486 487
		let list = [{vod_name:'无数据,防无限请求',type_name: "文件夹",vod_id:'no_data',vod_remarks:'不要点,会崩的',vod_pic:'https://ghproxy.com/https://raw.githubusercontent.com/hjdhnx/dr_py/main/404.jpg',vod_actor:e.message,vod_director: tid,vod_content: otid}];
		return JSON.stringify({ 'list': list });
H
hjdhnx 已提交
488 489 490 491
	}
}

function detail(tid) {
H
hjdhnx 已提交
492 493
	let isSearch = tid.includes('#search#');
	let isAll = tid.includes('#all#');
H
hjdhnx 已提交
494
	let otid = tid;
H
hjdhnx 已提交
495
	tid = tid.replace(/#all#|#search#/g,'');
H
hjdhnx 已提交
496 497
	let isFile = isMedia(tid.split('@@@')[0]);
	print(`isFile:${tid}?${isFile}`);
H
hjdhnx 已提交
498
	let { drives, path } = get_drives_path(tid);
H
hjdhnx 已提交
499
	print(`drives:${drives},path:${path},`);
H
hjdhnx 已提交
500
	if (path.endsWith("/")) { //长按文件夹可以 加载里面全部视频到详情
H
hjdhnx 已提交
501
		return getAll(otid,tid,drives,path);
H
测试  
hjdhnx 已提交
502
	} else {
H
hjdhnx 已提交
503
		if(isSearch&&!isFile){ // 搜索结果 当前目录获取所有文件
H
hjdhnx 已提交
504
			return getAll(otid,tid,drives,path);
H
hjdhnx 已提交
505 506 507 508 509 510 511 512
		}else if(isAll){ // 上级目录获取所有文件  不管是搜索还是分类,只要不是 搜索到的文件夹,且展示模式为全部,都获取上级目录的所有文件
			// 是文件就取上级目录
			let new_tid;
			if(isFile){
				new_tid = tid.split('/').slice(0,-1).join('/')+'/';
			}else{
				new_tid = tid;
			}
H
hjdhnx 已提交
513 514 515
			print(`全集模式 tid:${tid}=>tid:${new_tid}`);
			let { drives, path } = get_drives_path(new_tid);
			return getAll(otid,new_tid,drives,path);
H
hjdhnx 已提交
516
		} else if(isFile){ // 单文件进入
H
hjdhnx 已提交
517 518
			let paths = path.split("@@@");
			let vod_name = paths[0].substring(paths[0].lastIndexOf("/") + 1);
H
hjdhnx 已提交
519
			let vod_title = vod_name;
H
hjdhnx 已提交
520
			if(otid.includes('#search#')){
H
hjdhnx 已提交
521
				vod_title+='[搜]';
H
hjdhnx 已提交
522
			}
H
hjdhnx 已提交
523
			let vod = {
H
hjdhnx 已提交
524
				vod_id: otid,
H
hjdhnx 已提交
525
				vod_name: vod_title,
H
hjdhnx 已提交
526 527 528 529 530 531 532 533 534 535 536 537
				type_name: "文件",
				vod_pic: "https://avatars.githubusercontent.com/u/97389433?s=120&v=4",
				vod_content: tid,
				vod_play_from: drives.name,
				vod_play_url: vod_name + "$" + path,
				vod_remarks: drives.settings.title,
			};
			print("----detail2----");
			print(vod);
			return JSON.stringify({
				'list': [vod]
			});
H
hjdhnx 已提交
538 539 540 541
		}else{
			return JSON.stringify({
				'list': []
			});
H
hjdhnx 已提交
542
		}
H
测试  
hjdhnx 已提交
543 544 545 546 547 548 549 550 551
	}
}

function play(flag, id, flags) {
	const drives = get_drives(flag);
	const urls = id.split("@@@"); // @@@ 分割前是 相对文件path,分割后是字幕文件
	let vod = {
		'parse': 0,
		'playUrl': '',
H
hjdhnx 已提交
552
		// 'url': drives.getFile(urls[0]).raw_url+'#.m3u8' // 加 # 没法播放
H
测试  
hjdhnx 已提交
553 554 555 556
		'url': drives.getFile(urls[0]).raw_url
	};
	if (urls.length >= 2) {
		const path = urls[0].substring(0, urls[0].lastIndexOf('/') + 1);
H
hjdhnx 已提交
557
		vod.subt = drives.getFile(path + urls[1]).raw_url1;
H
测试  
hjdhnx 已提交
558 559 560 561 562 563 564
	}
	print("----play----");
	print(vod);
	return JSON.stringify(vod);
}

function search(wd, quick) {
H
hjdhnx 已提交
565 566
	print(__drives);
	print('可搜索的alist驱动:'+searchDriver);
H
hjdhnx 已提交
567
	if(!searchDriver||!wd){
H
hjdhnx 已提交
568 569 570 571 572
		return JSON.stringify({
			'list': []
		});
	}else{
		let driver = __drives[searchDriver];
H
hjdhnx 已提交
573
		wd = wd.split(' ').filter(it=>it.trim()).join('+');
H
hjdhnx 已提交
574
		print(driver);
H
hjdhnx 已提交
575 576 577 578 579 580
		let surl = driver.server + '/search?box='+wd+'&url=';
		if(search_type){
			surl+='&type='+search_type;
		}
		print('搜索链接:'+surl);
		let html = http.get(surl).text();
H
hjdhnx 已提交
581 582 583 584
		let lists = [];
		try {
			lists = pdfa(html,'div&&ul&&a');
		}catch (e) {}
H
hjdhnx 已提交
585
		print(`搜索结果数:${lists.length},搜索结果显示数量限制:${limit_search_show}`);
H
hjdhnx 已提交
586
		let vods = [];
H
hjdhnx 已提交
587
		let excludeReg = /\.(pdf|epub|mobi|txt|doc|lrc)$/; // 过滤后缀文件
H
hjdhnx 已提交
588
		let cnt = 0;
H
hjdhnx 已提交
589
		lists.forEach(it=>{
H
hjdhnx 已提交
590 591 592 593 594 595 596
			let vhref = pdfh(it,'a&&href');
			if(vhref){
				vhref = unescape(vhref);
			}
			if(excludeReg.test(vhref)){
				return; //跳过本次循环
			}
H
hjdhnx 已提交
597 598 599 600
			if(cnt < limit_search_show){
				print(vhref);
			}
			cnt ++;
H
hjdhnx 已提交
601
			let vid = searchDriver+'$'+vhref+'#search#';
H
hjdhnx 已提交
602 603 604
			if(showMode==='all'){
				vid+='#all#';
			}
H
hjdhnx 已提交
605 606 607
			vods.push({
				vod_name:pdfh(it,'a&&Text'),
				vod_id:vid,
H
hjdhnx 已提交
608
				vod_tag: isMedia(vhref) ? 'file' : 'folder',
H
hjdhnx 已提交
609 610 611 612
				vod_pic:'http://img1.3png.com/281e284a670865a71d91515866552b5f172b.png',
				vod_remarks:searchDriver
			});
		});
H
hjdhnx 已提交
613 614
		// 截取搜索结果
		vods = vods.slice(0,limit_search_show);
H
hjdhnx 已提交
615 616 617 618 619
		print(vods);
		return JSON.stringify({
			'list': vods
		});
	}
H
测试  
hjdhnx 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
}

function get_size(sz) {
	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 if( sz > 1024.0){
		sz /= 1024.0;
		filesize = "KB";
	}else{
		filesize = "B";
	}
	// 转成字符串
	let sizeStr = sz.toFixed(2) + filesize,
	// 获取小数点处的索引
	index = sizeStr.indexOf("."),
	// 获取小数点后两位的值
	dou = sizeStr.substr(index + 1, 2);
	if (dou === "00") {
		return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2);
	}else{
		return sizeStr;
	}
}

H
hjdhnx 已提交
655
// 相似度获取
H
测试  
hjdhnx 已提交
656 657 658 659
function levenshteinDistance(str1, str2) {
    return 100 - 100 * distance(str1, str2) / Math.max(str1.length, str2.length);
}

H
hjdhnx 已提交
660 661 662 663 664 665 666 667
/**
 * 自然排序
 * ["第1集","第10集","第20集","第2集","1","2","10","12","23","01","02"].sort(naturalSort())
 * @param options {{key,caseSensitive, order: string}}
 */
function naturalSort(options) {
	if (!options) {
		options = {};
H
hjdhnx 已提交
668 669
	}

H
hjdhnx 已提交
670 671 672 673
	return function (a, b) {
		if(options.key){
			a = a[options.key];
			b = b[options.key];
H
hjdhnx 已提交
674
		}
H
hjdhnx 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		var EQUAL = 0;
		var GREATER = (options.order === 'desc' ?
				-1 :
				1
		);
		var SMALLER = -GREATER;

		var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi;
		var sre = /(^[ ]*|[ ]*$)/g;
		var dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/;
		var hre = /^0x[0-9a-f]+$/i;
		var ore = /^0/;

		var normalize = function normalize(value) {
			var string = '' + value;
			return (options.caseSensitive ?
					string :
					string.toLowerCase()
			);
		};
H
hjdhnx 已提交
695

H
hjdhnx 已提交
696 697 698
		// Normalize values to strings
		var x = normalize(a).replace(sre, '') || '';
		var y = normalize(b).replace(sre, '') || '';
H
hjdhnx 已提交
699

H
hjdhnx 已提交
700 701 702
		// chunk/tokenize
		var xN = x.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0');
		var yN = y.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0');
H
hjdhnx 已提交
703

H
hjdhnx 已提交
704 705 706 707
		// Return immediately if at least one of the values is empty.
		if (!x && !y) return EQUAL;
		if (!x && y) return GREATER;
		if (x && !y) return SMALLER;
H
hjdhnx 已提交
708

H
hjdhnx 已提交
709 710 711 712
		// numeric, hex or date detection
		var xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x));
		var yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null;
		var oFxNcL, oFyNcL;
H
hjdhnx 已提交
713

H
hjdhnx 已提交
714 715 716 717 718
		// first try and sort Hex codes or Dates
		if (yD) {
			if (xD < yD) return SMALLER;
			else if (xD > yD) return GREATER;
		}
H
hjdhnx 已提交
719

H
hjdhnx 已提交
720 721
		// natural sorting through split numeric strings and default strings
		for (var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
H
hjdhnx 已提交
722

H
hjdhnx 已提交
723 724 725
			// find floats not starting with '0', string or 0 if not defined (Clint Priest)
			oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
			oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
H
hjdhnx 已提交
726

H
hjdhnx 已提交
727 728
			// handle numeric vs string comparison - number < string - (Kyle Adams)
			if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? GREATER : SMALLER;
H
hjdhnx 已提交
729

H
hjdhnx 已提交
730 731 732 733
			// rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
			else if (typeof oFxNcL !== typeof oFyNcL) {
				oFxNcL += '';
				oFyNcL += '';
H
hjdhnx 已提交
734
			}
H
hjdhnx 已提交
735 736
			if (oFxNcL < oFyNcL) return SMALLER;
			if (oFxNcL > oFyNcL) return GREATER;
H
hjdhnx 已提交
737
		}
H
hjdhnx 已提交
738 739 740 741 742 743 744
		return EQUAL;
	};
}
// 完整名称排序
const sortListByName = (vodList,key,order) => {
	if(!key){
		return vodList
H
hjdhnx 已提交
745
	}
H
hjdhnx 已提交
746 747 748
	order = order||'asc'; // 默认正序
	// 排序键,顺序,区分大小写
	return vodList.sort(naturalSort({key: key, order: order,caseSensitive:true}))
H
hjdhnx 已提交
749
};
H
测试  
hjdhnx 已提交
750

H
hjdhnx 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
const getTimeInt = (timeStr) => {
	return (new Date(timeStr)).getTime();
};

// 时间
const sortListByTime = (vodList,key,order) => {
	if (!key) {
		return vodList
	}
	let ASCarr = vodList.sort((a, b) => {
		a = a[key];
		b = b[key];
		return getTimeInt(a) - getTimeInt(b);
	});
	if(order==='desc'){
		ASCarr.reverse();
	}
	return ASCarr
};

// 大小
const sortListBySize = (vodList,key,order) => {
	if (!key) {
		return vodList
	}
	let ASCarr = vodList.sort((a, b) => {
		a = a[key];
		b = b[key];
		return (Number(a) || 0) - (Number(b) || 0);
	});
	if(order==='desc'){
		ASCarr.reverse();
	}
	return ASCarr
};

H
测试  
hjdhnx 已提交
787 788 789 790 791 792 793 794 795 796
// 导出函数对象
export default {
	init: init,
	home: home,
	homeVod: homeVod,
	category: category,
	detail: detail,
	play: play,
	search: search
}