test.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { _ } from "./lib/cat.js";
  2. import {classes} from "http://127.0.0.1:9978/file/tvbox/aaabbbccc.js";
  3. import {filterObj} from "http://127.0.0.1:9978/file/tvbox/aaabbbccc.js";
  4. let key = '';
  5. let homeName = '';
  6. let HOST = 'https://api.bilibili.com';
  7. let siteKey = '';
  8. let siteType = 0;
  9. const PC_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.361";
  10. // let cookie = "DedeUserID=690781341;DedeUserID__ckMd5=cabc96906269c5b6;SESSDATA=2245ba24%2C1684212125%2C466fd%2Ab2;bili_jct=de6fdb60c10f8a83910aa55d79407b4e;"; // 可更换成自己的cookie
  11. let cookie = "http://127.0.0.1:9978/file/tvbox/bilicookie.txt"; // 可更换成自己的cookie
  12. async function request(reqUrl) {
  13. const res = await req(reqUrl, {
  14. headers: getMb(),
  15. });
  16. return res.content;
  17. }
  18. async function init(cfg) {
  19. siteKey = cfg.skey;
  20. siteType = cfg.stype;
  21. if (cookie.startsWith('http')) cookie = await request(cookie);
  22. // console.debug('我的哔哩 cookie =====>' + cookie); // js_debug.log
  23. }
  24. async function home(filter) {
  25. let classes=aaabbbccc.classes;
  26. let filterObj=aaabbbccc.filterObj;
  27. return JSON.stringify({
  28. class: classes,
  29. filters: filterObj,
  30. });
  31. }
  32. async function homeVod() {
  33. let html = HOST + '/x/web-interface/search/type?search_type=video&keyword='+homeName;
  34. let data = JSON.parse(await request(html)).data.result;
  35. let videos = [];
  36. data.forEach(function(it) {
  37. videos.push({
  38. vod_id: it.aid,
  39. vod_name: stripHtmlTag(it.title),
  40. vod_pic: 'http:'+it.pic,
  41. vod_remarks: turnDHM(it.duration) || '',
  42. });
  43. });
  44. return JSON.stringify({
  45. list: videos,
  46. });
  47. }
  48. async function category(tid, pg, filter, extend) {
  49. let html = HOST + '/x/web-interface/search/type?search_type=video&page=' + pg + '&keyword=' + (extend.tid || tid) + '&duration=' + (extend.duration || '') + '&order=' + (extend.order || '');
  50. let data = JSON.parse(await request(html)).data;
  51. let videos = [];
  52. data.result.forEach(function(it) {
  53. videos.push({
  54. vod_id: it.aid,
  55. vod_name: stripHtmlTag(it.title),
  56. vod_pic: 'https:' + it.pic,
  57. vod_remarks: turnDHM(it.duration) || '',
  58. });
  59. });
  60. return JSON.stringify({
  61. page: parseInt(data.page),
  62. pagecount: data.numPages,
  63. limit: 20,
  64. total: data.numResults,
  65. list: videos,
  66. });
  67. }
  68. async function detail(id) {
  69. let data = JSON.parse(await request(HOST + '/x/web-interface/view?aid=' + id)).data;
  70. let vod = {
  71. vod_id: data.aid,
  72. vod_name: stripHtmlTag(data.title),
  73. vod_pic: data.pic,
  74. type_name: data.tname,
  75. vod_year: new Date(data.pubdate*1000).getFullYear(),
  76. vod_remarks: data.duration || '',
  77. vod_director: data.owner.name,
  78. vod_content: stripHtmlTag(data.desc),
  79. };
  80. let episodes = data.pages;
  81. let playurls = [];
  82. episodes.forEach(function(it) {
  83. let cid = it.cid;
  84. let part = it.part.replace('#', '﹟').replace('$', '﹩');
  85. playurls.push(
  86. part + '$' + data.aid + '_' + cid
  87. )
  88. });
  89. let playUrl = playurls.join('#');
  90. vod.vod_play_from = 'B站';
  91. vod.vod_play_url = playUrl;
  92. return JSON.stringify({
  93. list: [vod],
  94. });
  95. }
  96. async function play(flag, id, flags) {
  97. let ids = id.split('_');
  98. let html = HOST + '/x/player/playurl?avid=' + ids[0] + '&cid=' + ids[1] + '&qn=116';
  99. let data = JSON.parse(await request(html)).data.durl;
  100. let maxSize = -1;
  101. let position = -1;
  102. data.forEach(function(it, i) {
  103. if (maxSize < Number(it.size)) {
  104. maxSize = Number(it.size);
  105. position = i
  106. }
  107. });
  108. let purl = '';
  109. if (data.length > 0) {
  110. if (position === -1) {
  111. position = 0
  112. }
  113. purl = data[position].url
  114. }
  115. // console.debug('我的哔哩 purl =====>' + purl); // js_debug.log
  116. return JSON.stringify({
  117. parse: 0,
  118. url: purl,
  119. header: getMb(),
  120. });
  121. }
  122. async function search(wd, quick, pg) {
  123. if (pg <= 0 || typeof(pg) == 'undefined') pg = 1;
  124. let html = HOST + '/x/web-interface/search/type?search_type=video&keyword=' + wd + '&page=' + pg;
  125. let data = JSON.parse(await request(html)).data;
  126. let videos = [];
  127. data.result.forEach(function(it) {
  128. videos.push({
  129. vod_id: it.aid,
  130. vod_name: stripHtmlTag(it.title),
  131. vod_pic: 'https:' + it.pic,
  132. vod_remarks: turnDHM(it.duration) || '',
  133. });
  134. });
  135. return JSON.stringify({
  136. page: parseInt(data.page),
  137. pagecount: data.numPages,
  138. limit: 20,
  139. total: data.numResults,
  140. list: videos,
  141. });
  142. }
  143. function getHeader(cookie) {
  144. let header = {};
  145. header['cookie'] = cookie;
  146. header['User-Agent'] = PC_UA;
  147. header['Referer'] = 'https://www.bilibili.com';
  148. return header;
  149. }
  150. function getMb() {
  151. return getHeader(cookie);
  152. }
  153. function stripHtmlTag(src) {
  154. return src
  155. .replace(/<\/?[^>]+(>|$)/g, '')
  156. .replace(/&.{1,5};/g, '')
  157. .replace(/\s{2,}/g, ' ');
  158. }
  159. function turnDHM(duration) {
  160. let min = duration.split(':')[0];
  161. let sec = duration.split(':')[1];
  162. if (min == 0) {
  163. return sec + '秒';
  164. } else if (0 < min && min < 60) {
  165. return min + '分';
  166. } else if (60 <= min && min < 1440) {
  167. if (min % 60 == 0) {
  168. let h = min / 60;
  169. return h + '小时';
  170. } else {
  171. let h = min / 60;
  172. h = (h + '').split('.')[0];
  173. let m = min % 60;
  174. return h + '小时' + m + '分';
  175. }
  176. } else if (min >= 1440) {
  177. let d = min / 60 / 24;
  178. d = (d + '').split('.')[0];
  179. let h = min / 60 % 24;
  180. h = (h + '').split('.')[0];
  181. let m = min % 60;
  182. let dhm = '';
  183. if (d > 0) {
  184. dhm = d + '天'
  185. }
  186. if (h >= 1) {
  187. dhm = dhm + h + '小时'
  188. }
  189. if (m > 0) {
  190. dhm = dhm + m + '分'
  191. }
  192. return dhm;
  193. }
  194. return null;
  195. }
  196. export function __jsEvalReturn() {
  197. return {
  198. init: init,
  199. home: home,
  200. homeVod: homeVod,
  201. category: category,
  202. detail: detail,
  203. play: play,
  204. search: search,
  205. };
  206. }