koishi-plugin-video-parser-all 0.7.8 → 0.8.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 (3) hide show
  1. package/lib/index.js +84 -29
  2. package/package.json +1 -1
  3. package/readme.md +2 -0
package/lib/index.js CHANGED
@@ -42,7 +42,7 @@ exports.Config = koishi_1.Schema.intersect([
42
42
  }).description('内容长度限制'),
43
43
  koishi_1.Schema.object({
44
44
  timeout: koishi_1.Schema.number().min(0).default(180000).description('API请求超时时间(毫秒)'),
45
- videoSendTimeout: koishi_1.Schema.number().min(0).default(0).description('视频发送超时时间(毫秒,0为不限制)'),
45
+ videoSendTimeout: koishi_1.Schema.number().min(0).default(60000).description('视频发送超时时间(毫秒,0为不限制)'),
46
46
  userAgent: koishi_1.Schema.string().default('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36').description('请求UA标识'),
47
47
  }).description('网络与API设置'),
48
48
  koishi_1.Schema.object({
@@ -637,7 +637,6 @@ function apply(ctx, config) {
637
637
  parseResult.author === '未知作者' &&
638
638
  parseResult.desc === '暂无简介';
639
639
  if (isAllDefault) {
640
- // 【关键修改1】控制台日志改为更精准的提示
641
640
  logger.warn(`解析结果均为默认值(可能暂不支持该链接): ${url}`);
642
641
  return {
643
642
  data: null,
@@ -702,26 +701,32 @@ function apply(ctx, config) {
702
701
  msg: '处理成功'
703
702
  };
704
703
  }
705
- async function sendTimeout(session, content) {
704
+ async function sendWithTimeout(session, content) {
706
705
  if (config.videoSendTimeout <= 0) {
707
- return session.send(content).catch((err) => {
706
+ try {
707
+ return await session.send(content);
708
+ }
709
+ catch (err) {
708
710
  const errorMsg = getErrorMessage(err);
709
711
  logger.error(`发送消息失败: ${errorMsg}`);
710
712
  if (!config.ignoreSendError)
711
- return null;
713
+ throw err;
712
714
  return null;
715
+ }
716
+ }
717
+ try {
718
+ const timeoutPromise = new Promise((_, reject) => {
719
+ setTimeout(() => reject(new Error('发送超时')), config.videoSendTimeout);
713
720
  });
721
+ return await Promise.race([session.send(content), timeoutPromise]);
714
722
  }
715
- return Promise.race([
716
- session.send(content),
717
- new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), config.videoSendTimeout))
718
- ]).catch((err) => {
723
+ catch (err) {
719
724
  const errorMsg = getErrorMessage(err);
720
- logger.error(`发送消息超时: ${errorMsg}`);
725
+ logger.error(`发送消息超时或失败: ${errorMsg}`);
721
726
  if (!config.ignoreSendError)
722
- return null;
727
+ throw err;
723
728
  return null;
724
- });
729
+ }
725
730
  }
726
731
  async function flush(session, manualUrls) {
727
732
  const key = `${session.platform}:${session.userId}:${session.channelId}`;
@@ -735,7 +740,7 @@ function apply(ctx, config) {
735
740
  const errors = [];
736
741
  for (const url of urls) {
737
742
  const result = await processSingleUrl(session, url);
738
- if (result.success) {
743
+ if (result.success && result.data) {
739
744
  items.push(result.data);
740
745
  }
741
746
  else {
@@ -745,10 +750,14 @@ function apply(ctx, config) {
745
750
  if (errors.length > 0) {
746
751
  const errorLines = errors.map(err => `【${err.url.slice(0, 50)}${err.url.length > 50 ? '...' : ''}】: ${err.msg}`);
747
752
  const errorMsg = `❌ 解析失败:\n${errorLines.join('\n')}`;
748
- await sendTimeout(session, errorMsg);
753
+ try {
754
+ await sendWithTimeout(session, errorMsg);
755
+ }
756
+ catch (e) {
757
+ logger.error(`发送错误消息失败: ${getErrorMessage(e)}`);
758
+ }
749
759
  await delay(500);
750
760
  }
751
- // 已删除⚠ 未解析到有效内容提示
752
761
  if (items.length === 0) {
753
762
  return;
754
763
  }
@@ -768,7 +777,7 @@ function apply(ctx, config) {
768
777
  if (config.downloadVideoBeforeSend) {
769
778
  const filename = crypto_1.default.createHash('md5').update(item.video).digest('hex');
770
779
  const dl = await downloadVideo(item.video, filename, config.userAgent, config.maxVideoSize, config.downloadThreads);
771
- if (dl.success) {
780
+ if (dl.success && dl.filePath) {
772
781
  forwardMessages.push(buildForwardNode(session, koishi_1.h.file(dl.filePath), botName));
773
782
  }
774
783
  else {
@@ -780,6 +789,7 @@ function apply(ctx, config) {
780
789
  }
781
790
  }
782
791
  catch (e) {
792
+ logger.error(`处理视频发送失败: ${getErrorMessage(e)}`);
783
793
  forwardMessages.push(buildForwardNode(session, koishi_1.h.video(item.video), botName));
784
794
  }
785
795
  }
@@ -792,27 +802,60 @@ function apply(ctx, config) {
792
802
  }
793
803
  else {
794
804
  if (item.text) {
795
- await sendTimeout(session, item.text);
805
+ await sendWithTimeout(session, item.text);
796
806
  await delay(300);
797
807
  }
798
808
  if (item.cover && item.type !== '图集') {
799
- await sendTimeout(session, koishi_1.h.image(item.cover));
809
+ try {
810
+ await sendWithTimeout(session, koishi_1.h.image(item.cover));
811
+ }
812
+ catch (e) {
813
+ logger.error(`发送封面失败: ${getErrorMessage(e)}`);
814
+ }
800
815
  await delay(300);
801
816
  }
802
817
  if (item.video && config.showVideoFile) {
803
818
  try {
804
- await sendTimeout(session, koishi_1.h.video(item.video));
819
+ if (config.downloadVideoBeforeSend) {
820
+ const filename = crypto_1.default.createHash('md5').update(item.video).digest('hex');
821
+ const dl = await downloadVideo(item.video, filename, config.userAgent, config.maxVideoSize, config.downloadThreads);
822
+ if (dl.success && dl.filePath) {
823
+ await sendWithTimeout(session, koishi_1.h.file(dl.filePath));
824
+ }
825
+ else {
826
+ await sendWithTimeout(session, koishi_1.h.video(item.video));
827
+ }
828
+ }
829
+ else {
830
+ await sendWithTimeout(session, koishi_1.h.video(item.video));
831
+ }
805
832
  }
806
833
  catch (e) {
807
- await sendTimeout(session, koishi_1.h.video(item.video));
834
+ logger.error(`发送视频失败: ${getErrorMessage(e)}`);
835
+ try {
836
+ await sendWithTimeout(session, koishi_1.h.video(item.video));
837
+ }
838
+ catch (e2) {
839
+ logger.error(`发送视频链接也失败: ${getErrorMessage(e2)}`);
840
+ }
808
841
  }
809
842
  await delay(500);
810
843
  }
811
844
  if ((item.type === '图集' || item.type === 'image') && item.images?.length) {
812
- await sendTimeout(session, `📸 图集内容(共${item.totalImageCount}张)`);
845
+ try {
846
+ await sendWithTimeout(session, `📸 图集内容(共${item.totalImageCount}张)`);
847
+ }
848
+ catch (e) {
849
+ logger.error(`发送图集提示失败: ${getErrorMessage(e)}`);
850
+ }
813
851
  await delay(300);
814
852
  for (const img of item.images) {
815
- await sendTimeout(session, koishi_1.h.image(img));
853
+ try {
854
+ await sendWithTimeout(session, koishi_1.h.image(img));
855
+ }
856
+ catch (e) {
857
+ logger.error(`发送图集图片失败: ${getErrorMessage(e)}`);
858
+ }
816
859
  await delay(200);
817
860
  }
818
861
  }
@@ -824,12 +867,18 @@ function apply(ctx, config) {
824
867
  }
825
868
  if (enableForward && forwardMessages.length) {
826
869
  try {
827
- await sendTimeout(session, (0, koishi_1.h)('message', { forward: true }, forwardMessages.slice(0, 100)));
870
+ await sendWithTimeout(session, (0, koishi_1.h)('message', { forward: true }, forwardMessages.slice(0, 100)));
828
871
  }
829
872
  catch (e) {
873
+ logger.error(`合并转发失败,降级为逐条发送: ${getErrorMessage(e)}`);
830
874
  for (const node of forwardMessages) {
831
- await sendTimeout(session, node.data.content);
832
- await delay(300);
875
+ try {
876
+ await sendWithTimeout(session, node.data.content);
877
+ await delay(300);
878
+ }
879
+ catch (e2) {
880
+ logger.error(`降级发送失败: ${getErrorMessage(e2)}`);
881
+ }
833
882
  }
834
883
  }
835
884
  }
@@ -841,21 +890,27 @@ function apply(ctx, config) {
841
890
  const urls = extractUrl(content);
842
891
  if (!urls.length)
843
892
  return;
844
- if (config.showWaitingTip)
845
- await sendTimeout(session, config.waitingTipText);
893
+ if (config.showWaitingTip) {
894
+ try {
895
+ await sendWithTimeout(session, config.waitingTipText);
896
+ }
897
+ catch (e) {
898
+ logger.error(`发送等待提示失败: ${getErrorMessage(e)}`);
899
+ }
900
+ }
846
901
  await flush(session, urls);
847
902
  });
848
903
  ctx.command('parse <url>', '手动解析视频').action(async ({ session }, url) => {
849
904
  const us = extractUrl(url);
850
905
  if (!us.length) {
851
- await sendTimeout(session, '无效的视频链接');
906
+ await sendWithTimeout(session, '无效的视频链接');
852
907
  return;
853
908
  }
854
909
  await flush(session, us);
855
910
  });
856
911
  ctx.command('clear-cache', '清空缓存').action(async ({ session }) => {
857
912
  clearAllCache();
858
- await sendTimeout(session, '✅ 缓存已清空');
913
+ await sendWithTimeout(session, '✅ 缓存已清空');
859
914
  });
860
915
  setInterval(() => {
861
916
  const now = Date.now();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-video-parser-all",
3
3
  "description": "Koishi 全平台视频解析插件,支持抖音/快手/B站/微博/今日头条/皮皮搞笑/皮皮虾/最右视频链接解析",
4
- "version": "0.7.8",
4
+ "version": "0.8.0",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
package/readme.md CHANGED
@@ -131,6 +131,8 @@ This is a **multi-platform video/image parsing plugin** developed for the Koishi
131
131
  |----------------------|-------------------------|
132
132
  | Minecraft-1314 | 插件完整开发 (Complete plugin development) |
133
133
  | JH-Ahua | BugPk-Api 支持 |
134
+ | 星之阁API | 星之阁API 支持 |
135
+ | shangxue | 灵感来源 |
134
136
  | (欢迎提交 PR 加入贡献者列表) | (Welcome to submit PR to join the contributor list) |
135
137
 
136
138
  ## 许可协议 (License)