koishi-plugin-bilibili-videolink-analysis 1.0.2 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +114 -81
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -77,11 +77,11 @@ exports.Config = Schema.intersect([
77
77
  // Schema.const('2').description('高清晰度优先(清晰的还是去B站看吧)'),
78
78
  //]).role('radio').default('1').description("发送的视频清晰度优先策略"),
79
79
  BVnumberParsing: Schema.boolean().default(true).description("是否允许根据`独立的BV号`解析视频 `开启后,可以通过视频的BV号解析视频。` <br> [触发说明见README](https://www.npmjs.com/package/koishi-plugin-bilibili-videolink-analysis)"),
80
- //Maximumduration: Schema.number().default(25).description("允许解析的视频最大时长(分钟)`超过这个时长 就不会发视频`").min(1),
81
- //Maximumduration_tip: Schema.union([
82
- // Schema.const('不返回文字提示').description('不返回文字提示'),
83
- // Schema.string().description('返回文字提示(请在右侧填写文字内容)').default('视频太长啦!还是去B站看吧~'),
84
- //]).description("对过长视频的文字提示内容").default('视频太长啦!还是去B站看吧~'),
80
+ Maximumduration: Schema.number().default(25).description("允许解析的视频最大时长(分钟)`超过这个时长 就不会发视频`").min(1),
81
+ Maximumduration_tip: Schema.union([
82
+ Schema.const('不返回文字提示').description('不返回文字提示'),
83
+ Schema.string().description('返回文字提示(请在右侧填写文字内容)').default('视频太长啦!还是去B站看吧~'),
84
+ ]).description("对过长视频的文字提示内容").default('视频太长啦!还是去B站看吧~'),
85
85
  MinimumTimeInterval: Schema.number().default(180).description("若干`秒`内 不再处理相同链接 `防止多bot互相触发 导致的刷屏/性能浪费`").min(1),
86
86
  }).description("基础设置"),
87
87
 
@@ -458,19 +458,50 @@ display: none !important;
458
458
  }
459
459
  }
460
460
  })
461
-
462
- async function handleBilibiliMedia(lastretUrl, config) {
461
+ /*async function handleBilibiliMedia(config, session, lastretUrl) {
463
462
  const fullAPIurl = `https://api.xingzhige.com/API/b_parse/?url=${encodeURIComponent(lastretUrl)}`;
464
463
 
465
464
  try {
466
465
  // 发起请求,解析 Bilibili 视频信息
467
- const data = await ctx.http.get(fullAPIurl);
468
- // 检查返回的状态码是否为0,表示成功
469
- if (data.code === 0 && data.msg === "video" && data.data && data.data.video) {
470
- const videoData = data.data.video;
471
- const videoUrl = videoData.url; // 视频直链
472
- // 返回视频直链
473
- return videoUrl;
466
+ const responseData = await ctx.http.get(fullAPIurl);
467
+
468
+ // 检查返回状态码是否为0且为视频内容
469
+ if (responseData.code === 0 && responseData.msg === "video" && responseData.data) {
470
+ const { bvid, cid } = responseData.data;
471
+
472
+ // 请求 Bilibili 播放 URL,获取视频信息
473
+ const bilibiliUrl = `https://api.bilibili.com/x/player/playurl?fnval=80&cid=${cid}&bvid=${bvid}`;
474
+ const playData = await ctx.http.get(bilibiliUrl);
475
+ //////
476
+ ctx.logger.info(bilibiliUrl)
477
+ // 检查返回的状态码是否为0,表示请求成功
478
+ if (playData.code === 0 && playData.data && playData.data.dash.duration) {
479
+ const videoDurationSeconds = playData.data.dash.duration; // 视频时长,单位为秒
480
+ const videoDurationMinutes = videoDurationSeconds / 60; // 转换为分钟
481
+
482
+ // 检查视频时长是否超过配置的最大允许时长
483
+ if (videoDurationMinutes > config.Maximumduration) {
484
+ // 视频时长超过最大限制,返回提示
485
+ if (config.Maximumduration_tip !== '不返回文字提示') {
486
+ await session.send(config.Maximumduration_tip)
487
+ return next()
488
+ } else {
489
+ return null; // 不返回提示信息
490
+ }
491
+ }
492
+
493
+ // 视频时长符合要求,继续解析并返回视频直链
494
+ const videoUrl = responseData.data.video.url;
495
+ //////
496
+ ctx.logger.info(videoUrl)
497
+ if (videoUrl) {
498
+ return videoUrl; // 返回视频直链
499
+ } else {
500
+ throw new Error("解析视频直链失败");
501
+ }
502
+ } else {
503
+ throw new Error("获取播放数据失败");
504
+ }
474
505
  } else {
475
506
  throw new Error("解析视频信息失败或非视频类型内容");
476
507
  }
@@ -478,7 +509,8 @@ display: none !important;
478
509
  logger.error("请求解析 API 失败或处理出错:", error);
479
510
  return null;
480
511
  }
481
- }
512
+ }*/
513
+
482
514
 
483
515
  //判断是否需要解析
484
516
  async function isProcessLinks(session, config, ctx, lastProcessedUrls, logger) {
@@ -557,42 +589,79 @@ display: none !important;
557
589
  await session.send(retWithoutLastLink);
558
590
  }
559
591
  }
560
-
561
592
  if (config.VideoParsing_ToLink) {
562
- const bilibilimediaDataURL = await handleBilibiliMedia(lastretUrl);
593
+ const fullAPIurl = `https://api.xingzhige.com/API/b_parse/?url=${encodeURIComponent(lastretUrl)}`;
563
594
 
564
- if (options.link) { // 发送链接
565
- await session.send(h.text(bilibilimediaDataURL));
566
- return;
567
- } else if (options.audio) { // 发送语音
568
- await session.send(h.audio(bilibilimediaDataURL));
569
- return;
570
- } else { // 默认发送视频
571
- // 根据配置的值来决定发送的内容
572
- switch (config.VideoParsing_ToLink) {
573
- case '1': // 不返回视频/视频直链
574
- break;
575
- case '2': // 仅返回视频
576
- await session.send(h.video(bilibilimediaDataURL)); // 发送视频
577
- break;
578
- case '3': // 仅返回视频直链
579
- await session.send(h.text(bilibilimediaDataURL)); // 发送视频直链
580
- break;
581
- case '4': // 返回视频和视频直链
582
- await session.send(h.text(bilibilimediaDataURL)); // 先发送视频直链
583
- await session.send(h.video(bilibilimediaDataURL)); // 发送视频
584
- break;
585
- case '5': // 返回视频,记录视频链接
586
- await logger.info(bilibilimediaDataURL); // 先记录日志
587
- await session.send(h.video(bilibilimediaDataURL)); // 发送视频
588
- break;
589
- default:
590
- // 处理默认情况或者错误配置
591
- break;
595
+ try {
596
+ const responseData = await ctx.http.get(fullAPIurl);
597
+
598
+ if (responseData.code === 0 && responseData.msg === "video" && responseData.data) {
599
+ const { bvid, cid, video } = responseData.data;
600
+ const bilibiliUrl = `https://api.bilibili.com/x/player/playurl?fnval=80&cid=${cid}&bvid=${bvid}`;
601
+ const playData = await ctx.http.get(bilibiliUrl);
602
+
603
+ // ctx.logger.info(bilibiliUrl);
604
+
605
+ if (playData.code === 0 && playData.data && playData.data.dash.duration) {
606
+ const videoDurationSeconds = playData.data.dash.duration;
607
+ const videoDurationMinutes = videoDurationSeconds / 60;
608
+
609
+ if (videoDurationMinutes > config.Maximumduration) {
610
+ if (config.Maximumduration_tip !== '不返回文字提示') {
611
+ await session.send(config.Maximumduration_tip);
612
+ return;
613
+ } else {
614
+ return;
615
+ }
616
+ }
617
+
618
+ const videoUrl = video.url;
619
+ //ctx.logger.info(videoUrl);
620
+
621
+ if (videoUrl) {
622
+ if (options.link) {
623
+ await session.send(h.text(videoUrl));
624
+ return;
625
+ } else if (options.audio) {
626
+ await session.send(h.audio(videoUrl));
627
+ return;
628
+ } else {
629
+ switch (config.VideoParsing_ToLink) {
630
+ case '1':
631
+ break;
632
+ case '2':
633
+ await session.send(h.video(videoUrl));
634
+ break;
635
+ case '3':
636
+ await session.send(h.text(videoUrl));
637
+ break;
638
+ case '4':
639
+ await session.send(h.text(videoUrl));
640
+ await session.send(h.video(videoUrl));
641
+ break;
642
+ case '5':
643
+ logger.info(videoUrl);
644
+ await session.send(h.video(videoUrl));
645
+ break;
646
+ default:
647
+ break;
648
+ }
649
+ }
650
+ } else {
651
+ throw new Error("解析视频直链失败");
652
+ }
653
+ } else {
654
+ throw new Error("获取播放数据失败");
655
+ }
656
+ } else {
657
+ throw new Error("解析视频信息失败或非视频类型内容");
592
658
  }
659
+ } catch (error) {
660
+ logger.error("请求解析 API 失败或处理出错:", error);
593
661
  }
594
662
  }
595
663
 
664
+
596
665
  if (config.loggerinfo) {
597
666
  //logger.info(`视频信息内容:\n ${JSON.stringify(mediaData)}`);
598
667
  logger.info(`机器人发送完整消息为:\n ${ret}`);
@@ -873,41 +942,5 @@ display: none !important;
873
942
  return null;
874
943
  }
875
944
  }
876
- /////////////////////////////////////////////////////////////////////////////////////////////////////////
877
- /////////////////////////////////////////////////////////////////////////////////////////////////////////
878
- /////////////////////////////////////////////////////////////////////////////////////////////////////////
879
- /**
880
- * 检查看看一个url是否返回403,或者无法访问,主要用在通过bilibili官方api拿到的视频流
881
- * @param url 链接
882
- * @returns boolean
883
- */
884
- async function checkResponseStatus(url) {
885
- try {
886
- const response = await ctx.http(url, {
887
- method: 'GET',
888
- headers: {
889
- 'Referer': 'no-referrer',
890
- 'Range': 'bytes=0-10000'
891
- }
892
- });
893
- //尝试打印一下看看response
894
- //await logger.info(response);
895
-
896
- if (response.status === 403 || response.status === 410) {
897
- return false;
898
- }
899
- else if (response.status === 200 || response.status === 206) {
900
- return true;
901
- }
902
- else {
903
- return false;
904
- }
905
- }
906
- catch (error) {
907
- return false;
908
- }
909
- }
910
-
911
-
912
945
  }
913
946
  exports.apply = apply;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "koishi-plugin-bilibili-videolink-analysis",
3
3
  "description": "[<ruby>Bilibili视频解析<rp>(</rp><rt>点我查看食用方法</rt><rp>)</rp></ruby>](https://www.npmjs.com/package/koishi-plugin-bilibili-videolink-analysis)解析B站链接(支持小程序卡片)支持搜索点播功能!灵感来自完美的 [bili-parser](/market?keyword=bili-parser) !",
4
4
  "license": "MIT",
5
- "version": "1.0.2",
5
+ "version": "1.1.1",
6
6
  "main": "lib/index.js",
7
7
  "typings": "lib/index.d.ts",
8
8
  "files": [