bili_open.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. import { Crypto, jinja2, _ } from 'assets://js/lib/cat.js';
  2. let siteKey = '';
  3. let siteType = 0;
  4. let cookie = '';
  5. let login = '';
  6. let vip = false;
  7. let extendObj = {};
  8. let bili_jct = '';
  9. let vod_audio_id = {
  10. 30280: 192000,
  11. 30232: 132000,
  12. 30216: 64000,
  13. };
  14. let vod_codec = {
  15. // 13: 'AV1',
  16. 12: 'HEVC',
  17. 7: 'AVC',
  18. };
  19. const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36';
  20. async function request(reqUrl, ua, buffer) {
  21. let res = await req(reqUrl, {
  22. method: 'get',
  23. headers: ua ? ua : { 'User-Agent': UA },
  24. timeout: 60000,
  25. buffer: buffer ? 1 : 0,
  26. });
  27. return res.content;
  28. }
  29. async function post(reqUrl, postData, ua, posttype) {
  30. let res = await req(reqUrl, {
  31. method: 'post',
  32. headers: ua ? ua : { 'User-Agent': UA },
  33. data: postData,
  34. timeout: 60000,
  35. postType: posttype,
  36. });
  37. return res.content;
  38. }
  39. function getHeaders() {
  40. const headers = {
  41. 'User-Agent': UA,
  42. };
  43. if (!_.isEmpty(cookie)) {
  44. headers.cookie = cookie;
  45. }
  46. return headers;
  47. }
  48. async function getCookie() {
  49. let result = await req('https://www.bilibili.com', {
  50. method: 'get',
  51. headers: { 'User-Agent': UA },
  52. timeout: 60000,
  53. });
  54. const setCookieHeaders = result.headers['set-cookie'];
  55. cookie = setCookieHeaders.map((kk) => kk.split(';')[0] + ';').join('');
  56. }
  57. async function init(cfg) {
  58. siteKey = cfg.skey;
  59. siteType = cfg.stype;
  60. let extend = cfg.ext;
  61. if (cfg.ext.hasOwnProperty('categories')) extend = cfg.ext.categories;
  62. if (cfg.ext.hasOwnProperty('cookie')) cookie = cfg.ext.cookie;
  63. // 获取csrf
  64. const cookies = cookie.split(';');
  65. cookies.forEach(cookie => {
  66. if (cookie.includes('bili_jct')) {
  67. bili_jct = cookie.split('=')[1];
  68. }
  69. });
  70. if (_.isEmpty(cookie)) await getCookie();
  71. let result = JSON.parse(await request('https://api.bilibili.com/x/web-interface/nav', getHeaders()));
  72. login = result.data.isLogin;
  73. vip = result.data.vipStatus;
  74. const ext = extend.split('#');
  75. const jsonData = [
  76. {
  77. key: 'order',
  78. name: '排序',
  79. value: [
  80. { n: '综合排序', v: '0' },
  81. { n: '最多点击', v: 'click' },
  82. { n: '最新发布', v: 'pubdate' },
  83. { n: '最多弹幕', v: 'dm' },
  84. { n: '最多收藏', v: 'stow' },
  85. ],
  86. },
  87. {
  88. key: 'duration',
  89. name: '时长',
  90. value: [
  91. { n: '全部时长', v: '0' },
  92. { n: '60分钟以上', v: '4' },
  93. { n: '30~60分钟', v: '3' },
  94. { n: '10~30分钟', v: '2' },
  95. { n: '10分钟以下', v: '1' },
  96. ],
  97. },
  98. ];
  99. const newarr = [];
  100. const d = {};
  101. const sc = {
  102. type_name: "首页",
  103. type_id: "首页",
  104. land: 1,
  105. ratio: 1.33,
  106. }
  107. newarr.push(sc);
  108. for (const kk of ext) {
  109. const c = {
  110. type_name: kk,
  111. type_id: kk,
  112. land: 1,
  113. ratio: 1.33,
  114. };
  115. newarr.push(c);
  116. d[kk] = jsonData;
  117. }
  118. if (!_.isEmpty(bili_jct)) {
  119. const hc = {
  120. type_name: "历史记录",
  121. type_id: "历史记录",
  122. land: 1,
  123. ratio: 1.33,
  124. }
  125. newarr.push(hc);
  126. }
  127. extendObj = {
  128. classes: newarr,
  129. filter: d,
  130. };
  131. }
  132. function home(filter) {
  133. try {
  134. const jSONObject = {
  135. class: extendObj.classes,
  136. };
  137. if (filter) {
  138. jSONObject.filters = extendObj.filter;
  139. }
  140. return JSON.stringify(jSONObject);
  141. } catch (e) {
  142. return '';
  143. }
  144. }
  145. async function homeVod() {
  146. try {
  147. const list = [];
  148. const url = 'https://api.bilibili.com/x/web-interface/index/top/rcmd?ps=14&fresh_idx=1&fresh_idx_1h=1';
  149. const response = await request(url, getHeaders());
  150. const responseData = JSON.parse(response);
  151. const vods = responseData.data.item;
  152. for (const item of vods) {
  153. const vod = {};
  154. let imageUrl = item.pic;
  155. if (imageUrl.startsWith('//')) {
  156. imageUrl = 'https:' + imageUrl;
  157. }
  158. let cd = getFullTime(item.duration);
  159. vod.vod_id = item.bvid;
  160. vod.vod_name = removeTags(item.title);
  161. vod.vod_pic = imageUrl;
  162. vod.vod_remarks = cd;
  163. vod.style = {
  164. type: 'rect',
  165. ratio: 1.33,
  166. },
  167. list.push(vod);
  168. }
  169. const result = { list: list };
  170. return JSON.stringify(result);
  171. } catch (e) { }
  172. }
  173. async function category(tid, page, filter, ext) {
  174. if (page < 1) page = 1;
  175. try {
  176. if (Object.keys(ext).length > 0 && ext.hasOwnProperty('tid') && ext['tid'].length > 0) {
  177. tid = ext['tid'];
  178. }
  179. let url = '';
  180. url = `https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword=${encodeURIComponent(tid)}`;
  181. if (Object.keys(ext).length > 0) {
  182. for (const k in ext) {
  183. if (k == 'tid') {
  184. continue;
  185. }
  186. url += `&${encodeURIComponent(k)}=${encodeURIComponent(ext[k])}`;
  187. }
  188. }
  189. url += `&page=${encodeURIComponent(page)}`;
  190. if (tid == "首页") {
  191. url = "https://api.bilibili.com/x/web-interface/index/top/rcmd?ps=14&fresh_idx=" + page + "&fresh_idx_1h=" + page;
  192. } else if (tid == "历史记录") {
  193. url = "https://api.bilibili.com/x/v2/history?pn=" + page;
  194. }
  195. const data = JSON.parse(await request(url, getHeaders())).data;
  196. let items = data.result;
  197. if (tid == "首页") {
  198. items = data.item;
  199. } else if (tid == "历史记录") {
  200. items = data;
  201. }
  202. const videos = [];
  203. for (const item of items) {
  204. const video = {};
  205. let pic = item.pic;
  206. if (pic.startsWith('//')) {
  207. pic = 'https:' + pic;
  208. }
  209. let cd = getFullTime(item.duration);
  210. video.vod_remarks = cd;
  211. video.vod_id = item.bvid;
  212. video.vod_name = removeTags(item.title);
  213. video.vod_pic = pic;
  214. video.style = {
  215. type: 'rect',
  216. ratio: 1.33,
  217. },
  218. videos.push(video);
  219. }
  220. const result = {
  221. page: page,
  222. pagecount: data.numPages ?? (page + 1),
  223. limit: videos.length,
  224. total: videos.length * (page + 1),
  225. list: videos,
  226. };
  227. return JSON.stringify(result);
  228. } catch (e) { }
  229. return null;
  230. }
  231. async function detail(ids) {
  232. try {
  233. const bvid = ids;
  234. const detailUrl = `https://api.bilibili.com/x/web-interface/view?bvid=${bvid}`;
  235. const detailData = JSON.parse(await request(detailUrl, getHeaders())).data;
  236. // 记录历史
  237. if (!_.isEmpty(bili_jct)) {
  238. const historyReport = 'https://api.bilibili.com/x/v2/history/report';
  239. let dataPost = {
  240. aid: detailData.aid,
  241. cid: detailData.cid,
  242. csrf: bili_jct,
  243. }
  244. await post(historyReport, dataPost, getHeaders(), 'form');
  245. }
  246. let cd = getFullTime(detailData.duration);
  247. const aid = detailData.aid;
  248. const video = {
  249. vod_id: bvid,
  250. vod_name: detailData.title,
  251. vod_pic: detailData.pic,
  252. type_name: detailData.tname,
  253. vod_year: '',
  254. vod_area: '',
  255. vod_remarks: cd,
  256. vod_actor: '',
  257. vod_director: '',
  258. vod_content: detailData.desc,
  259. };
  260. const playurldata = 'https://api.bilibili.com/x/player/playurl?avid=' + aid + '&cid=' + detailData.cid + '&qn=127&fnval=4048&fourk=1';
  261. const playurldatas = JSON.parse(await request(playurldata, getHeaders()));
  262. const playurldatalist = playurldatas.data;
  263. const accept_quality = playurldatalist.accept_quality;
  264. const accept_description = playurldatalist.accept_description;
  265. const qualitylist = [];
  266. const descriptionList = [];
  267. for (let i = 0; i < accept_quality.length; i++) {
  268. if (!vip) {
  269. if (!login) {
  270. if (accept_quality[i] > 32) continue;
  271. } else {
  272. if (accept_quality[i] > 80) continue;
  273. }
  274. }
  275. descriptionList.push(base64Encode(accept_description[i]));
  276. qualitylist.push(accept_quality[i]);
  277. }
  278. let treeMap = {};
  279. const jSONArray = detailData.pages;
  280. let playList = [];
  281. for (let j = 0; j < jSONArray.length; j++) {
  282. const jSONObject6 = jSONArray[j];
  283. const cid = jSONObject6.cid;
  284. const playUrl = j + '$' + aid + '+' + cid + '+' + qualitylist.join(':') + '+' + descriptionList.join(':');
  285. playList.push(playUrl);
  286. }
  287. treeMap['dash'] = playList.join('#');
  288. treeMap['mp4'] = playList.join('#');
  289. const relatedUrl = 'https://api.bilibili.com/x/web-interface/archive/related?bvid=' + bvid;
  290. const relatedData = JSON.parse(await request(relatedUrl, getHeaders())).data;
  291. playList = [];
  292. for (let j = 0; j < relatedData.length; j++) {
  293. const jSONObject6 = relatedData[j];
  294. const cid = jSONObject6.cid;
  295. const title = jSONObject6.title;
  296. const aaid = jSONObject6.aid;
  297. const playUrl = title + '$' + aaid + '+' + cid + '+' + qualitylist.join(':') + '+' + descriptionList.join(':');
  298. playList.push(playUrl);
  299. }
  300. treeMap['相关'] = playList.join('#');
  301. video.vod_play_from = Object.keys(treeMap).join("$$$");
  302. video.vod_play_url = Object.values(treeMap).join("$$$");
  303. const list = [video];
  304. const result = { list };
  305. return JSON.stringify(result);
  306. } catch (e) { }
  307. return null;
  308. }
  309. async function play(flag, id, flags) {
  310. try {
  311. const playHeaders = { Referer: 'https://www.bilibili.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36' };
  312. const ids = id.split('+');
  313. const aid = ids[0];
  314. const cid = ids[1];
  315. const qualityIds = ids[2].split(':');
  316. const qualityName = ids[3].split(':');
  317. const dan = 'https://api.bilibili.com/x/v1/dm/list.so?oid=' + cid;
  318. if (flag == 'dash' || flag == '相关') {
  319. // dash mpd 代理
  320. const js2Base = await js2Proxy(true, siteType, siteKey, 'dash/', {});
  321. let urls = [];
  322. for (let i = 0; i < qualityIds.length; i++) {
  323. urls.push(base64Decode(qualityName[i]), js2Base + base64Encode(aid + '+' + cid + '+' + qualityIds[i]));
  324. }
  325. return JSON.stringify({
  326. parse: 0,
  327. url: urls,
  328. danmaku: dan,
  329. header: playHeaders,
  330. });
  331. } else if (flag == 'mp4') {
  332. // 直链
  333. let urls = [];
  334. for (let i = 0; i < qualityIds.length; i++) {
  335. const url = `https://api.bilibili.com/x/player/playurl?avid=${aid}&cid=${cid}&qn=${qualityIds[i]}&fourk=1`;
  336. const resp = JSON.parse(await request(url, getHeaders()));
  337. const data = resp.data;
  338. if (data.quality != qualityIds[i]) continue;
  339. let durl = data.durl[0].url;
  340. urls.push(base64Decode(qualityName[i]), durl);
  341. }
  342. return JSON.stringify({
  343. parse: 0,
  344. url: urls,
  345. danmaku: dan,
  346. header: playHeaders,
  347. });
  348. } else {
  349. // 音频外挂
  350. let urls = [];
  351. let audios = [];
  352. for (let i = 0; i < qualityIds.length; i++) {
  353. const url = `https://api.bilibili.com/x/player/playurl?avid=${aid}&cid=${cid}&qn=${qualityIds[i]}&fnval=4048&fourk=1`;
  354. let resp = JSON.parse(await request(url, getHeaders()));
  355. const dash = resp.data.dash;
  356. const video = dash.video;
  357. const audio = dash.audio;
  358. for (let j = 0; j < video.length; j++) {
  359. const dashjson = video[j];
  360. if (dashjson.id == qualityIds[i]) {
  361. for (const key in vod_codec) {
  362. if (dashjson.codecid == key) {
  363. urls.push(base64Decode(qualityName[i]) + ' ' + vod_codec[key], dashjson.baseUrl);
  364. }
  365. }
  366. }
  367. }
  368. if (audios.length == 0) {
  369. for (let j = 0; j < audio.length; j++) {
  370. const dashjson = audio[j];
  371. for (const key in vod_audio_id) {
  372. if (dashjson.id == key) {
  373. audios.push({
  374. title: _.floor(parseInt(vod_audio_id[key]) / 1024) + 'Kbps',
  375. bit: vod_audio_id[key],
  376. url: dashjson.baseUrl,
  377. });
  378. }
  379. }
  380. }
  381. audios = _.sortBy(audios, 'bit');
  382. }
  383. }
  384. return JSON.stringify({
  385. parse: 0,
  386. url: urls,
  387. extra: {
  388. audio: audios,
  389. },
  390. header: playHeaders,
  391. });
  392. }
  393. } catch (e) { }
  394. return null;
  395. }
  396. async function search(key, quick, pg) {
  397. let page = pg || 1;
  398. if (page == 0) page = 1;
  399. try {
  400. const ext = {
  401. duration: '0',
  402. };
  403. let resp = JSON.parse(await category(key, page, true, ext));
  404. const catVideos = resp.list;
  405. const pageCount = resp.pagecount;
  406. const videos = [];
  407. for (let i = 0; i < catVideos.length; ++i) {
  408. videos.push(catVideos[i]);
  409. }
  410. const result = {
  411. page: page,
  412. pagecount: pageCount,
  413. land: 1,
  414. ratio: 1.33,
  415. list: videos,
  416. };
  417. return JSON.stringify(result);
  418. } catch (e) { }
  419. return null;
  420. }
  421. async function proxy(segments, headers) {
  422. let what = segments[0];
  423. let url = base64Decode(segments[1]);
  424. if (what == 'dash') {
  425. const ids = url.split('+');
  426. const aid = ids[0];
  427. const cid = ids[1];
  428. const str5 = ids[2];
  429. const urls = `https://api.bilibili.com/x/player/playurl?avid=${aid}&cid=${cid}&qn=${str5}&fnval=4048&fourk=1`;
  430. let videoList = '';
  431. let audioList = '';
  432. let resp = JSON.parse(await request(urls, getHeaders()));
  433. const dash = resp.data.dash;
  434. const video = dash.video;
  435. const audio = dash.audio;
  436. for (let i = 0; i < video.length; i++) {
  437. // if (i > 0) continue; // 只取一个
  438. const dashjson = video[i];
  439. if (dashjson.id == str5) {
  440. videoList += getDashMedia(dashjson);
  441. }
  442. }
  443. for (let i = 0; i < audio.length; i++) {
  444. // if (i > 0) continue;
  445. const ajson = audio[i];
  446. for (const key in vod_audio_id) {
  447. if (ajson.id == key) {
  448. audioList += getDashMedia(ajson);
  449. }
  450. }
  451. }
  452. let mpd = getDash(resp, videoList, audioList);
  453. return JSON.stringify({
  454. code: 200,
  455. content: mpd,
  456. headers: {
  457. 'Content-Type': 'application/dash+xml',
  458. },
  459. });
  460. }
  461. return JSON.stringify({
  462. code: 500,
  463. content: '',
  464. });
  465. }
  466. function getDashMedia(dash) {
  467. try {
  468. let qnid = dash.id;
  469. const codecid = dash.codecid;
  470. const media_codecs = dash.codecs;
  471. const media_bandwidth = dash.bandwidth;
  472. const media_startWithSAP = dash.startWithSap;
  473. const media_mimeType = dash.mimeType;
  474. const media_BaseURL = dash.baseUrl.replace(/&/g, '&amp;');
  475. const media_SegmentBase_indexRange = dash.SegmentBase.indexRange;
  476. const media_SegmentBase_Initialization = dash.SegmentBase.Initialization;
  477. const mediaType = media_mimeType.split('/')[0];
  478. let media_type_params = '';
  479. if (mediaType == 'video') {
  480. const media_frameRate = dash.frameRate;
  481. const media_sar = dash.sar;
  482. const media_width = dash.width;
  483. const media_height = dash.height;
  484. media_type_params = `height='${media_height}' width='${media_width}' frameRate='${media_frameRate}' sar='${media_sar}'`;
  485. } else if (mediaType == 'audio') {
  486. for (const key in vod_audio_id) {
  487. if (qnid == key) {
  488. const audioSamplingRate = vod_audio_id[key];
  489. media_type_params = `numChannels='2' sampleRate='${audioSamplingRate}'`;
  490. }
  491. }
  492. }
  493. qnid += '_' + codecid;
  494. return `<AdaptationSet lang="chi">
  495. <ContentComponent contentType="${mediaType}"/>
  496. <Representation id="${qnid}" bandwidth="${media_bandwidth}" codecs="${media_codecs}" mimeType="${media_mimeType}" ${media_type_params} startWithSAP="${media_startWithSAP}">
  497. <BaseURL>${media_BaseURL}</BaseURL>
  498. <SegmentBase indexRange="${media_SegmentBase_indexRange}">
  499. <Initialization range="${media_SegmentBase_Initialization}"/>
  500. </SegmentBase>
  501. </Representation>
  502. </AdaptationSet>`;
  503. } catch (e) {
  504. // Handle exceptions here
  505. }
  506. }
  507. function getDash(ja, videoList, audioList) {
  508. const duration = ja.data.dash.duration;
  509. const minBufferTime = ja.data.dash.minBufferTime;
  510. return `<MPD xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:mpeg:dash:schema:mpd:2011" xsi:schemaLocation="urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd" type="static" mediaPresentationDuration="PT${duration}S" minBufferTime="PT${minBufferTime}S" profiles="urn:mpeg:dash:profile:isoff-on-demand:2011">
  511. <Period duration="PT${duration}S" start="PT0S">
  512. ${videoList}
  513. ${audioList}
  514. </Period>
  515. </MPD>`;
  516. }
  517. function base64Encode(text) {
  518. return Crypto.enc.Base64.stringify(Crypto.enc.Utf8.parse(text));
  519. }
  520. function base64Decode(text) {
  521. return Crypto.enc.Utf8.stringify(Crypto.enc.Base64.parse(text));
  522. }
  523. function removeTags(input) {
  524. return input.replace(/<[^>]*>/g, '');
  525. }
  526. function getFullTime(numberSec) {
  527. let totalSeconds = '';
  528. try {
  529. var timeParts = numberSec.split(":");
  530. var min = parseInt(timeParts[0]);
  531. var sec = parseInt(timeParts[1]);
  532. totalSeconds = min * 60 + sec;
  533. } catch (e) {
  534. totalSeconds = parseInt(numberSec);
  535. }
  536. if (isNaN(totalSeconds)) {
  537. return '无效输入';
  538. }
  539. if (totalSeconds >= 3600) {
  540. const hours = Math.floor(totalSeconds / 3600);
  541. const remainingSecondsAfterHours = totalSeconds % 3600;
  542. const minutes = Math.floor(remainingSecondsAfterHours / 60);
  543. const seconds = remainingSecondsAfterHours % 60;
  544. return `${hours}小时 ${minutes}分钟 ${seconds}秒`;
  545. } else {
  546. const minutes = Math.floor(totalSeconds / 60);
  547. const seconds = totalSeconds % 60;
  548. return `${minutes}分钟 ${seconds}秒`;
  549. }
  550. }
  551. export function __jsEvalReturn() {
  552. return {
  553. init: init,
  554. home: home,
  555. homeVod: homeVod,
  556. category: category,
  557. detail: detail,
  558. play: play,
  559. proxy: proxy,
  560. search: search,
  561. };
  562. }