info.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. console.log('bundle2!')
  2. printable = (platform) => {
  3. return platform === "WEB";
  4. }
  5. parseCodecs = (format) => {
  6. const mimeType = format['mimeType']
  7. if (!mimeType) {
  8. return {};
  9. }
  10. const regex = /(?<mimetype>[^/]+\/[^;]+)(?:;\s*codecs="?(?<codecs>[^"]+))?/;
  11. const match = mimeType.match(regex);
  12. if (!match) {
  13. return {};
  14. }
  15. const codecs = match.groups.codecs;
  16. if (!codecs) {
  17. return {};
  18. }
  19. const splitCodecs = codecs.trim().replace(/,$/, '').split(',').map(str => str.trim()).filter(Boolean);
  20. let vcodec = null;
  21. let acodec = null;
  22. for (const fullCodec of splitCodecs) {
  23. const codec = fullCodec.split('.')[0];
  24. if (['avc1', 'avc2', 'avc3', 'avc4', 'vp9', 'vp8', 'hev1', 'hev2', 'h263', 'h264', 'mp4v', 'hvc1', 'av01', 'theora'].includes(codec)) {
  25. if (!vcodec) {
  26. vcodec = fullCodec;
  27. }
  28. } else if (['mp4a', 'opus', 'vorbis', 'mp3', 'aac', 'ac-3', 'ec-3', 'eac3', 'dtsc', 'dtse', 'dtsh', 'dtsl'].includes(codec)) {
  29. if (!acodec) {
  30. acodec = fullCodec;
  31. }
  32. } else {
  33. console.log(`WARNING: Unknown codec ${fullCodec}`);
  34. }
  35. }
  36. if (!vcodec && !acodec) {
  37. if (splitCodecs.length === 2) {
  38. return {
  39. vcodec: splitCodecs[0],
  40. acodec: splitCodecs[1]
  41. };
  42. }
  43. } else {
  44. return {
  45. vcodec: vcodec,
  46. acodec: acodec
  47. };
  48. }
  49. return {};
  50. }
  51. parseSetCookie = (headers) => {
  52. if (!headers) {
  53. return ""
  54. }
  55. const setCookie = headers['Set-Cookie']
  56. if (!setCookie) {
  57. return ""
  58. }
  59. console.log(`setCookie: ${setCookie}`)
  60. let result = 'PREF=hl=en&tz=UTC; SOCS=CAI; GPS=1; ';
  61. const needCookieNames = ['YSC', 'VISITOR_INFO1_LIVE', 'VISITOR_PRIVACY_METADATA'];
  62. for (const i in needCookieNames) {
  63. const cookieName = needCookieNames[i];
  64. const regexp = new RegExp(`${cookieName}=([^;,]+)`)
  65. const match = setCookie.match(regexp)
  66. if (match && match.length === 2) {
  67. const cookieValue = match[1]
  68. if (i != needCookieNames.length - 1) {
  69. result += `${cookieName}=${cookieValue}; `
  70. } else {
  71. result += `${cookieName}=${cookieValue}`
  72. }
  73. }
  74. }
  75. console.log(`current cookie: ${result}`)
  76. return result;
  77. }
  78. request = async (method, url, data = null, headers = {}, platform) => {
  79. if (platform === "WEB") {
  80. url = url.replace("https://www.youtube.com", "http://127.0.0.1");
  81. }
  82. console.log(`request url:${url}`)
  83. console.log(`request data:${data}`)
  84. console.log(`request method:${method}`)
  85. console.log(`request headers:${JSON.stringify((headers))}`)
  86. if (platform === "WEB") {
  87. const res = await fetch(url, {
  88. 'mode': 'cors',
  89. 'method': method,
  90. 'headers': headers,
  91. 'body': data
  92. })
  93. const resData = await res.text()
  94. return Promise.resolve({
  95. 'data': resData,
  96. 'headers': res.headers
  97. });
  98. }
  99. return new Promise((resolve, reject) => {
  100. AF.request(url, method, data, headers, (data, headers, err) => {
  101. if (err) {
  102. reject(err);
  103. } else {
  104. console.log(`response headers: ${headers}`);
  105. resolve({
  106. 'data': data,
  107. 'headers': JSON.parse(headers)
  108. });
  109. }
  110. });
  111. })
  112. }
  113. getStringBetween = (string, needleStart, needleEnd, offsetStart = 0, offsetEnd = 0) => {
  114. const x = string.indexOf(needleStart);
  115. const y = needleEnd ? string.indexOf(needleEnd, x) : string.length;
  116. return string.substring(x + needleStart.length + offsetEnd, y + offsetStart);
  117. }
  118. findFunction = (jsCode, regexp, platform) => {
  119. const match = jsCode.match(regexp)
  120. if (!match && match.length <= 1) {
  121. return null;
  122. }
  123. let result = "";
  124. const dependencyMatches = match[0].match(/([$a-zA-Z0-9]+\.[$a-zA-Z0-9]+)/g)
  125. const existDependencies = [];
  126. if (dependencyMatches && dependencyMatches.length >= 1) {
  127. for (let currentMatch of dependencyMatches) {
  128. const varName = currentMatch.split('.')[0];
  129. if (existDependencies.includes(varName)) {
  130. continue
  131. }
  132. const varNameMatch = jsCode.match(new RegExp(`var \\${varName}={(.|\\n)*?};`), 'ig');
  133. if (varNameMatch && varNameMatch.length >= 1) {
  134. result += varNameMatch[0] + "\n";
  135. }
  136. existDependencies.push(varName);
  137. }
  138. }
  139. result += `\n${match[0]}`;
  140. if (printable(platform)) {
  141. console.log(`decipherFunction result: ` + result);
  142. }
  143. return eval(result);
  144. };
  145. const cache = {};
  146. fetchBaseJSContent = async (baseJsUrl, platform) => {
  147. const cacheKey = `jsContent:${baseJsUrl}`;
  148. if (cache[cacheKey]) {
  149. console.log(`baseContent from cache: ${baseJsUrl}`);
  150. return cache[cacheKey];
  151. }
  152. console.log(`extract baseUrl: ${baseJsUrl}`);
  153. const baseContentResp = await request('GET', baseJsUrl, null, {
  154. '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',
  155. }, platform);
  156. const {data, _} = baseContentResp;
  157. cache[cacheKey] = data;
  158. return data;
  159. }
  160. extractJSSignatureFunction = async (baseJsUrl, platform) => {
  161. const cacheKey = `jsSign:${baseJsUrl}`
  162. if (cache[cacheKey]) {
  163. console.log(`jsSign from cache: ${baseJsUrl}`);
  164. return cache[cacheKey];
  165. }
  166. const baseJsContent = await fetchBaseJSContent(baseJsUrl, platform);
  167. const result = findFunction(baseJsContent, /([a-zA-Z0-9]+)=function\([a-zA-Z0-9]+\)\{a=a\.split\(""\).*};/, platform);
  168. cache[cacheKey] = result
  169. return result
  170. }
  171. extractNJSFunction = async (baseJsUrl, platform) => {
  172. const cacheKey = `jsN:${baseJsUrl}`
  173. if (cache[cacheKey]) {
  174. console.log(`jsN from cache: ${baseJsUrl}`);
  175. return cache[cacheKey];
  176. }
  177. const baseJsContent = await fetchBaseJSContent(baseJsUrl, platform);
  178. const result = findFunction(baseJsContent, /([a-zA-Z0-9]+)=function\([a-zA-Z0-9]+\)\{var b=a\.split\(""\)[\s\S]*?};/, platform);
  179. cache[cacheKey] = result
  180. return result
  181. }
  182. signUrl = async (signatureCipher, baseJsUrl, platform) => {
  183. const searchParams = {}
  184. for (const item of signatureCipher.split('&')) {
  185. const [key, value] = item.split('=');
  186. searchParams[decodeURIComponent(key)] = decodeURIComponent(value);
  187. }
  188. const [url, signature, sp] = [searchParams['url'], searchParams['s'], searchParams['sp']];
  189. const decipher = await extractJSSignatureFunction(baseJsUrl, platform);
  190. if (!decipher) {
  191. return null;
  192. }
  193. if (printable(platform)) {
  194. console.log(`signatureCipher=${signatureCipher}, url=${url}, signature=${signature}, sp=${sp}`)
  195. }
  196. let newUrl = `${url}&${sp}=${decipher(signature)}`;
  197. function replaceUrlParam(url, paramName, paramValue) {
  198. let pattern = new RegExp(`([?&])${paramName}=.*?(&|$)`, 'i');
  199. let newUrl = url.replace(pattern, `$1${paramName}=${paramValue}$2`);
  200. if (newUrl === url && url.indexOf('?') === -1) {
  201. newUrl += `?${paramName}=${paramValue}`;
  202. } else if (newUrl === url) {
  203. newUrl += `&${paramName}=${paramValue}`;
  204. }
  205. return newUrl;
  206. }
  207. for (const item of url.split('&')) {
  208. const [key, value] = item.split('=');
  209. searchParams[decodeURIComponent(key)] = decodeURIComponent(value);
  210. }
  211. const nFunction = await extractNJSFunction(baseJsUrl, platform);
  212. const n = searchParams['n']
  213. if (n && nFunction) {
  214. const newN = nFunction(n);
  215. return replaceUrlParam(newUrl, 'n', newN);
  216. }
  217. return newUrl;
  218. }
  219. detail = async (url, platform) => {
  220. try {
  221. const htmlResp = await request('GET', `${url}&bpctr=9999999999&has_verified=1`, null, {
  222. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
  223. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  224. 'Accept-Language': 'en-us,en;q=0.5',
  225. 'Sec-Fetch-Mode': 'navigate',
  226. 'Accept-Encoding': 'gzip, deflate, br',
  227. 'Cookie': 'PREF=hl=en&tz=UTC; SOCS=CAI'
  228. }, platform);
  229. let {data: html, headers: htmlHeaders} = htmlResp;
  230. let regex = /var ytInitialPlayerResponse\s*=\s*({.*?});/;
  231. let match = html.match(regex);
  232. if (!match || !match.length) {
  233. console.log('can not found JSON: ytInitialPlayerResponse');
  234. throw new Error('JSON not found: ytInitialPlayerResponse');
  235. }
  236. const ytInitialPlayerResponse = JSON.parse(match[1]);
  237. if (printable(platform)) {
  238. console.log(ytInitialPlayerResponse);
  239. }
  240. const originVideoDetails = ytInitialPlayerResponse['videoDetails'];
  241. const thumbnails = []
  242. for (const item of originVideoDetails['thumbnail']['thumbnails']) {
  243. thumbnails.push({
  244. 'url': item['url'],
  245. 'width': item['width'] + "",
  246. 'height': item['height'] + ""
  247. })
  248. }
  249. let originFormats = [];
  250. const currentFormats = [];
  251. for (const format of ytInitialPlayerResponse["streamingData"]["formats"].concat(ytInitialPlayerResponse["streamingData"]["adaptiveFormats"])) {
  252. if (format) {
  253. format["from"] = "web"
  254. currentFormats.push(format);
  255. }
  256. }
  257. currentFormats.reverse()
  258. originFormats = originFormats.concat(currentFormats);
  259. console.log(`after html, format size:${originFormats.length}`);
  260. const baseJsUrl = `https://www.youtube.com${JSON.parse(html.match(/set\(({.+?})\);/)[1])["PLAYER_JS_URL"]}`
  261. let formatIds = [];
  262. const formats = [];
  263. for (let format of originFormats) {
  264. if (printable(platform)) {
  265. console.log(format);
  266. }
  267. if (format && formatIds.indexOf(format['itag']) === -1) {
  268. if (!format["url"]) {
  269. format["url"] = await signUrl(format["signatureCipher"], baseJsUrl, platform);
  270. }
  271. if (format["url"]) {
  272. const {vcodec, acodec} = parseCodecs(format)
  273. if (vcodec && acodec) {
  274. const current = {
  275. "width": format["width"] + "",
  276. "height": format["height"] + "",
  277. "type": format["mimeType"],
  278. "quality": format["quality"],
  279. "itag": format["itag"],
  280. "fps": format["fps"] + "",
  281. "bitrate": format["bitrate"] + "",
  282. "url": format["url"],
  283. "ext": "mp4",
  284. "vcodec": vcodec,
  285. "acodec": acodec,
  286. "vbr": "0",
  287. "abr": "0",
  288. "container": "mp4_dash",
  289. "from": format["from"]
  290. }
  291. if (platform === "WEB") {
  292. current["source"] = format
  293. }
  294. formats.push(current)
  295. formatIds.push(format["itag"]);
  296. }
  297. }
  298. }
  299. }
  300. const ytInitialDataMatch = html.match(/var ytInitialData\s*=\s*({.*?});/);
  301. const recommendInfo = [];
  302. if (ytInitialDataMatch && ytInitialDataMatch.length === 2) {
  303. const ytInitialData = JSON.parse(ytInitialDataMatch[1]);
  304. if (printable(platform)) {
  305. console.log(ytInitialData);
  306. }
  307. for (const item of ytInitialData["contents"]?.["twoColumnWatchNextResults"]?.["secondaryResults"]?.["secondaryResults"]?.["results"] || []) {
  308. if (item["compactVideoRenderer"]) {
  309. const recommendVideo = item["compactVideoRenderer"];
  310. console.log(`recommend video: ${JSON.stringify(recommendVideo)}`);
  311. if (recommendVideo["videoId"]) {
  312. recommendInfo.push({
  313. "type": "gridVideoRenderer",
  314. "videoId": recommendVideo["videoId"],
  315. "title": recommendVideo["title"]?.["simpleText"],
  316. "thumbnails": recommendVideo["thumbnail"]?.["thumbnails"],
  317. "channelName": recommendVideo["longBylineText"]?.["runs"]?.[0]?.["text"],
  318. "publishedTimeText": recommendVideo["publishedTimeText"]?.["simpleText"],
  319. "viewCountText": recommendVideo["viewCountText"]?.["simpleText"],
  320. "shortViewCountText": recommendVideo["shortViewCountText"]?.["simpleText"],
  321. "lengthText": recommendVideo["lengthText"]?.["simpleText"]
  322. })
  323. }
  324. }
  325. }
  326. }
  327. const videoDetails = {
  328. "isLiveContent": originVideoDetails["isLiveContent"],
  329. "title": originVideoDetails["title"],
  330. "thumbnails": thumbnails,
  331. "description": originVideoDetails["shortDescription"],
  332. "lengthSeconds": originVideoDetails["lengthSeconds"],
  333. "viewCount": originVideoDetails["viewCount"],
  334. "keywords": originVideoDetails["keywords"],
  335. "author": originVideoDetails["author"],
  336. "channelID": originVideoDetails["channelId"],
  337. "recommendInfo": recommendInfo,
  338. "channelURL": `https://www.youtube.com/channel/${originVideoDetails["channelId"]}`,
  339. "videoId": originVideoDetails["videoId"]
  340. }
  341. const ret = {
  342. "code": 200,
  343. "msg": "",
  344. "data": {
  345. "videoDetails": videoDetails,
  346. "streamingData": {
  347. "formats": formats.reverse()
  348. }
  349. },
  350. "id": "MusicDetailViewModel_detail_url"
  351. }
  352. console.log(`detail result: ${JSON.stringify(ret)}`);
  353. return ret;
  354. } catch (e) {
  355. const ret = {
  356. "code": -1,
  357. "msg": e.toString()
  358. }
  359. console.log(`detail result error: ${JSON.stringify(ret)}`);
  360. console.log(e);
  361. return ret;
  362. }
  363. }
  364. search = async (keyword, next, platform) => {
  365. try {
  366. console.log(`search keyword: ${keyword}`);
  367. console.log(`search next: ${next}`);
  368. if (next) {
  369. const nextObject = JSON.parse(next);
  370. const key = nextObject["key"];
  371. const body = {
  372. context: {
  373. client: {
  374. clientName: "WEB",
  375. clientVersion: "2.20240506.01.00",
  376. },
  377. },
  378. continuation: nextObject["continuation"]
  379. };
  380. let res = await request('POST', `https://www.youtube.com/youtubei/v1/search?key=${key}`, JSON.stringify(body), {}, platform);
  381. const {data, _} = res;
  382. res = JSON.parse(data);
  383. const videos = [];
  384. for (const item of res["onResponseReceivedCommands"][0]["appendContinuationItemsAction"]["continuationItems"][0]["itemSectionRenderer"]["contents"]) {
  385. const video = item["videoRenderer"];
  386. if (printable(platform)) {
  387. console.log(video);
  388. }
  389. if (video && video["videoId"]) {
  390. videos.push({
  391. "type": "videoWithContextRenderer",
  392. "data": {
  393. "videoId": video["videoId"],
  394. "title": video["title"]?.["runs"]?.[0]?.["text"],
  395. "thumbnails": video["thumbnail"]?.["thumbnails"],
  396. "channelName": video["longBylineText"]?.["runs"]?.[0]?.["text"],
  397. "publishedTimeText": video["publishedTimeText"]?.["simpleText"],
  398. "viewCountText": video["viewCountText"]?.["simpleText"],
  399. "shortViewCountText": video["shortViewCountText"]?.["simpleText"],
  400. "lengthText": video["lengthText"]?.["simpleText"]
  401. }
  402. });
  403. }
  404. }
  405. const ret = {
  406. "code": 200,
  407. "msg": "",
  408. "data": {
  409. "data": videos,
  410. "next": JSON.stringify({
  411. "key": nextObject["key"],
  412. "continuation": res["onResponseReceivedCommands"]?.[0]?.["appendContinuationItemsAction"]?.["continuationItems"]?.[1]?.["continuationItemRenderer"]?.["continuationEndpoint"]?.["continuationCommand"]?.["token"],
  413. }),
  414. },
  415. "id": "MusicSearchResultViewModel_search_result"
  416. }
  417. console.log(`[next] search result: ${JSON.stringify(ret)}`);
  418. return ret;
  419. } else {
  420. let url = `https://www.youtube.com/results?q=${encodeURIComponent(keyword)}&sp=EgIQAQ%253D%253D`;
  421. const htmlRes = await request('GET', url, null, {}, platform);
  422. const {data: html, _} = htmlRes;
  423. let regex = /var ytInitialData\s*=\s*({.*?});/;
  424. let match = html.match(regex);
  425. if (!match || !match.length) {
  426. console.log("can not found ytInitialData");
  427. throw new Error('JSON not found: ytInitialData');
  428. }
  429. const ytInitialDataResp = JSON.parse(match[1]);
  430. const videos = [];
  431. for (const item of ytInitialDataResp["contents"]?.["twoColumnSearchResultsRenderer"]?.["primaryContents"]?.["sectionListRenderer"]?.["contents"]?.[0]?.["itemSectionRenderer"]?.["contents"]) {
  432. if (item["videoRenderer"]) {
  433. const video = item["videoRenderer"];
  434. if (printable(platform)) {
  435. console.log(video);
  436. }
  437. if (video && video["videoId"]) {
  438. videos.push({
  439. "type": "videoWithContextRenderer",
  440. "data": {
  441. "videoId": video["videoId"],
  442. "title": video["title"]?.["runs"]?.[0]?.["text"],
  443. "thumbnails": video["thumbnail"]?.["thumbnails"],
  444. "channelName": video["longBylineText"]?.["runs"]?.[0]?.["text"],
  445. "publishedTimeText": video["publishedTimeText"]?.["simpleText"],
  446. "viewCountText": video["viewCountText"]?.["simpleText"],
  447. "shortViewCountText": video["shortViewCountText"]?.["simpleText"],
  448. "lengthText": video["lengthText"]?.["simpleText"]
  449. }
  450. });
  451. }
  452. }
  453. }
  454. let next = {};
  455. if (html.split("innertubeApiKey").length > 0) {
  456. next["key"] = html
  457. .split("innertubeApiKey")[1]
  458. .trim()
  459. .split(",")[0]
  460. .split('"')[2];
  461. }
  462. next["continuation"] = ytInitialDataResp["contents"]?.["twoColumnSearchResultsRenderer"]?.["primaryContents"]?.["sectionListRenderer"]?.["contents"]?.[1]?.["continuationItemRenderer"]?.["continuationEndpoint"]?.["continuationCommand"]?.["token"]
  463. const ret = {
  464. "code": 200,
  465. "msg": "",
  466. "data": {
  467. "data": videos,
  468. "next": JSON.stringify(next),
  469. },
  470. "id": "MusicSearchResultViewModel_search_result"
  471. }
  472. console.log(`unnext search result: ${JSON.stringify(ret)}`);
  473. return ret;
  474. }
  475. } catch (e) {
  476. const ret = {
  477. "code": -1,
  478. "msg": e.toString()
  479. }
  480. console.log(`search result error: ${JSON.stringify(ret)}`);
  481. return ret;
  482. }
  483. }