alist.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. // import _ from 'https://underscorejs.org/underscore-esm-min.js'
  2. // import {distance} from 'https://unpkg.com/fastest-levenshtein@1.0.16/esm/mod.js'
  3. import {distance} from './mod.js'
  4. import {sortListByCN} from './sortName.js'
  5. /**
  6. * alist js
  7. * 配置设置 {"key":"Alist","name":"Alist","type":3,"api":"http://xxx.com/alist.js","searchable":0,"quickSearch":0,"filterable":0,"ext":"http://xxx.com/alist.json"}
  8. * alist.json [{
  9. name:'名称',
  10. server:'地址',
  11. startPage:'/', //启动文件夹
  12. showAll: false , //是否显示全部文件,默认false只显示 音视频和文件夹
  13. search: true, // 启用小雅的搜索,搜索只会搜第一个开启此开关的磁盘
  14. params:{ //对应文件夹参数 如设置对应文件夹的密码
  15. '/abc':{ password : '123' },
  16. '/abc/abc':{ password : '123' },
  17. }
  18. }]
  19. * 提示 想要加载文件夹里面全部视频到详情(看剧可以自动播放下一集支持历史记录)
  20. * 需要改软件才能支持,,建议长按文件夹时添加判断 tag == folder 时跳转 DetailActivity
  21. */
  22. String.prototype.rstrip = function (chars) {
  23. let regex = new RegExp(chars + "$");
  24. return this.replace(regex, "");
  25. };
  26. var showMode = 'single';
  27. var searchDriver = '';
  28. var limit_search_show = 200;
  29. var search_type = '';
  30. var detail_order = 'name';
  31. var playRaw = 1; // 播放直链获取,默认0直接拼接/d 填1可以获取阿里oss链接。注意,有时效性
  32. const request_timeout = 5000;
  33. const VERSION = 'alist v2/v3 20221223';
  34. const UA = 'Mozilla/5.0'; //默认请求ua
  35. /**
  36. * 打印日志
  37. * @param any 任意变量
  38. */
  39. function print(any){
  40. any = any||'';
  41. if(typeof(any)=='object'&&Object.keys(any).length>0){
  42. try {
  43. any = JSON.stringify(any);
  44. console.log(any);
  45. }catch (e) {
  46. // console.log('print:'+e.message);
  47. console.log(typeof(any)+':'+any.length);
  48. }
  49. }else if(typeof(any)=='object'&&Object.keys(any).length<1){
  50. console.log('null object');
  51. }else{
  52. console.log(any);
  53. }
  54. }
  55. /*** js自封装的方法 ***/
  56. /**
  57. * 获取链接的host(带http协议的完整链接)
  58. * @param url 任意一个正常完整的Url,自动提取根
  59. * @returns {string}
  60. */
  61. function getHome(url){
  62. if(!url){
  63. return ''
  64. }
  65. let tmp = url.split('//');
  66. url = tmp[0] + '//' + tmp[1].split('/')[0];
  67. try {
  68. url = decodeURIComponent(url);
  69. }catch (e) {}
  70. return url
  71. }
  72. const http = function (url, options = {}) {
  73. if(options.method ==='POST' && options.data){
  74. options.body = JSON.stringify(options.data);
  75. options.headers = Object.assign({'content-type':'application/json'}, options.headers);
  76. }
  77. options.timeout = request_timeout;
  78. if(!options.headers){
  79. options.headers = {};
  80. }
  81. let keys = Object.keys(options.headers).map(it=>it.toLowerCase());
  82. if(!keys.includes('referer')){
  83. options.headers['Referer'] = getHome(url);
  84. }
  85. if(!keys.includes('user-agent')){
  86. options.headers['User-Agent'] = UA;
  87. }
  88. try {
  89. const res = req(url, options);
  90. res.json = () => res&&res.content ? JSON.parse(res.content) : null;
  91. res.text = () => res&&res.content ? res.content:'';
  92. return res
  93. }catch (e) {
  94. return {
  95. json() {
  96. return null
  97. }, text() {
  98. return ''
  99. }
  100. }
  101. }
  102. };
  103. ["get", "post"].forEach(method => {
  104. http[method] = function (url, options = {}) {
  105. return http(url, Object.assign(options, {method: method.toUpperCase()}));
  106. }
  107. });
  108. const __drives = {};
  109. function isMedia(file){
  110. 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());
  111. }
  112. function get_drives_path(tid) {
  113. const index = tid.indexOf('$');
  114. const name = tid.substring(0, index);
  115. const path = tid.substring(index + 1);
  116. return { drives: get_drives(name), path };
  117. }
  118. function get_drives(name) {
  119. const { settings, api, server } = __drives[name];
  120. if (settings.v3 == null) { //获取 设置
  121. settings.v3 = false;
  122. const data = http.get(server + '/api/public/settings').json().data;
  123. if (Array.isArray(data)) {
  124. settings.title = data.find(x => x.key === 'title')?.value;
  125. settings.v3 = false;
  126. settings.version = data.find(x => x.key === 'version')?.value;
  127. settings.enableSearch = data.find(x => x.key === 'enable search')?.value === 'true';
  128. } else {
  129. settings.title = data.title;
  130. settings.v3 = true;
  131. settings.version = data.version;
  132. settings.enableSearch = false; //v3 没有找到 搜索配置
  133. }
  134. //不同版本 接口不一样
  135. api.path = settings.v3 ? '/api/fs/list' : '/api/public/path';
  136. api.file = settings.v3 ? '/api/fs/get' : '/api/public/path';
  137. api.search = settings.v3 ? '/api/public/search' : '/api/public/search';
  138. }
  139. return __drives[name]
  140. }
  141. function init(ext) {
  142. console.log("当前版本号:"+VERSION);
  143. let data;
  144. if (typeof ext == 'object'){
  145. data = ext;
  146. print('alist ext:object');
  147. } else if (typeof ext == 'string') {
  148. if (ext.startsWith('http')) {
  149. let alist_data = ext.split(';');
  150. let alist_data_url = alist_data[0];
  151. limit_search_show = alist_data.length>1?Number(alist_data[1])||limit_search_show:limit_search_show;
  152. search_type = alist_data.length>2?alist_data[2]:search_type;
  153. print(alist_data_url);
  154. data = http.get(alist_data_url).json(); // .map(it=>{it.name='🙋丫仙女';return it})
  155. } else {
  156. print('alist ext:json string');
  157. data = JSON.parse(ext);
  158. }
  159. }
  160. // print(data); // 测试证明壳子标题支持emoji,是http请求源码不支持emoji
  161. let drives = [];
  162. if(Array.isArray(data) && data.length > 0 && data[0].hasOwnProperty('server') && data[0].hasOwnProperty('name')){
  163. drives = data;
  164. }else if(!Array.isArray(data)&&data.hasOwnProperty('drives')&&Array.isArray(data.drives)){
  165. drives = data.drives.filter(it=>(it.type&&it.type==='alist')||!it.type);
  166. }
  167. print(drives);
  168. searchDriver = (drives.find(x=>x.search)||{}).name||'';
  169. if(!searchDriver && drives.length > 0){
  170. searchDriver = drives[0].name;
  171. }
  172. print(searchDriver);
  173. drives.forEach(item => {
  174. let _path_param = [];
  175. if(item.params){
  176. _path_param = Object.keys(item.params);
  177. // 升序排列
  178. _path_param.sort((a,b)=>(a.length-b.length));
  179. }
  180. if(item.password){
  181. let pwdObj = {
  182. password: item.password
  183. };
  184. if(!item.params){
  185. item.params = {'/':pwdObj};
  186. }else{
  187. item.params['/'] = pwdObj;
  188. }
  189. _path_param.unshift('/');
  190. }
  191. __drives[item.name] = {
  192. name: item.name,
  193. server: item.server.endsWith("/") ? item.server.rstrip("/") : item.server,
  194. startPage: item.startPage || '/', //首页
  195. showAll: item.showAll === true, //默认只显示 视频和文件夹,如果想显示全部 showAll 设置true
  196. search: !!item.search, //是否支持搜索,只有小丫的可以,多个可搜索只取最前面的一个
  197. params: item.params || {},
  198. _path_param: _path_param,
  199. settings: {},
  200. api: {},
  201. getParams(path) {
  202. const key = this._path_param.find(x => path.startsWith(x));
  203. return Object.assign({}, this.params[key], { path });
  204. },
  205. getPath(path) {
  206. const res = http.post(this.server + this.api.path, { data: this.getParams(path) }).json();
  207. return this.settings.v3 ? res.data.content : res.data.files
  208. },
  209. getFile(path) {
  210. let raw_url = this.server+'/d'+path;
  211. raw_url = encodeURI(raw_url);
  212. let data = {raw_url:raw_url,raw_url1:raw_url};
  213. if(playRaw===1){
  214. try {
  215. const res = http.post(this.server + this.api.file, { data: this.getParams(path) }).json();
  216. data = this.settings.v3 ? res.data : res.data.files[0];
  217. if (!this.settings.v3) {
  218. data.raw_url = data.url; //v2 的url和v3不一样
  219. }
  220. data.raw_url1 = raw_url;
  221. return data
  222. }catch (e) {
  223. return data
  224. }
  225. }else{
  226. return data
  227. }
  228. },
  229. isFolder(data) { return data.type === 1 },
  230. isVideo(data) { //判断是否是 视频文件
  231. // return this.settings.v3 ? data.type === 2 : data.type === 3
  232. // 增加音乐识别 视频,其他,音频
  233. return this.settings.v3 ? (data.type === 2||data.type===0||data.type===3) : (data.type === 3||data.type===0||data.type === 4)
  234. },
  235. is_subt(data) {
  236. if (data.type === 1) {
  237. return false;
  238. }
  239. const ext = /\.(srt|ass|scc|stl|ttml)$/; // [".srt", ".ass", ".scc", ".stl", ".ttml"];
  240. // return ext.some(x => data.name.endsWith(x));
  241. return ext.test(data.name);
  242. },
  243. getPic(data) {
  244. let pic = this.settings.v3 ? data.thumb : data.thumbnail;
  245. return pic || (this.isFolder(data) ? "http://img1.3png.com/281e284a670865a71d91515866552b5f172b.png" : '');
  246. },
  247. getTime(data,isStandard) {
  248. isStandard = isStandard||false;
  249. try {
  250. let tTime = data.updated_at || data.time_str || data.modified || "";
  251. let date = '';
  252. if(tTime){
  253. tTime = tTime.split("T");
  254. date = tTime[0];
  255. if(isStandard){
  256. date = date.replace(/-/g,"/");
  257. }
  258. tTime = tTime[1].split(/Z|\./);
  259. date += " " + tTime[0];
  260. }
  261. return date;
  262. }catch (e) {
  263. // print(e.message);
  264. // print(data);
  265. return ''
  266. }
  267. },
  268. }
  269. }
  270. );
  271. print('init执行完毕');
  272. }
  273. function home(filter) {
  274. let classes = Object.keys(__drives).map(key => ({
  275. type_id: `${key}$${__drives[key].startPage}`,
  276. type_name: key,
  277. type_flag: '1',
  278. }));
  279. let filter_dict = {};
  280. let filters = [{'key': 'order', 'name': '排序', 'value': [{'n': '名称⬆️', 'v': 'vod_name_asc'}, {'n': '名称⬇️', 'v': 'vod_name_desc'},
  281. {'n': '中英⬆️', 'v': 'vod_cn_asc'}, {'n': '中英⬇️', 'v': 'vod_cn_desc'},
  282. {'n': '时间⬆️', 'v': 'vod_time_asc'}, {'n': '时间⬇️', 'v': 'vod_time_desc'},
  283. {'n': '大小⬆️', 'v': 'vod_size_asc'}, {'n': '大小⬇️', 'v': 'vod_size_desc'},{'n': '无', 'v': 'none'}]},
  284. {'key': 'show', 'name': '播放展示', 'value': [{'n': '单集', 'v': 'single'},{'n': '全集', 'v': 'all'}]}
  285. ];
  286. classes.forEach(it=>{
  287. filter_dict[it.type_id] = filters;
  288. });
  289. print("----home----");
  290. print(classes);
  291. return JSON.stringify({ 'class': classes,'filters': filter_dict});
  292. }
  293. function homeVod(params) {
  294. let _post_data = {"pageNum":0,"pageSize":100};
  295. let _post_url = 'https://pbaccess.video.qq.com/trpc.videosearch.hot_rank.HotRankServantHttp/HotRankHttp';
  296. let data = http.post(_post_url,{ data: _post_data }).json();
  297. let _list = [];
  298. try {
  299. data = data['data']['navItemList'][0]['hotRankResult']['rankItemList'];
  300. // print(data);
  301. data.forEach(it=>{
  302. _list.push({
  303. vod_name:it.title,
  304. vod_id:'msearch:'+it.title,
  305. vod_pic:'https://avatars.githubusercontent.com/u/97389433?s=120&v=4',
  306. vod_remarks:it.changeOrder,
  307. });
  308. });
  309. }catch (e) {
  310. print('Alist获取首页推荐发送错误:'+e.message);
  311. }
  312. return JSON.stringify({ 'list': _list });
  313. }
  314. function category(tid, pg, filter, extend) {
  315. let orid = tid.replace(/#all#|#search#/g,'');
  316. let { drives, path } = get_drives_path(orid);
  317. const id = orid.endsWith('/') ? orid : orid + '/';
  318. const list = drives.getPath(path);
  319. let subList = [];
  320. let vodFiles = [];
  321. let allList = [];
  322. let fl = filter?extend:{};
  323. if(fl.show){
  324. showMode = fl.show;
  325. }
  326. list.forEach(item => {
  327. if (drives.is_subt(item)) {
  328. subList.push(item.name);
  329. }
  330. if (!drives.showAll && !drives.isFolder(item) && !drives.isVideo(item)) {
  331. return //只显示视频文件和文件夹
  332. }
  333. let vod_time = drives.getTime(item);
  334. let vod_size = get_size(item.size);
  335. let remark = vod_time.split(' ')[0].substr(3)+'\t'+vod_size;
  336. let vod_id = id + item.name + (drives.isFolder(item) ? '/' : '');
  337. if(showMode==='all'){
  338. vod_id+='#all#';
  339. }
  340. print(vod_id);
  341. const vod = {
  342. 'vod_id': vod_id,
  343. 'vod_name': item.name.replaceAll("$", "").replaceAll("#", ""),
  344. 'vod_pic': drives.getPic(item),
  345. 'vod_time':vod_time ,
  346. 'vod_size':item.size ,
  347. 'vod_tag': drives.isFolder(item) ? 'folder' : 'file',
  348. 'vod_remarks': drives.isFolder(item) ? remark + ' 文件夹' : remark
  349. };
  350. if (drives.isVideo(item)) {
  351. vodFiles.push(vod);
  352. }
  353. allList.push(vod);
  354. });
  355. if (vodFiles.length === 1 && subList.length > 0) { //只有一个视频 一个或者多个字幕 取相似度最高的
  356. // let sub = subList.length === 1 ? subList[0] : _.chain(allList).sortBy(x => (x.includes('chs') ? 100 : 0) + levenshteinDistance(x, vodFiles[0].vod_name)).last().value();
  357. let sub; // 字幕文件名称
  358. if(subList.length === 1){
  359. sub = subList[0];
  360. }else {
  361. let subs = JSON.parse(JSON.stringify(subList));
  362. subs.sort((a,b)=>{
  363. // chs是简体中文字幕
  364. let a_similar = (a.includes('chs') ? 100 : 0) + levenshteinDistance(a, vodFiles[0].vod_name);
  365. let b_similar = (b.includes('chs') ? 100 : 0) + levenshteinDistance(b, vodFiles[0].vod_name);
  366. if(a_similar>b_similar) { // 按相似度正序排列
  367. return 1;
  368. }else{ //否则,位置不变
  369. return -1;
  370. }
  371. });
  372. sub = subs.slice(-1)[0];
  373. }
  374. vodFiles[0].vod_id += "@@@" + sub;
  375. // vodFiles[0].vod_remarks += " 有字幕";
  376. vodFiles[0].vod_remarks += "🏷️";
  377. } else {
  378. vodFiles.forEach(item => {
  379. const lh = 0;
  380. let sub;
  381. subList.forEach(s => {
  382. //编辑距离相似度
  383. const l = levenshteinDistance(s, item.vod_name);
  384. if (l > 60 && l > lh) {
  385. sub = s;
  386. }
  387. });
  388. if (sub) {
  389. item.vod_id += "@@@" + sub;
  390. // item.vod_remarks += " 有字幕";
  391. item.vod_remarks += "🏷️";
  392. }
  393. });
  394. }
  395. if(fl.order){
  396. // print(fl.order);
  397. let key = fl.order.split('_').slice(0,-1).join('_');
  398. let order = fl.order.split('_').slice(-1)[0];
  399. print(`排序key:${key},排序order:${order}`);
  400. if(key.includes('name')){
  401. detail_order = 'name';
  402. allList = sortListByName(allList,key,order);
  403. }else if(key.includes('cn')){
  404. detail_order = 'cn';
  405. allList = sortListByCN(allList,'vod_name',order);
  406. }else if(key.includes('time')){
  407. detail_order = 'time';
  408. allList = sortListByTime(allList,key,order);
  409. }else if(key.includes('size')){
  410. detail_order = 'size';
  411. allList = sortListBySize(allList,key,order);
  412. }else if(fl.order.includes('none')){
  413. detail_order = 'none';
  414. print('不排序');
  415. }
  416. }else{
  417. // 没传order是其他地方调用的,自动按名称正序排序方便追剧,如果传了none进去就不排序,假装云盘里本身文件顺序是正常的
  418. if(detail_order!=='none'){
  419. allList = sortListByName(allList,'vod_name','asc');
  420. }
  421. }
  422. print("----category----"+`tid:${tid},detail_order:${detail_order},showMode:${showMode}`);
  423. // print(allList);
  424. return JSON.stringify({
  425. 'page': 1,
  426. 'pagecount': 1,
  427. 'limit': allList.length,
  428. 'total': allList.length,
  429. 'list': allList,
  430. });
  431. }
  432. function getAll(otid,tid,drives,path){
  433. try {
  434. const content = category(tid, null, false, null);
  435. const isFile = isMedia(otid.replace(/#all#|#search#/g,'').split('@@@')[0]);
  436. const { list } = JSON.parse(content);
  437. let vod_play_url = [];
  438. list.forEach(x => {
  439. if (x.vod_tag === 'file'){
  440. let vid = x.vod_id.replace(/#all#|#search#/g,'');
  441. vod_play_url.push(`${x.vod_name}$${vid.substring(vid.indexOf('$') + 1)}`);
  442. }
  443. });
  444. const pl = path.split("/").filter(it=>it);
  445. let vod_name = pl[pl.length - 1] || drives.name;
  446. if(vod_name === drives.name){
  447. print(pl);
  448. }
  449. if(otid.includes('#search#')){
  450. vod_name+='[搜]';
  451. }
  452. let vod = {
  453. // vod_id: tid,
  454. vod_id: otid,
  455. vod_name: vod_name,
  456. type_name: "文件夹",
  457. vod_pic: "https://avatars.githubusercontent.com/u/97389433?s=120&v=4",
  458. vod_content: tid,
  459. vod_tag: 'folder',
  460. vod_play_from: drives.name,
  461. vod_play_url: vod_play_url.join('#'),
  462. vod_remarks: drives.settings.title,
  463. }
  464. print("----detail1----");
  465. print(vod);
  466. return JSON.stringify({ 'list': [vod] });
  467. }catch (e) {
  468. print(e.message);
  469. 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}];
  470. return JSON.stringify({ 'list': list });
  471. }
  472. }
  473. function detail(tid) {
  474. let isSearch = tid.includes('#search#');
  475. let isAll = tid.includes('#all#');
  476. let otid = tid;
  477. tid = tid.replace(/#all#|#search#/g,'');
  478. let isFile = isMedia(tid.split('@@@')[0]);
  479. print(`isFile:${tid}?${isFile}`);
  480. let { drives, path } = get_drives_path(tid);
  481. print(`drives:${drives},path:${path},`);
  482. if (path.endsWith("/")) { //长按文件夹可以 加载里面全部视频到详情
  483. return getAll(otid,tid,drives,path);
  484. } else {
  485. if(isSearch&&!isFile){ // 搜索结果 当前目录获取所有文件
  486. return getAll(otid,tid,drives,path);
  487. }else if(isAll){ // 上级目录获取所有文件 不管是搜索还是分类,只要不是 搜索到的文件夹,且展示模式为全部,都获取上级目录的所有文件
  488. // 是文件就取上级目录
  489. let new_tid;
  490. if(isFile){
  491. new_tid = tid.split('/').slice(0,-1).join('/')+'/';
  492. }else{
  493. new_tid = tid;
  494. }
  495. print(`全集模式 tid:${tid}=>tid:${new_tid}`);
  496. let { drives, path } = get_drives_path(new_tid);
  497. return getAll(otid,new_tid,drives,path);
  498. } else if(isFile){ // 单文件进入
  499. let paths = path.split("@@@");
  500. let vod_name = paths[0].substring(paths[0].lastIndexOf("/") + 1);
  501. let vod_title = vod_name;
  502. if(otid.includes('#search#')){
  503. vod_title+='[搜]';
  504. }
  505. let vod = {
  506. vod_id: otid,
  507. vod_name: vod_title,
  508. type_name: "文件",
  509. vod_pic: "https://avatars.githubusercontent.com/u/97389433?s=120&v=4",
  510. vod_content: tid,
  511. vod_play_from: drives.name,
  512. vod_play_url: vod_name + "$" + path,
  513. vod_remarks: drives.settings.title,
  514. };
  515. print("----detail2----");
  516. print(vod);
  517. return JSON.stringify({
  518. 'list': [vod]
  519. });
  520. }else{
  521. return JSON.stringify({
  522. 'list': []
  523. });
  524. }
  525. }
  526. }
  527. function play(flag, id, flags) {
  528. const drives = get_drives(flag);
  529. const urls = id.split("@@@"); // @@@ 分割前是 相对文件path,分割后是字幕文件
  530. let vod = {
  531. 'parse': 0,
  532. 'playUrl': '',
  533. // 'url': drives.getFile(urls[0]).raw_url+'#.m3u8' // 加 # 没法播放
  534. 'url': drives.getFile(urls[0]).raw_url
  535. };
  536. if (urls.length >= 2) {
  537. const path = urls[0].substring(0, urls[0].lastIndexOf('/') + 1);
  538. vod.subt = drives.getFile(path + urls[1]).raw_url1;
  539. }
  540. print("----play----");
  541. print(vod);
  542. return JSON.stringify(vod);
  543. }
  544. function search(wd, quick) {
  545. print(__drives);
  546. print('可搜索的alist驱动:'+searchDriver);
  547. if(!searchDriver||!wd){
  548. return JSON.stringify({
  549. 'list': []
  550. });
  551. }else{
  552. let driver = __drives[searchDriver];
  553. wd = wd.split(' ').filter(it=>it.trim()).join('+');
  554. print(driver);
  555. let surl = driver.server + '/search?box='+wd+'&url=';
  556. if(search_type){
  557. surl+='&type='+search_type;
  558. }
  559. print('搜索链接:'+surl);
  560. let html = http.get(surl).text();
  561. let lists = [];
  562. try {
  563. lists = pdfa(html,'div&&ul&&a');
  564. }catch (e) {}
  565. print(`搜索结果数:${lists.length},搜索结果显示数量限制:${limit_search_show}`);
  566. let vods = [];
  567. let excludeReg = /\.(pdf|epub|mobi|txt|doc|lrc)$/; // 过滤后缀文件
  568. let cnt = 0;
  569. lists.forEach(it=>{
  570. let vhref = pdfh(it,'a&&href');
  571. if(vhref){
  572. vhref = unescape(vhref);
  573. }
  574. if(excludeReg.test(vhref)){
  575. return; //跳过本次循环
  576. }
  577. if(cnt < limit_search_show){
  578. print(vhref);
  579. }
  580. cnt ++;
  581. let vid = searchDriver+'$'+vhref+'#search#';
  582. if(showMode==='all'){
  583. vid+='#all#';
  584. }
  585. vods.push({
  586. vod_name:pdfh(it,'a&&Text'),
  587. vod_id:vid,
  588. vod_tag: isMedia(vhref) ? 'file' : 'folder',
  589. vod_pic:'http://img1.3png.com/281e284a670865a71d91515866552b5f172b.png',
  590. vod_remarks:searchDriver
  591. });
  592. });
  593. // 截取搜索结果
  594. vods = vods.slice(0,limit_search_show);
  595. print(vods);
  596. return JSON.stringify({
  597. 'list': vods
  598. });
  599. }
  600. }
  601. function get_size(sz) {
  602. if (sz <= 0) {
  603. return "";
  604. }
  605. let filesize = "";
  606. if (sz > 1024 * 1024 * 1024 * 1024.0) {
  607. sz /= (1024 * 1024 * 1024 * 1024.0);
  608. filesize = "TB";
  609. } else if (sz > 1024 * 1024 * 1024.0) {
  610. sz /= (1024 * 1024 * 1024.0);
  611. filesize = "GB";
  612. } else if (sz > 1024 * 1024.0) {
  613. sz /= (1024 * 1024.0);
  614. filesize = "MB";
  615. } else if( sz > 1024.0){
  616. sz /= 1024.0;
  617. filesize = "KB";
  618. }else{
  619. filesize = "B";
  620. }
  621. // 转成字符串
  622. let sizeStr = sz.toFixed(2) + filesize,
  623. // 获取小数点处的索引
  624. index = sizeStr.indexOf("."),
  625. // 获取小数点后两位的值
  626. dou = sizeStr.substr(index + 1, 2);
  627. if (dou === "00") {
  628. return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2);
  629. }else{
  630. return sizeStr;
  631. }
  632. }
  633. // 相似度获取
  634. function levenshteinDistance(str1, str2) {
  635. return 100 - 100 * distance(str1, str2) / Math.max(str1.length, str2.length);
  636. }
  637. /**
  638. * 自然排序
  639. * ["第1集","第10集","第20集","第2集","1","2","10","12","23","01","02"].sort(naturalSort())
  640. * @param options {{key,caseSensitive, order: string}}
  641. */
  642. function naturalSort(options) {
  643. if (!options) {
  644. options = {};
  645. }
  646. return function (a, b) {
  647. if(options.key){
  648. a = a[options.key];
  649. b = b[options.key];
  650. }
  651. var EQUAL = 0;
  652. var GREATER = (options.order === 'desc' ?
  653. -1 :
  654. 1
  655. );
  656. var SMALLER = -GREATER;
  657. var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi;
  658. var sre = /(^[ ]*|[ ]*$)/g;
  659. var dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/;
  660. var hre = /^0x[0-9a-f]+$/i;
  661. var ore = /^0/;
  662. var normalize = function normalize(value) {
  663. var string = '' + value;
  664. return (options.caseSensitive ?
  665. string :
  666. string.toLowerCase()
  667. );
  668. };
  669. // Normalize values to strings
  670. var x = normalize(a).replace(sre, '') || '';
  671. var y = normalize(b).replace(sre, '') || '';
  672. // chunk/tokenize
  673. var xN = x.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0');
  674. var yN = y.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0');
  675. // Return immediately if at least one of the values is empty.
  676. if (!x && !y) return EQUAL;
  677. if (!x && y) return GREATER;
  678. if (x && !y) return SMALLER;
  679. // numeric, hex or date detection
  680. var xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x));
  681. var yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null;
  682. var oFxNcL, oFyNcL;
  683. // first try and sort Hex codes or Dates
  684. if (yD) {
  685. if (xD < yD) return SMALLER;
  686. else if (xD > yD) return GREATER;
  687. }
  688. // natural sorting through split numeric strings and default strings
  689. for (var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
  690. // find floats not starting with '0', string or 0 if not defined (Clint Priest)
  691. oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
  692. oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
  693. // handle numeric vs string comparison - number < string - (Kyle Adams)
  694. if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? GREATER : SMALLER;
  695. // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
  696. else if (typeof oFxNcL !== typeof oFyNcL) {
  697. oFxNcL += '';
  698. oFyNcL += '';
  699. }
  700. if (oFxNcL < oFyNcL) return SMALLER;
  701. if (oFxNcL > oFyNcL) return GREATER;
  702. }
  703. return EQUAL;
  704. };
  705. }
  706. // 完整名称排序
  707. const sortListByName = (vodList,key,order) => {
  708. if(!key){
  709. return vodList
  710. }
  711. order = order||'asc'; // 默认正序
  712. // 排序键,顺序,区分大小写
  713. return vodList.sort(naturalSort({key: key, order: order,caseSensitive:true}))
  714. };
  715. const getTimeInt = (timeStr) => {
  716. return (new Date(timeStr)).getTime();
  717. };
  718. // 时间
  719. const sortListByTime = (vodList,key,order) => {
  720. if (!key) {
  721. return vodList
  722. }
  723. let ASCarr = vodList.sort((a, b) => {
  724. a = a[key];
  725. b = b[key];
  726. return getTimeInt(a) - getTimeInt(b);
  727. });
  728. if(order==='desc'){
  729. ASCarr.reverse();
  730. }
  731. return ASCarr
  732. };
  733. // 大小
  734. const sortListBySize = (vodList,key,order) => {
  735. if (!key) {
  736. return vodList
  737. }
  738. let ASCarr = vodList.sort((a, b) => {
  739. a = a[key];
  740. b = b[key];
  741. return (Number(a) || 0) - (Number(b) || 0);
  742. });
  743. if(order==='desc'){
  744. ASCarr.reverse();
  745. }
  746. return ASCarr
  747. };
  748. // 导出函数对象
  749. export default {
  750. init: init,
  751. home: home,
  752. homeVod: homeVod,
  753. category: category,
  754. detail: detail,
  755. play: play,
  756. search: search
  757. }