哔哩.js 2.9 KB

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