alist.js 24 KB

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