youtubev1.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. console.log('v1')
  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 = {}, requestId, platform) => {
  79. if (platform === "WEB") {
  80. url = url.replace("https://www.youtube.com/", "http://16.162.163.175:80/");
  81. url = url.replace("https://music.youtube.com/", "http://16.162.163.175:80/");
  82. }
  83. console.log(`request url:${url}`)
  84. console.log(`request data:${data}`)
  85. console.log(`request method:${method}`)
  86. console.log(`request headers:${JSON.stringify((headers))}`)
  87. if (platform === "WEB") {
  88. const res = await fetch(url, {
  89. 'mode': 'cors',
  90. 'method': method,
  91. 'headers': headers,
  92. 'body': data
  93. })
  94. const resData = await res.text()
  95. return Promise.resolve({
  96. 'data': resData,
  97. 'headers': res.headers
  98. });
  99. }
  100. return new Promise((resolve, reject) => {
  101. AF.request(url, method, data, headers, requestId, (data, headers, err) => {
  102. if (err) {
  103. console.log(`request error: ${err}`);
  104. reject(err);
  105. } else {
  106. console.log(`response headers: ${headers}`);
  107. resolve({
  108. 'data': data,
  109. 'headers': JSON.parse(headers)
  110. });
  111. }
  112. });
  113. })
  114. }
  115. detail = async (url, requestId, platform) => {
  116. try {
  117. // fetch recommend
  118. const recommendInfo = [];
  119. const htmlResp = await request('GET', `${url}&bpctr=9999999999&has_verified=1`, null, {
  120. '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',
  121. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  122. 'Accept-Language': 'en-us,en;q=0.5',
  123. 'Sec-Fetch-Mode': 'navigate',
  124. 'Accept-Encoding': 'gzip, deflate, br',
  125. 'Cookie': 'PREF=hl=en&tz=UTC; SOCS=CAI'
  126. }, requestId, platform);
  127. let {data: html, headers: htmlHeaders} = htmlResp;
  128. let regex = /var ytInitialPlayerResponse\s*=\s*({.*?});/;
  129. let match = html.match(regex);
  130. if (match) {
  131. const ytInitialPlayerResponse = JSON.parse(match[1]);
  132. console.log(ytInitialPlayerResponse);
  133. const originVideoDetails = ytInitialPlayerResponse['videoDetails'];
  134. const ytInitialDataMatch = html.match(/var ytInitialData\s*=\s*({.*?});/);
  135. if (ytInitialDataMatch && ytInitialDataMatch.length === 2) {
  136. const ytInitialData = JSON.parse(ytInitialDataMatch[1]);
  137. console.log(ytInitialData);
  138. for (const item of ytInitialData["contents"]?.["twoColumnWatchNextResults"]?.["secondaryResults"]?.["secondaryResults"]?.["results"] || []) {
  139. if (item["compactVideoRenderer"]) {
  140. const recommendVideo = item["compactVideoRenderer"];
  141. console.log(`recommend video: ${JSON.stringify(recommendVideo)}`);
  142. if (recommendVideo["videoId"]) {
  143. recommendInfo.push({
  144. "type": "gridVideoRenderer",
  145. "videoId": recommendVideo["videoId"],
  146. "title": recommendVideo["title"]?.["simpleText"],
  147. "thumbnails": recommendVideo["thumbnail"]?.["thumbnails"],
  148. "channelName": recommendVideo["longBylineText"]?.["runs"]?.[0]?.["text"],
  149. "publishedTimeText": recommendVideo["publishedTimeText"]?.["simpleText"],
  150. "viewCountText": recommendVideo["viewCountText"]?.["simpleText"],
  151. "shortViewCountText": recommendVideo["shortViewCountText"]?.["simpleText"],
  152. "lengthText": recommendVideo["lengthText"]?.["simpleText"]
  153. })
  154. }
  155. }
  156. }
  157. }
  158. }
  159. // android
  160. let thumbnails = [];
  161. let originFormats = [];
  162. let originVideoDetails = undefined;
  163. try {
  164. const apiUrl = `https://music.youtube.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8`;
  165. const apiResp = await request('POST', apiUrl, JSON.stringify({
  166. "context": {
  167. "client": {
  168. "clientName": "ANDROID",
  169. "hl": "en",
  170. "clientVersion": "18.49.37",
  171. "gl": "US"
  172. }
  173. },
  174. "videoId": url.replace('https://www.youtube.com/watch?v=', ''),
  175. "params": "CgIQBg"
  176. }), {
  177. 'Host': 'www.youtube.com',
  178. 'Connection': 'keep-alive',
  179. 'User-Agent': 'com.google.android.apps.youtube.music/17.31.35 (Linux; U; Android 11) gzip',
  180. 'Accept-Language': 'en-US,en',
  181. 'Cookie': parseSetCookie(htmlHeaders),
  182. 'Content-Type': 'application/json'
  183. }, requestId, platform);
  184. let {data: apiData, _} = apiResp;
  185. console.log(`android api result: ${JSON.stringify(apiResp)}`);
  186. const res = JSON.parse(apiData);
  187. const currentFormats = [];
  188. originVideoDetails = res["videoDetails"];
  189. for (const format of [].concat(res["streamingData"]["formats"] || []).concat(res["streamingData"]["adaptiveFormats"] || [])) {
  190. if (format) {
  191. format["from"] = "android"
  192. currentFormats.push(format);
  193. }
  194. }
  195. originFormats = originFormats.concat(currentFormats);
  196. } catch (e) {
  197. console.log(`can not found format android api error: ${e}`);
  198. const ret = {
  199. "code": -1,
  200. "msg": e.toString(),
  201. "requestId": requestId
  202. }
  203. console.log(`detail2 result error: ${JSON.stringify(ret)}`);
  204. return ret;
  205. }
  206. console.log(`after android api, format size:${originFormats.length}`);
  207. let qualities = [];
  208. const formats = [];
  209. for (let format of originFormats) {
  210. if (printable(platform)) {
  211. console.log(format);
  212. }
  213. if (format && qualities.indexOf(format['itag']) === -1) {
  214. if (format["url"]) {
  215. const {vcodec, acodec} = parseCodecs(format)
  216. if (vcodec && acodec) {
  217. const current = {
  218. "width": format["width"] + "",
  219. "height": format["height"] + "",
  220. "type": format["mimeType"],
  221. "quality": format["qualityLabel"],
  222. "itag": format["itag"],
  223. "fps": format["fps"] + "",
  224. "bitrate": format["bitrate"] + "",
  225. "url": format["url"],
  226. "ext": "mp4",
  227. "vcodec": vcodec,
  228. "acodec": acodec,
  229. "vbr": "0",
  230. "abr": "0",
  231. "container": "mp4_dash",
  232. "from": format["from"],
  233. "source": format
  234. }
  235. formats.push(current)
  236. qualities.push(format["qualityLabel"]);
  237. }
  238. }
  239. }
  240. }
  241. for (const item of originVideoDetails['thumbnail']['thumbnails']) {
  242. thumbnails.push({
  243. 'url': item['url'], 'width': item['width'] + "", 'height': item['height'] + ""
  244. })
  245. }
  246. formats.sort((a, b) => parseInt(a["height"]) - parseInt(b["height"]));
  247. const videoDetails = {
  248. "isLiveContent": originVideoDetails["isLiveContent"],
  249. "title": originVideoDetails["title"],
  250. "thumbnails": thumbnails,
  251. "description": originVideoDetails["shortDescription"],
  252. "lengthSeconds": originVideoDetails["lengthSeconds"],
  253. "viewCount": originVideoDetails["viewCount"],
  254. "keywords": originVideoDetails["keywords"],
  255. "author": originVideoDetails["author"],
  256. "channelID": originVideoDetails["channelId"],
  257. "recommendInfo": recommendInfo,
  258. "channelURL": `https://www.youtube.com/channel/${originVideoDetails["channelId"]}`,
  259. "videoId": originVideoDetails["videoId"]
  260. }
  261. const ret = {
  262. "code": 200,
  263. "msg": "",
  264. "data": {
  265. "videoDetails": videoDetails,
  266. "streamingData": {
  267. "formats": formats
  268. }
  269. },
  270. "id": "MusicDetailViewModel_detail_url",
  271. "requestId": requestId
  272. }
  273. console.log(`detail result: ${JSON.stringify(ret)}`);
  274. return ret;
  275. } catch (e) {
  276. const ret = {
  277. "code": -1,
  278. "msg": e.toString(),
  279. "requestId": requestId
  280. }
  281. console.log(`detail2 result error: ${JSON.stringify(ret)}`);
  282. console.log(e);
  283. return ret;
  284. }
  285. }
  286. search = async (keyword, next, requestId, platform) => {
  287. try {
  288. console.log(`search keyword: ${keyword}`);
  289. console.log(`search next: ${next}`);
  290. if (next) {
  291. const nextObject = JSON.parse(next);
  292. const key = nextObject["key"];
  293. const body = {
  294. context: {
  295. client: {
  296. clientName: "WEB",
  297. clientVersion: "2.20240506.01.00",
  298. },
  299. },
  300. continuation: nextObject["continuation"]
  301. };
  302. let res = await request('POST', `https://www.youtube.com/youtubei/v1/search?key=${key}`, JSON.stringify(body), {}, requestId, platform);
  303. const {data, _} = res;
  304. res = JSON.parse(data);
  305. const videos = [];
  306. for (const item of res["onResponseReceivedCommands"][0]["appendContinuationItemsAction"]["continuationItems"][0]["itemSectionRenderer"]["contents"]) {
  307. const video = item["videoRenderer"];
  308. if (printable(platform)) {
  309. console.log(video);
  310. }
  311. if (video && video["videoId"] && video["lengthText"]) {
  312. videos.push({
  313. "type": "videoWithContextRenderer",
  314. "data": {
  315. "videoId": video["videoId"],
  316. "title": video["title"]?.["runs"]?.[0]?.["text"],
  317. "thumbnails": video["thumbnail"]?.["thumbnails"],
  318. "channelName": video["longBylineText"]?.["runs"]?.[0]?.["text"],
  319. "publishedTimeText": video["publishedTimeText"]?.["simpleText"],
  320. "viewCountText": video["viewCountText"]?.["simpleText"],
  321. "shortViewCountText": video["shortViewCountText"]?.["simpleText"],
  322. "lengthText": video["lengthText"]?.["simpleText"]
  323. }
  324. });
  325. }
  326. }
  327. const ret = {
  328. "code": 200,
  329. "msg": "",
  330. "data": {
  331. "data": videos,
  332. "next": JSON.stringify({
  333. "key": nextObject["key"],
  334. "continuation": res["onResponseReceivedCommands"]?.[0]?.["appendContinuationItemsAction"]?.["continuationItems"]?.[1]?.["continuationItemRenderer"]?.["continuationEndpoint"]?.["continuationCommand"]?.["token"],
  335. }),
  336. },
  337. "id": "MusicSearchResultViewModel_search_result",
  338. "requestId": requestId
  339. }
  340. console.log(`[next] search result: ${JSON.stringify(ret)}`);
  341. return ret;
  342. } else {
  343. let url = `https://www.youtube.com/results?q=${encodeURIComponent(keyword)}&sp=EgIQAQ%253D%253D`;
  344. const htmlRes = await request('GET', url, null, {}, requestId, platform);
  345. const {data: html, _} = htmlRes;
  346. let regex = /var ytInitialData\s*=\s*({.*?});/;
  347. let match = html.match(regex);
  348. if (!match || !match.length) {
  349. console.log("can not found ytInitialData");
  350. throw new Error('JSON not found: ytInitialData');
  351. }
  352. const ytInitialDataResp = JSON.parse(match[1]);
  353. if (printable(platform)) {
  354. console.log(ytInitialDataResp);
  355. }
  356. const videos = [];
  357. const contents = ytInitialDataResp["contents"]?.["twoColumnSearchResultsRenderer"]?.["primaryContents"]?.["sectionListRenderer"]?.["contents"] || []
  358. for (const content of contents) {
  359. const currentContents = content["itemSectionRenderer"]?.["contents"]
  360. if (Array.isArray(currentContents)) {
  361. for (const currentContent of currentContents) {
  362. if (currentContent["videoRenderer"]) {
  363. const video = currentContent["videoRenderer"];
  364. if (printable(platform)) {
  365. console.log(video);
  366. }
  367. if (video && video["videoId"] && video["lengthText"]) {
  368. videos.push({
  369. "type": "videoWithContextRenderer",
  370. "data": {
  371. "videoId": video["videoId"],
  372. "title": video["title"]?.["runs"]?.[0]?.["text"],
  373. "thumbnails": video["thumbnail"]?.["thumbnails"],
  374. "channelName": video["longBylineText"]?.["runs"]?.[0]?.["text"],
  375. "publishedTimeText": video["publishedTimeText"]?.["simpleText"],
  376. "viewCountText": video["viewCountText"]?.["simpleText"],
  377. "shortViewCountText": video["shortViewCountText"]?.["simpleText"],
  378. "lengthText": video["lengthText"]?.["simpleText"]
  379. }
  380. });
  381. }
  382. }
  383. }
  384. }
  385. }
  386. let next = {};
  387. if (html.split("innertubeApiKey").length > 0) {
  388. next["key"] = html
  389. .split("innertubeApiKey")[1]
  390. .trim()
  391. .split(",")[0]
  392. .split('"')[2];
  393. }
  394. next["continuation"] = contents[contents.length - 1]?.["continuationItemRenderer"]?.["continuationEndpoint"]?.["continuationCommand"]?.["token"]
  395. const ret = {
  396. "code": 200,
  397. "msg": "",
  398. "data": {
  399. "data": videos,
  400. "next": JSON.stringify(next),
  401. },
  402. "id": "MusicSearchResultViewModel_search_result",
  403. "requestId": requestId
  404. }
  405. console.log(`unnext search result: ${JSON.stringify(ret)}`);
  406. return ret;
  407. }
  408. } catch (e) {
  409. const ret = {
  410. "code": -1,
  411. "msg": e.toString(),
  412. "requestId": requestId
  413. }
  414. console.log(`search result error: ${JSON.stringify(ret)}`);
  415. return ret;
  416. }
  417. }