koishi-plugin-bilibili-videolink-analysis 1.1.21 → 1.1.22

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 +70 -12
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -118,6 +118,7 @@ exports.Config = Schema.intersect([
118
118
 
119
119
  Schema.object({
120
120
  isfigure: Schema.boolean().default(false).description("是否开启合并转发 `仅支持 onebot 适配器` 其他平台开启 无效").experimental(),
121
+ filebuffer: Schema.boolean().default(true).description("是否将视频链接下载后再发送 (以解决部分onebot协议端的问题)<br>否则使用视频直链发送").experimental(),
121
122
  middleware: Schema.boolean().default(false).description("前置中间件模式"),
122
123
  userAgent: Schema.string().description("所有 API 请求所用的 User-Agent").default("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"),
123
124
  }).description("调试设置"),
@@ -545,31 +546,59 @@ display: none !important;
545
546
  }
546
547
  } else {
547
548
  // 视频时长在允许范围内,处理视频
548
- const videoUrl = video.url;
549
- logInfo(videoUrl);
549
+ let videoData = video.url; // 使用新变量名,避免覆盖原始URL
550
+ logInfo(videoData);
550
551
 
551
- if (videoUrl) {
552
+ if (config.filebuffer) {
553
+ try {
554
+ const videoFileBuffer = await ctx.http.file(video.url);
555
+ logInfo(videoFileBuffer);
556
+
557
+ // 检查文件类型
558
+ if (videoFileBuffer && videoFileBuffer.data) {
559
+ // 将ArrayBuffer转换为Buffer
560
+ const buffer = Buffer.from(videoFileBuffer.data);
561
+
562
+ // 获取MIME类型
563
+ const mimeType = videoFileBuffer.type || videoFileBuffer.mime || 'video/mp4';
564
+
565
+ // 创建data URI
566
+ const base64Data = buffer.toString('base64');
567
+ videoData = `data:${mimeType};base64,${base64Data}`;
568
+
569
+ logInfo("成功使用 ctx.http.file 将视频URL 转换为data URI格式");
570
+ } else {
571
+ logInfo("文件数据无效,使用原始URL");
572
+ }
573
+ } catch (error) {
574
+ logger.error("获取视频文件失败:", error);
575
+ // 出错时继续使用原始URL
576
+ }
577
+ }
578
+
579
+ if (videoData) {
552
580
  if (options.link) {
553
- videoElements.push(h.text(videoUrl));
581
+ // 如果是链接选项,仍然使用原始URL
582
+ videoElements.push(h.text(video.url));
554
583
  } else if (options.audio) {
555
- videoElements.push(h.audio(videoUrl));
584
+ videoElements.push(h.audio(videoData));
556
585
  } else {
557
586
  switch (config.VideoParsing_ToLink) {
558
587
  case '1':
559
588
  break;
560
589
  case '2':
561
- videoElements.push(h.video(videoUrl));
590
+ videoElements.push(h.video(videoData));
562
591
  break;
563
592
  case '3':
564
- videoElements.push(h.text(videoUrl));
593
+ videoElements.push(h.text(video.url));
565
594
  break;
566
595
  case '4':
567
- videoElements.push(h.text(videoUrl));
568
- videoElements.push(h.video(videoUrl));
596
+ videoElements.push(h.text(video.url));
597
+ videoElements.push(h.video(videoData));
569
598
  break;
570
599
  case '5':
571
- logger.info(videoUrl);
572
- videoElements.push(h.video(videoUrl));
600
+ logger.info(video.url);
601
+ videoElements.push(h.video(videoData));
573
602
  break;
574
603
  default:
575
604
  break;
@@ -578,6 +607,7 @@ display: none !important;
578
607
  } else {
579
608
  throw new Error("解析视频直链失败");
580
609
  }
610
+
581
611
  }
582
612
  } else {
583
613
  throw new Error("获取播放数据失败");
@@ -615,7 +645,35 @@ display: none !important;
615
645
  const figureContent = h('figure', {
616
646
  children: allElements
617
647
  });
618
- logInfo(JSON.stringify(figureContent, null, 2));
648
+
649
+ // 创建一个用于日志的深拷贝对象,避免修改原始对象
650
+ const logObject = JSON.parse(JSON.stringify(figureContent));
651
+
652
+ // 递归处理对象,截断长字符串
653
+ function truncateLongStrings(obj, maxLength = 150) {
654
+ if (!obj) return obj;
655
+
656
+ if (typeof obj === 'string' && obj.length > maxLength) {
657
+ return obj.substring(0, maxLength) + '... [截断剩余' + (obj.length - maxLength) + '字符]';
658
+ }
659
+
660
+ if (Array.isArray(obj)) {
661
+ return obj.map(item => truncateLongStrings(item, maxLength));
662
+ }
663
+
664
+ if (typeof obj === 'object') {
665
+ const newObj = {};
666
+ for (const key in obj) {
667
+ newObj[key] = truncateLongStrings(obj[key], maxLength);
668
+ }
669
+ return newObj;
670
+ }
671
+
672
+ return obj;
673
+ }
674
+
675
+ // 截断长字符串后再打印
676
+ logInfo(JSON.stringify(truncateLongStrings(logObject), null, 2));
619
677
 
620
678
  // 发送合并转发消息
621
679
  await session.send(figureContent);
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.1.21",
5
+ "version": "1.1.22",
6
6
  "main": "lib/index.js",
7
7
  "typings": "lib/index.d.ts",
8
8
  "files": [