mirror of
https://github.com/alantang1977/X.git
synced 2024-11-06 06:53:21 +02:00
1 line
No EOL
19 KiB
JavaScript
1 line
No EOL
19 KiB
JavaScript
import{sortListByCN}from"./sortName.js";import{pdfa,pdfh}from"./pdf.js";import{_}from"assets://js/lib/cat.js";import{findBestLCS}from"./lib/similarity.js";String.prototype.rstrip=function(chars){chars=new RegExp(chars+"$");return this.replace(chars,"")};let key="xiaoya",url="https://xy.omii.link:5344/",device={},siteKey="",siteType=0;var showMode="single",searchDriver="",limit_search_show=200,search_type="",detail_order="name";const request_timeout=5e3,VERSION="xiaoya_open v2/v3 20230721";function print(any){}const http=async function(url,options={}){"POST"===options.method&&options.data&&(options.body=JSON.stringify(options.data),options.headers=Object.assign({"content-type":"application/json"},options.headers),options.headers=Object.assign({Authorization:"alist-09ceb38a-f143-47f7-b255-c3eec819cd7b0lSmqjgBRIMJakAkbJIE2KzO6h2CUVBuEkqrLiA5cJJzOzYxJtCTIGBXXnhrg7Av"},options.headers)),options.timeout=request_timeout;try{const res=await req(url,options);return res.json=()=>res&&res.content?JSON.parse(res.content):null,res.text=()=>res&&res.content?res.content:"",res}catch(e){return{json(){return null},text(){return""}}}},__drives=(["get","post"].forEach(method=>{http[method]=function(url,options={}){return http(url,Object.assign(options,{method:method.toUpperCase()}))}}),{}),__subtitle_cache={};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())}async function get_drives_path(tid){var index=tid.indexOf("$"),name=tid.substring(0,index),tid=tid.substring(index+1);return{drives:await get_drives(name),path:tid}}async function get_drives(name){var{settings,api,server}=__drives[name];return null==settings.v3&&(settings.v3=!1,server=(await http.get(server+"/api/public/settings")).json().data,Array.isArray(server)?(settings.title=server.find(x=>"title"===x.key)?.value,settings.v3=!1,settings.version=server.find(x=>"version"===x.key)?.value,settings.enableSearch="true"===server.find(x=>"enable search"===x.key)?.value):(settings.title=server.title,settings.v3=!0,settings.version=server.version,settings.enableSearch=!1),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.other=settings.v3?"/api/fs/other":null),__drives[name]}async function init(cfg){console.log("当前版本号:"+VERSION);let data;var alist_data,alist_data_url,ext=url+"/tvbox/json/alist_open.json";"object"==typeof ext?(data=ext,print("alist ext:object")):"string"==typeof ext&&(data=ext.startsWith("http")?(alist_data_url=(alist_data=ext.split(";"))[0],limit_search_show=1<alist_data.length&&Number(alist_data[1])||limit_search_show,search_type=2<alist_data.length?alist_data[2]:search_type,print(alist_data_url),(await http.get(alist_data_url)).json()):(print("alist ext:json string"),JSON.parse(ext)));let drives=[];Array.isArray(data)&&0<data.length&&data[0].hasOwnProperty("server")&&data[0].hasOwnProperty("name")?drives=data:!Array.isArray(data)&&data.hasOwnProperty("drives")&&Array.isArray(data.drives)&&(drives=data.drives.filter(it=>it.type&&"alist"===it.type||!it.type)),print(drives),print(searchDriver=!(searchDriver=(drives.find(x=>x.search)||{}).name||"")&&0<drives.length?drives[0].name:searchDriver),drives.forEach(item=>{let _path_param=[];item.params&&(_path_param=Object.keys(item.params)).sort((a,b)=>a.length-b.length),__drives[item.name]={name:item.name,server:item.server.endsWith("/")?item.server.rstrip("/"):item.server,startPage:item.startPage||"/",showAll:!0===item.showAll,search:!!item.search,params:item.params||{},_path_param:_path_param,settings:{},api:{},getParams(path){var key=this._path_param.find(x=>path.startsWith(x));return Object.assign({},this.params[key],{path:path})},async getPath(path){path=(await http.post(this.server+this.api.path,{data:this.getParams(path)})).json();return this.settings.v3?path.data.content:path.data.files},async getFile(path){path=(await http.post(this.server+this.api.file,{data:this.getParams(path)})).json(),path=this.settings.v3?path.data:path.data.files[0];return this.settings.v3||(path.raw_url=path.url),path},async getOther(method,path){path=this.getParams(path),path.method=method,method=(await http.post(this.server+this.api.other,{data:path})).json();return method},isFolder(data){return 1===data.type},isVideo(data){return this.settings.v3?2===data.type||0===data.type||3===data.type:3===data.type||0===data.type||4===data.type},isSubtitle(data){return 1!=data.type&&[".srt",".ass",".scc",".stl",".ttml"].some(x=>data.name.endsWith(x))},getType(data){var isVideo=this.isVideo(data);return this.isFolder(data)?0:isVideo?10:1},getPic(data){return(this.settings.v3?data.thumb:data.thumbnail)||(this.isFolder(data)?"http://img1.3png.com/281e284a670865a71d91515866552b5f172b.png":"")},getSize(data){let sz=data.size||0;if(sz<=0)return"";let filesize="";return filesize=1099511627776<sz?(sz/=1099511627776,"TB"):1073741824<sz?(sz/=1073741824,"GB"):1048576<sz?(sz/=1048576,"MB"):(sz/=1024,"KB"),sz.toFixed(2)+filesize},getRemark(data){return""},getTime(data,isStandard){isStandard=isStandard||!1;try{let tTime=data.updated_at||data.time_str||data.modified||"",date="";return tTime&&(tTime=tTime.split("T"),date=tTime[0],isStandard&&(date=date.replace(/-/g,"/")),tTime=tTime[1].split(/Z|\./),date+=" "+tTime[0]),date}catch(e){return""}}}}),print("init执行完毕")}function home(filter){var classes=Object.keys(__drives).map(key=>({type_id:key+"$"+__drives[key].startPage,type_name:key,type_flag:"1"}));let filter_dict={},filters=[{key:"order",name:"排序",value:[{n:"名称⬆️",v:"vod_name_asc"},{n:"名称⬇️",v:"vod_name_desc"},{n:"中英⬆️",v:"vod_cn_asc"},{n:"中英⬇️",v:"vod_cn_desc"},{n:"时间⬆️",v:"vod_time_asc"},{n:"时间⬇️",v:"vod_time_desc"},{n:"大小⬆️",v:"vod_size_asc"},{n:"大小⬇️",v:"vod_size_desc"},{n:"无",v:"none"}]},{key:"show",name:"播放展示",value:[{n:"单集",v:"single"},{n:"全集",v:"all"}]}];return classes.forEach(it=>{filter_dict[it.type_id]=filters}),print("----home----"),print(classes),JSON.stringify({class:classes,filters:filter_dict})}async function homeVod(params){var driver=__drives[searchDriver],driver=(print(driver),driver.server+"/sou?filter=last&num=50&type=video"),driver=(print("搜索链接:"+driver),(await http.get(driver)).text());let lists=[];try{lists=pdfa(driver,"div&&ul&&a")}catch(e){}print(`搜索结果数:${lists.length},搜索结果显示数量限制:`+limit_search_show);let vods=[],excludeReg=/\.(pdf|epub|mobi|txt|doc|lrc)$/,cnt=0;return lists.forEach(it=>{let vhref=pdfh(it,"a&&href");if(vhref=vhref&&unescape(vhref),!excludeReg.test(vhref)){var uri_parts,it=vhref.split("#");2<=it.length&&(vhref=it[0]),cnt<limit_search_show&&print(vhref),cnt++;let vid=searchDriver+"$"+vhref+"#search#",have_pic=("all"===showMode&&(vid+="#all#"),0),poster="",douban_rate="",display_path="";5===it.length&&(uri_parts=it[4].split("://"),poster=url+"/image/"+uri_parts[1],have_pic=1),4<=it.length&&(douban_rate=it[3]),it.length<2&&(uri_parts=it[0].split("/"),display_path=uri_parts[uri_parts.length-2]+"/"+uri_parts[uri_parts.length-1]),vods.push({vod_id:vid,vod_name:it.length<2?display_path:it[1],vod_tag:isMedia(vhref)?"file":"folder",vod_pic:have_pic?poster:"http://img.xiaoya.pro/xiaoya.jpg",vod_remarks:""==douban_rate?"":"豆瓣:"+douban_rate})}}),JSON.stringify({list:vods})}async function subcategory(tid){var tid=tid.replace(/#all#|#search#/g,""),{drives,path}=await get_drives_path(tid);const id=tid.endsWith("/")?tid:tid+"/";var list=await drives.getPath(path);let subList=[];var vodFiles=[],allList=[];for(const k in list){var item=list[k];if(drives.isSubtitle(item)&&subList.push(item.name),drives.showAll||drives.isFolder(item)||drives.isVideo(item)&&isMedia(item.name)){var vod_time=drives.getTime(item),vod_size=get_size(item.size),vod_size=vod_time.split(" ")[0].substr(2)+"\t"+vod_size;let vod_id=id+item.name+(drives.isFolder(item)?"/":"");"all"===showMode&&(vod_id+="#all#"),print(vod_id);vod_time={vod_id:vod_id,vod_name:item.name.replaceAll("$","").replaceAll("#",""),vod_pic:drives.getPic(item),vod_time:vod_time,vod_size:item.size,vod_tag:drives.isFolder(item)?"folder":"file",vod_remarks:drives.isFolder(item)?vod_size+" 文件夹":vod_size};drives.isVideo(item)&&isMedia(vod_id)&&(vodFiles.push(vod_time),allList.push(vod_time))}}return 0<subList.length&&vodFiles.forEach(item=>{var sbust=findBestLCS(item.vod_name,subList);sbust.bestMatch&&(__subtitle_cache[item.vod_id]=[id+sbust.bestMatch.target])}),JSON.stringify({subAllList:allList})}async function category(tid,pg,filter,extend){for(const k in __subtitle_cache)delete __subtitle_cache[k];tid.includes("#search#"),tid.includes("#all#");var orid=tid.replace(/#all#|#search#/g,"");const id=orid.endsWith("/")?orid:orid+"/";let allList=[],subList=[],vodFiles=[];filter=filter?extend:{};filter.show&&(showMode=filter.show);let{drives,path}=await get_drives_path(orid);if("/"==path){var extend=await homeVod(null),orid=JSON.parse(extend)["list"];allList=orid}else{const list=await drives.getPath(path);list.forEach(item=>{if(drives.isSubtitle(item)&&subList.push(item.name),drives.showAll||drives.isFolder(item)||drives.isVideo(item)&&isMedia(item.name)){var vod_time=drives.getTime(item),vod_size=get_size(item.size),vod_size=vod_time.split(" ")[0].substr(2)+"\t"+vod_size;let vod_id=id+item.name+(drives.isFolder(item)?"/":"");"all"===showMode&&(vod_id+="#all#"),print(vod_id);vod_time={vod_id:vod_id,vod_name:item.name.replaceAll("$","").replaceAll("#",""),vod_pic:drives.getPic(item),vod_time:vod_time,vod_size:item.size,vod_tag:drives.isFolder(item)?"folder":"file",vod_remarks:drives.isFolder(item)?vod_size+" 文件夹":vod_size};drives.isVideo(item)&&isMedia(vod_id)&&vodFiles.push(vod_time),allList.push(vod_time)}}),0<subList.length&&vodFiles.forEach(item=>{var sbust=findBestLCS(item.vod_name,subList);sbust.bestMatch&&(__subtitle_cache[item.vod_id]=[id+sbust.bestMatch.target])})}return filter.order?(print(`排序key:${extend=filter.order.split("_").slice(0,-1).join("_")},排序order:`+(orid=filter.order.split("_").slice(-1)[0])),extend.includes("name")?(detail_order="name",allList=sortListByName(allList,extend,orid)):extend.includes("cn")?(detail_order="cn",allList=sortListByCN(allList,"vod_name",orid)):extend.includes("time")?(detail_order="time",allList=sortListByTime(allList,extend,orid)):extend.includes("size")?(detail_order="size",allList=sortListBySize(allList,extend,orid)):filter.order.includes("none")&&(detail_order="none",print("不排序"))):"none"!==detail_order&&(allList=sortListByName(allList,"vod_name","asc")),pg&&1<vodFiles.length&&(extend={vod_id:id+"~playlist",vod_name:"播放列表",vod_pic:"http://img1.3png.com/3063ad894f04619af7270df68a124f129c8f.png",vod_tag:"file",vod_remarks:"共"+vodFiles.length+"集"},allList.unshift(extend)),print("----category----"+`tid:${tid},detail_order:${detail_order},showMode:`+showMode),JSON.stringify({parent:id,page:1,pagecount:1,limit:allList.length,total:allList.length,list:allList})}async function getAll(otid,tid,drives,path){try{otid.includes("#search#"),otid.includes("#all#");var content=await category(otid,null,!1,null),list=(isMedia(otid.replace(/#all#|#search#/g,"").split("@@@")[0]),JSON.parse(content)["list"]),vod_play_url=[];for(const k in list){var x=list[k];if("file"===x.vod_tag){var vid=x.vod_id.replace(/#all#|#search#/g,"");vod_play_url.push(x.vod_name+"$"+vid.substring(vid.indexOf("$")+1))}else{var subcontent=await subcategory(x.vod_id),subAllList=JSON.parse(subcontent)["subAllList"];for(const j in subAllList){var y=subAllList[j];if("file"===y.vod_tag){let vid=y.vod_id.replace(/#all#|#search#/g,"");vod_play_url.push(y.vod_name+"$"+vid.substring(vid.indexOf("$")+1))}}}}var pl=path.split("/").filter(it=>it);let vod_name=pl[pl.length-1]||drives.name;vod_name===drives.name&&print(pl),otid.includes("#search#")&&(vod_name+="[搜]");var vod={vod_id:otid,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};return print("----detail1----"),print(vod),JSON.stringify({list:[vod]})}catch(e){return print(e.message),JSON.stringify({list:[{}]})}}async function playlist(otid,tid,drives,path){return tid=tid.replace("/~playlist",""),getAll(otid=otid.replace("/~playlist",""),tid,drives,path=path.replace("/~playlist",""))}async function detail(tid){var isSearch=tid.includes("#search#"),isAll=tid.includes("#all#"),otid=tid,isFile=isMedia((tid=tid.replace(/#all#|#search#/g,"")).split("@@@")[0]),{drives,path}=(print(`isFile:${tid}?`+isFile),await get_drives_path(tid));if(print(`drives:${drives},path:`+path),path.endsWith("/"))return getAll(otid,tid,drives,path);if(path.endsWith("/~playlist"))return playlist(otid,tid,drives,path);if(isSearch&&!isFile)return getAll(otid,tid,drives,path);if(isAll){let new_tid,{drives,path}=(new_tid=isFile?tid.split("/").slice(0,-1).join("/")+"/":tid,print(`全集模式 tid:${tid}=>tid:`+new_tid),await get_drives_path(new_tid));return getAll(otid,new_tid,drives,path)}if(isFile){isSearch=path.split("@@@"),isAll=isSearch[0].substring(isSearch[0].lastIndexOf("/")+1);let vod_title=isAll;otid.includes("#search#")&&(vod_title+="[搜]");isFile={vod_id:otid,vod_name:vod_title,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:isAll+"$"+path,vod_remarks:drives.settings.title};return print("----detail2----"),print(isFile),JSON.stringify({list:[isFile]})}return JSON.stringify({list:[]})}async function play(flag,id,flags){if(null!=id){var drives=await get_drives(flag),flag=id.split("@@@"),id=flag[0],item=await drives.getFile(id),subs=[],vod={parse:0,playUrl:"",url:item.raw_url,size:0,remark:"",header:{},extra:{subt:subs}},path=drives.name+"$"+id;if(__subtitle_cache[path])for(const sub of __subtitle_cache[path])try{var subP=await get_drives_path(sub),subItem=await drives.getFile(subP.path);subs.push(subItem.raw_url)}catch(error){}if("AliyundriveShare2Open"===item.provider&&drives.api.other){var urlss=["原画",item.raw_url];try{for(const live of(await drives.getOther("video_preview",flag[0])).data.video_preview_play_info.live_transcoding_task_list)"finished"===live.status&&(urlss.push(live.template_id),urlss.push(live.url))}catch(error){}vod.url=urlss,vod.name=item.name,vod.header={},vod.extra.subt=subs,vod.size=drives.getSize(item),vod.remark=drives.name+"$"+id}else{if("123Pan"===item.provider){let url=item.raw_url;try{url=(await http.get(url)).json().data.redirect_url}catch(error){}vod.url=url}else vod.url=item.raw_url;vod.name=item.name,vod.size=drives.getSize(item),vod.remark=drives.name+"$"+id,vod.header={},vod.extra.subt=subs}return print("----play----"),print(vod),JSON.stringify(vod)}}async function search(wd,quick){if(print(__drives),print("可搜索的alist驱动:"+searchDriver),searchDriver&&wd){var driver=__drives[searchDriver];wd=wd.split(" ").filter(it=>it.trim()).join("+"),print(driver);let surl=driver.server+"/sou?box="+wd+"&url=";search_type&&(surl+="&type="+search_type),print("搜索链接:"+surl);driver=(await http.get(surl)).text();let lists=[];try{lists=pdfa(driver,"div&&ul&&a")}catch(e){}print(`搜索结果数:${lists.length},搜索结果显示数量限制:`+limit_search_show);let vods=[],excludeReg=/\.(pdf|epub|mobi|txt|doc|lrc)$/,cnt=0;return lists.forEach(it=>{let vhref=pdfh(it,"a&&href");if(vhref=vhref&&unescape(vhref),!excludeReg.test(vhref)){var uri_parts,it=vhref.split("#");2<=it.length&&(vhref=it[0]),cnt<limit_search_show&&print(vhref),cnt++;let vid=searchDriver+"$"+vhref+"#search#",have_pic=("all"===showMode&&(vid+="#all#"),0),poster="",douban_rate="",display_path="";5===it.length&&(uri_parts=it[4].split("://"),poster=url+"/image/"+uri_parts[1],have_pic=1),4<=it.length&&(douban_rate=it[3]),it.length<2&&(uri_parts=it[0].split("/"),display_path=uri_parts[uri_parts.length-2]+"/"+uri_parts[uri_parts.length-1]),vods.push({vod_id:vid,vod_name:it.length<2?display_path:it[1],vod_tag:isMedia(vhref)?"file":"folder",vod_pic:have_pic?poster:"http://img.xiaoya.pro/xiaoya.jpg",vod_remarks:""==douban_rate?searchDriver:searchDriver+" /豆瓣:"+douban_rate})}}),print(vods=vods.slice(0,limit_search_show)),JSON.stringify({list:vods})}return JSON.stringify({list:[]})}function get_size(sz){if(sz<=0)return"";let filesize="";filesize=1099511627776<sz?(sz/=1099511627776,"TB"):1073741824<sz?(sz/=1073741824,"GB"):1048576<sz?(sz/=1048576,"MB"):1024<sz?(sz/=1024,"KB"):"B";var sz=sz.toFixed(2)+filesize,index=sz.indexOf(".");return"00"===sz.substr(index+1,2)?sz.substring(0,index)+sz.substr(index+3,2):sz}function naturalSort(options){return options=options||{},function(a,b){options.key&&(a=a[options.key],b=b[options.key]);function normalize(value){return value=""+value,options.caseSensitive?value:value.toLowerCase()}var GREATER="desc"===options.order?-1:1,SMALLER=-GREATER,re=/(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,sre=/(^[ ]*|[ ]*$)/g,dre=/(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,hre=/^0x[0-9a-f]+$/i,ore=/^0/,a=normalize(a).replace(sre,"")||"",b=normalize(b).replace(sre,"")||"",xN=a.replace(re,"\0$1\0").replace(/\0$/,"").replace(/^\0/,"").split("\0"),yN=b.replace(re,"\0$1\0").replace(/\0$/,"").replace(/^\0/,"").split("\0");if(a||b){if(!a&&b)return GREATER;if(a&&!b)return SMALLER;var oFxNcL,oFyNcL,sre=parseInt(a.match(hre))||1!=xN.length&&a.match(dre)&&Date.parse(a),re=parseInt(b.match(hre))||sre&&b.match(dre)&&Date.parse(b)||null;if(re){if(sre<re)return SMALLER;if(re<sre)return GREATER}for(var cLoc=0,numS=Math.max(xN.length,yN.length);cLoc<numS;cLoc++){if(oFxNcL=!(xN[cLoc]||"").match(ore)&&parseFloat(xN[cLoc])||xN[cLoc]||0,oFyNcL=!(yN[cLoc]||"").match(ore)&&parseFloat(yN[cLoc])||yN[cLoc]||0,isNaN(oFxNcL)!==isNaN(oFyNcL))return isNaN(oFxNcL)?GREATER:SMALLER;if(typeof oFxNcL!=typeof oFyNcL&&(oFxNcL+="",oFyNcL+=""),oFxNcL<oFyNcL)return SMALLER;if(oFyNcL<oFxNcL)return GREATER}}return 0}}const sortListByName=(vodList,key,order)=>key?vodList.sort(naturalSort({key:key,order:order=order||"asc",caseSensitive:!0})):vodList,getTimeInt=timeStr=>new Date(timeStr).getTime(),sortListByTime=(vodList,key,order)=>{var ASCarr;return key?(ASCarr=vodList.sort((a,b)=>(a=a[key],b=b[key],getTimeInt(a)-getTimeInt(b))),"desc"===order&&ASCarr.reverse(),ASCarr):vodList},sortListBySize=(vodList,key,order)=>{var ASCarr;return key?(ASCarr=vodList.sort((a,b)=>(a=a[key],b=b[key],(Number(a)||0)-(Number(b)||0))),"desc"===order&&ASCarr.reverse(),ASCarr):vodList};export default{init:init,home:home,homeVod:homeVod,category:category,detail:detail,play:play,search:search};function __jsEvalReturn(){return{init:init,home:home,homeVod:homeVod,category:category,detail:detail,play:play,search:search}}export{__jsEvalReturn}; |