1 |
- (()=>{var __webpack_exports__={};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)},getUrlFromSignature=(e,t)=>{var t=getDecipherFunction(t),n={};for(const l of e.split("&")){var[o,i]=l.split("=");n[decodeURIComponent(o)]=decodeURIComponent(i)}var[s,r,a]=[n.url,n.s,n.sp];return console.log(e,s,r,a),s+`&${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={"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"},o=await request("GET",t,null,n,e),i="https://www.youtube.com"+JSON.parse(o.match(/set\(({.+?})\);/)[1]).PLAYER_JS_URL,s=(console.log("解析baseUrl: "+i),await request("GET",i,null,n,e)),r=/var ytInitialPlayerResponse\s*=\s*({.*?});/,a=o.match(r);if(!a||!a.length)throw console.log("解释失败: 无法找到json"),new Error("JSON not found.");var l=JSON.parse(a[1]),c=(console.log("ytInitialPlayerResponse: "+JSON.stringify(l)),l.videoDetails),d=[];for(const x of c.thumbnail.thumbnails)d.push({url:x.url,width:x.width+"",height:x.height+""});var u,h,p,m=[];for(u of[].concat(l.streamingData.formats).concat(l.streamingData.adaptiveFormats))console.log("format: "+JSON.stringify(u)),u.url||(u.url=getUrlFromSignature(u.signatureCipher,s)),u.url&&({vcodec:h,acodec:p}=parseCodecs(u),h)&&p&&m.push({width:u.width+"",height:u.height+"",type:u.mimeType,quality:u.quality,itag:u.itag,fps:u.fps+"",bitrate:u.bitrate+"",url:u.url,ext:"mp4",vcodec:h,acodec:p,vbr:"0",abr:"0",container:"mp4_dash"});if(r=/var ytInitialData\s*=\s*({.*?});/,!(a=o.match(r))||!a.length)throw console.log("解析失败,无法找到 ytInitialData"),new Error("JSON not found.");var g,v=JSON.parse(a[1]),y=(console.log("ytInitialData: "+JSON.stringify(v)),[]);for(const T of v.contents.twoColumnWatchNextResults.secondaryResults.secondaryResults.results)T.compactVideoRenderer&&(g=T.compactVideoRenderer,console.log("推荐视频: "+JSON.stringify(g)),g.videoId)&&y.push({type:"gridVideoRenderer",videoId:g.videoId,title:g.title?.simpleText,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 w={code:200,msg:"",data:{videoDetails:{isLiveContent:c.isLiveContent,title:c.title,thumbnails:d,description:c.shortDescription,lengthSeconds:c.lengthSeconds,viewCount:c.viewCount,keywords:c.keywords,author:c.author,channelID:c.channelId,recommendInfo:y,channelURL:"https://www.youtube.com/channel/"+c.channelId,videoId:c.videoId},streamingData:{formats:m}},id:"MusicDetailViewModel_detail_url"};return console.log("解析结果: "+JSON.stringify(w)),w}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 d=`https://www.youtube.com/results?q=${encodeURIComponent(e)}&sp=EgIQAQ%253D%253D`,u=await request("GET",d,null,{},n),h=/var ytInitialData\s*=\s*({.*?});/,p=u.match(h);if(!p||!p.length)throw console.log("搜索失败:无法找到ytInitialData"),new Error("JSON not found.");var m,g=JSON.parse(p[1]),v=(console.log("ytInitialDataResp: "+JSON.stringify(g)),[]);for(const T of g.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents[0].itemSectionRenderer.contents)T.videoRenderer&&(m=T.videoRenderer)&&m.videoId&&v.push({type:"videoWithContextRenderer",data:{videoId:m.videoId,title:m.title?.runs?.[0]?.text,thumbnails:m.thumbnail?.thumbnails,channelName:m.longBylineText?.runs?.[0]?.text,publishedTimeText:m.publishedTimeText?.simpleText,viewCountText:m.viewCountText?.simpleText,shortViewCountText:m.shortViewCountText?.simpleText,lengthText:m.lengthText?.simpleText}});var y={},w=(0<u.split("innertubeApiKey").length&&(y.key=u.split("innertubeApiKey")[1].trim().split(",")[0].split('"')[2]),y.continuation=g.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}}})();
|