bundle.js 7.2 KB

1
  1. (()=>{var __webpack_exports__={},cache=(parseCodecs=e=>{e=e.mimeType;if(!e)return{};e=e.match(/(?<mimetype>[^/]+\/[^;]+)(?:;\s*codecs="?(?<codecs>[^"]+))?/);if(!e)return{};e=e.groups.codecs;if(!e)return{};e=e.trim().replace(/,$/,"").split(",").map(e=>e.trim()).filter(Boolean);let t=null,n=null;for(const i of e){var o=i.split(".")[0];["avc1","avc2","avc3","avc4","vp9","vp8","hev1","hev2","h263","h264","mp4v","hvc1","av01","theora"].includes(o)?t=t||i:["mp4a","opus","vorbis","mp3","aac","ac-3","ec-3","eac3","dtsc","dtse","dtsh","dtsl"].includes(o)?n=n||i:console.warn("WARNING: Unknown codec "+i)}return t||n?{vcodec:t,acodec:n}:2===e.length?{vcodec:e[0],acodec:e[1]}:{}},getStringBetween=(e,t,n,o=0,i=0)=>{var s=e.indexOf(t),n=n?e.indexOf(n,s):e.length;return e.substring(s+t.length+i,n+o)},{});extractJSSignatureFunction=async(e,t)=>{return console.log("解析baseUrl: "+e),cache[e]?(console.log("从缓存中获取JSSignatureFunction: "+e),cache[e]):(t=await request("GET",e,null,{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Safari/537.36"},t),t=getDecipherFunction(t),cache[e]=t)},getUrlFromSignature=async(e,t,n)=>{var t=await extractJSSignatureFunction(t,n),o={};for(const l of e.split("&")){var[i,s]=l.split("=");o[decodeURIComponent(i)]=decodeURIComponent(s)}var[n,r,a]=[o.url,o.s,o.sp];return console.log(e,n,r,a),n+`&${a}=`+t(r)},getDecipherFunction=string=>{const js=string.replace("var _yt_player={}",""),top=getStringBetween(js,'a=a.split("")',"};",1,-28),beginningOfFunction="var "+getStringBetween(top,'a=a.split("")',"(",10,1).split(".")[0]+"=",side=getStringBetween(js,beginningOfFunction,"};",2,-beginningOfFunction.length);return console.log(side+top),eval(side+top)},request=async(e,t,i=null,s={},n)=>(n&&(t=t.replace("https://www.youtube.com","http://127.0.0.1")),console.log("请求url:"+t),console.log("请求data:"+i),console.log("请求method:"+e),console.log("请求headers:"+JSON.stringify(s)),n?fetch(t,{method:e,headers:s,body:i}).then(e=>e.text()):new Promise((n,o)=>{AF.request(t,e,i,s,(e,t)=>{t?(console.log("请求失败: "+t),o(t)):n(e)})})),detail=async(t,e)=>{try{console.log("接受到解析请求: "+t);var n=await request("GET",t,null,{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Safari/537.36"},e),o=/var ytInitialPlayerResponse\s*=\s*({.*?});/,i=n.match(o);if(!i||!i.length)throw console.log("解释失败: 无法找到json"),new Error("JSON not found.");var s=JSON.parse(i[1]),r=(console.log("ytInitialPlayerResponse: "+JSON.stringify(s)),s.videoDetails),a=[];for(const y of r.thumbnail.thumbnails)a.push({url:y.url,width:y.width+"",height:y.height+""});var l,c,u,d="https://www.youtube.com"+JSON.parse(n.match(/set\(({.+?})\);/)[1]).PLAYER_JS_URL,h=(console.log("解析baseUrl: "+d),[]);for(l of[].concat(s.streamingData.formats).concat(s.streamingData.adaptiveFormats))console.log("format: "+JSON.stringify(l)),l.url||(l.url=await getUrlFromSignature(l.signatureCipher,d,e)),l.url&&({vcodec:c,acodec:u}=parseCodecs(l),c)&&u&&h.push({width:l.width+"",height:l.height+"",type:l.mimeType,quality:l.quality,itag:l.itag,fps:l.fps+"",bitrate:l.bitrate+"",url:l.url,ext:"mp4",vcodec:c,acodec:u,vbr:"0",abr:"0",container:"mp4_dash"});if(o=/var ytInitialData\s*=\s*({.*?});/,!(i=n.match(o))||!i.length)throw console.log("解析失败,无法找到 ytInitialData"),new Error("JSON not found.");var p,g=JSON.parse(i[1]),m=(console.log("ytInitialData: "+JSON.stringify(g)),[]);for(const w of g.contents.twoColumnWatchNextResults.secondaryResults.secondaryResults.results)w.compactVideoRenderer&&(p=w.compactVideoRenderer,console.log("推荐视频: "+JSON.stringify(p)),p.videoId)&&m.push({type:"gridVideoRenderer",videoId:p.videoId,title:p.title?.simpleText,thumbnails:p.thumbnail?.thumbnails,channelName:p.longBylineText?.runs?.[0]?.text,publishedTimeText:p.publishedTimeText?.simpleText,viewCountText:p.viewCountText?.simpleText,shortViewCountText:p.shortViewCountText?.simpleText,lengthText:p.lengthText?.simpleText});var v={code:200,msg:"",data:{videoDetails:{isLiveContent:r.isLiveContent,title:r.title,thumbnails:a,description:r.shortDescription,lengthSeconds:r.lengthSeconds,viewCount:r.viewCount,keywords:r.keywords,author:r.author,channelID:r.channelId,recommendInfo:m,channelURL:"https://www.youtube.com/channel/"+r.channelId,videoId:r.videoId},streamingData:{formats:h}},id:"MusicDetailViewModel_detail_url"};return console.log("解析结果: "+JSON.stringify(v)),v}catch(e){t={code:-1,msg:e.toString()};return console.log("解析失败: "+JSON.stringify(t)),t}},search=async(e,t,n)=>{try{if(console.log("接受到搜索请求: "+e),console.log("next: "+t),t){var o=JSON.parse(t),i=o.key,s={context:{client:{clientName:"WEB",clientVersion:"2.20240506.01.00"}},continuation:o.continuation},r=await request("POST","https://www.youtube.com/youtubei/v1/search?key="+i,JSON.stringify(s),{},n),r=JSON.parse(r),a=(console.log("搜索结果res: "+JSON.stringify(r)),[]);for(const x of r.onResponseReceivedCommands[0].appendContinuationItemsAction.continuationItems[0].itemSectionRenderer.contents){var l=x.videoRenderer;l&&l.videoId&&a.push({type:"videoWithContextRenderer",data:{videoId:l.videoId,title:l.title?.runs?.[0]?.text,thumbnails:l.thumbnail?.thumbnails,channelName:l.longBylineText?.runs?.[0]?.text,publishedTimeText:l.publishedTimeText?.simpleText,viewCountText:l.viewCountText?.simpleText,shortViewCountText:l.shortViewCountText?.simpleText,lengthText:l.lengthText?.simpleText}})}var c={code:200,msg:"",data:{data:a,next:JSON.stringify({key:o.key,continuation:r.onResponseReceivedCommands[0].appendContinuationItemsAction.continuationItems[1].continuationItemRenderer.continuationEndpoint.continuationCommand.token})},id:"MusicSearchResultViewModel_search_result"};return console.log("搜索结果成功1: "+JSON.stringify(c)),c}var u=`https://www.youtube.com/results?q=${encodeURIComponent(e)}&sp=EgIQAQ%253D%253D`,d=await request("GET",u,null,{},n),h=/var ytInitialData\s*=\s*({.*?});/,p=d.match(h);if(!p||!p.length)throw console.log("搜索失败:无法找到ytInitialData"),new Error("JSON not found.");var g,m=JSON.parse(p[1]),v=(console.log("ytInitialDataResp: "+JSON.stringify(m)),[]);for(const T of m.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents[0].itemSectionRenderer.contents)T.videoRenderer&&(g=T.videoRenderer)&&g.videoId&&v.push({type:"videoWithContextRenderer",data:{videoId:g.videoId,title:g.title?.runs?.[0]?.text,thumbnails:g.thumbnail?.thumbnails,channelName:g.longBylineText?.runs?.[0]?.text,publishedTimeText:g.publishedTimeText?.simpleText,viewCountText:g.viewCountText?.simpleText,shortViewCountText:g.shortViewCountText?.simpleText,lengthText:g.lengthText?.simpleText}});var y={},w=(0<d.split("innertubeApiKey").length&&(y.key=d.split("innertubeApiKey")[1].trim().split(",")[0].split('"')[2]),y.continuation=m.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents[1].continuationItemRenderer.continuationEndpoint.continuationCommand.token,{code:200,msg:"",data:{data:v,next:JSON.stringify(y)},id:"MusicSearchResultViewModel_search_result"});return console.log("搜索结果成功2: "+JSON.stringify(w)),w}catch(e){t={code:-1,msg:e.toString()};return console.log("解析失败: "+JSON.stringify(t)),t}}})();