哔哩.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // print(env);
  2. // vipUrl = 'https://www.bilibili.com/bangumi/play/ep704873';
  3. fetch_params.headers = {
  4. 'User-Agent':PC_UA,
  5. "Referer": "https://www.bilibili.com",
  6. "Cookie":env.bili_cookie||''
  7. };
  8. let appkey = env.appkey||'';
  9. let access_key = env.access_key||'';
  10. // print(fetch_params);
  11. function getEpUrl(ssUrl){
  12. let html = request(ssUrl);
  13. let short_link = html.match(/short_link(.*?),/)[1];
  14. short_link = short_link.split(':').slice(1,-1).join('"').split('"')[1];
  15. let epUrl = unescape(short_link);
  16. return epUrl
  17. }
  18. function getCidEid(vipUrl){
  19. // print(vipUrl);
  20. let url = vipUrl.split('?')[0];
  21. // print(url);
  22. if(url.endsWith('/')){
  23. url = url.slice(0,-1);
  24. }
  25. if(url.includes('bilibili.com/video/')){
  26. let r = request(vipUrl);
  27. try{
  28. let mtext = r.match(/window\.__INITIAL_STATE__=(.*?);\(function/)[1];
  29. mtext = JSON.parse(mtext);
  30. let avid = mtext['aid'];
  31. let bvid = mtext['bvid'];
  32. let cid = mtext['videoData']['cid'];
  33. return [cid,avid,bvid]
  34. } catch (e) {
  35. return null
  36. }
  37. }else if(url.includes('/ep')){
  38. let epid = url.split('ep')[1];
  39. let data_url = 'https://api.bilibili.com/pgc/view/web/season?ep_id='+epid;
  40. let r = JSON.parse(request(data_url));
  41. if(r.code === 0){
  42. let episodes = r['result']['episodes'];
  43. let furl = url.replace('https://m.bilibili.com', 'https://www.bilibili.com');
  44. let now_ep = episodes.filter(function (it){
  45. return [it['short_link'], it['share_url'],it['link']].includes(furl)
  46. })[0];
  47. let avid = now_ep['aid'];
  48. let cid = now_ep['cid'];
  49. return [cid,avid,null];
  50. }else{
  51. return null
  52. }
  53. }else if(url.includes('/ss')){
  54. let epUrl = getEpUrl(vipUrl);
  55. return getCid(epUrl)
  56. }else{
  57. return null
  58. }
  59. }
  60. if(!/bilibili/.test(vipUrl)){
  61. realUrl = vipUrl;
  62. }else {
  63. try {
  64. // print(vipUrl);
  65. let ids = getCidEid(vipUrl);
  66. if (Array.isArray(ids)) {
  67. print(ids);
  68. let cid = ids[0];
  69. let avid = ids[1];
  70. let rurl = "https://api.bilibili.com/x/player/playurl?avid=" + avid + "&cid=" + cid + "&qn=120&type=&128=128&otype=json&fnver=&fourk=1&mid=&appkey=" + appkey + "&access_key=" + access_key;
  71. print(rurl);
  72. try {
  73. let r = JSON.parse(request(rurl));
  74. print(r);
  75. let purl = r['data']['durl'][0]['url'];
  76. print('purl:' + purl);
  77. // realUrl = purl;
  78. // https://upos-szbyjkm8g1.bilivideo.com
  79. realUrl = purl.replace(/.*bilivideo.*?\/(.*)/,'https://upos-szbyjkm8g1.bilivideo.com/$1');
  80. } catch (e) {
  81. print(e.message);
  82. realUrl = vipUrl;
  83. }
  84. } else {
  85. realUrl = vipUrl;
  86. }
  87. } catch (e) {
  88. print(e.message);
  89. }
  90. }