6/18/ds/yunjs/lib/alist.min.js

1 line
16 KiB
JavaScript
Raw Normal View History

2024-07-12 09:44:07 +03:00
import{distance}from"./mod.js";import{sortListByCN}from"./sortName.js";String.prototype.rstrip=function(chars){let regex=new RegExp(chars+"$");return this.replace(regex,"")};var showMode="single";var searchDriver="";var limit_search_show=200;var search_type="";var detail_order="name";var playRaw=1;const request_timeout=5e3;const VERSION="alist v2/v3 20221223";const UA="Mozilla/5.0";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(typeof any+":"+any.length)}}else if(typeof any=="object"&&Object.keys(any).length<1){console.log("null object")}else{console.log(any)}}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}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)}options.timeout=request_timeout;if(!options.headers){options.headers={}}let keys=Object.keys(options.headers).map(it=>it.toLowerCase());if(!keys.includes("referer")){options.headers["Referer"]=getHome(url)}if(!keys.includes("user-agent")){options.headers["User-Agent"]=UA}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""}}}};["get","post"].forEach(method=>{http[method]=function(url,options={}){return http(url,Object.assign(options,{method:method.toUpperCase()}))}});const __drives={};function isMedia(file){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())}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: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}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){console.log("当前版本号:"+VERSION);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()}else{print("alist ext:json string");data=JSON.parse(ext)}}let drives=[];if(Array.isArray(data)&&data.length>0&&data[0].hasOwnProperty("server")&&data[0].hasOwnProperty("name")){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||"";if(!searchDriver&&drives.length>0){searchDriver=drives[0].name}print(searchDriver);drives.forEach(item=>{let _path_param=[];if(item.params){_path_param=Object.keys(item.params);_path_param.sort((a,b)=>a.length-b.length)}if(item.password){let pwdObj={password:item.password};if(!item.params){item.params={"/":pwdObj}}else{item.params["/"]=pwdObj}_path_param.unshift("/")}__drives[item.name]={name:item.name,server:item.server.endsWith("/")?item.server.rstrip("/"):item.