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

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 +46 -50
  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
 
@@ -459,18 +459,49 @@ display: none !important;
459
459
  }
460
460
  })
461
461
 
462
- async function handleBilibiliMedia(lastretUrl, config) {
462
+ async function handleBilibiliMedia(config, session, lastretUrl) {
463
463
  const fullAPIurl = `https://api.xingzhige.com/API/b_parse/?url=${encodeURIComponent(lastretUrl)}`;
464
464
 
465
465
  try {
466
466
  // 发起请求,解析 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;
467
+ const responseData = await ctx.http.get(fullAPIurl);
468
+
469
+ // 检查返回状态码是否为0且为视频内容
470
+ if (responseData.code === 0 && responseData.msg === "video" && responseData.data) {
471
+ const { bvid, cid } = responseData.data;
472
+
473
+ // 请求 Bilibili 播放 URL,获取视频信息
474
+ const bilibiliUrl = `https://api.bilibili.com/x/player/playurl?fnval=80&cid=${cid}&bvid=${bvid}`;
475
+ const playData = await ctx.http.get(bilibiliUrl);
476
+ //////
477
+ ctx.logger.info(bilibiliUrl)
478
+ // 检查返回的状态码是否为0,表示请求成功
479
+ if (playData.code === 0 && playData.data && playData.data.dash.duration) {
480
+ const videoDurationSeconds = playData.data.dash.duration; // 视频时长,单位为秒
481
+ const videoDurationMinutes = videoDurationSeconds / 60; // 转换为分钟
482
+
483
+ // 检查视频时长是否超过配置的最大允许时长
484
+ if (videoDurationMinutes > config.Maximumduration) {
485
+ // 视频时长超过最大限制,返回提示
486
+ if (config.Maximumduration_tip !== '不返回文字提示') {
487
+ await session.send(config.Maximumduration_tip)
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
  }
@@ -480,6 +511,7 @@ display: none !important;
480
511
  }
481
512
  }
482
513
 
514
+
483
515
  //判断是否需要解析
484
516
  async function isProcessLinks(session, config, ctx, lastProcessedUrls, logger) {
485
517
  // 解析内容中的链接
@@ -559,7 +591,7 @@ display: none !important;
559
591
  }
560
592
 
561
593
  if (config.VideoParsing_ToLink) {
562
- const bilibilimediaDataURL = await handleBilibiliMedia(lastretUrl);
594
+ const bilibilimediaDataURL = await handleBilibiliMedia(config, session, lastretUrl);
563
595
 
564
596
  if (options.link) { // 发送链接
565
597
  await session.send(h.text(bilibilimediaDataURL));
@@ -873,41 +905,5 @@ display: none !important;
873
905
  return null;
874
906
  }
875
907
  }
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
908
  }
913
909
  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.0",
6
6
  "main": "lib/index.js",
7
7
  "typings": "lib/index.d.ts",
8
8
  "files": [