koishi-plugin-bilibili-videolink-analysis 1.1.25 → 1.2.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.
package/lib/index.js CHANGED
@@ -1,966 +1,717 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.apply = exports.usage = exports.Config = exports.name = exports.inject = void 0;
4
- const { Schema, Logger, h } = require("koishi");
5
- const logger = new Logger('bilibili-videolink-analysis');
6
- exports.name = 'bilibili-videolink-analysis';
7
- exports.inject = {
8
- optional: ['puppeteer'],
9
- //required: ['BiliBiliVideo']
10
- }
11
- exports.usage = `
12
-
13
- <h1>→ <a href="https://www.npmjs.com/package/koishi-plugin-bilibili-videolink-analysis" target="_blank">可以点击这里查看详细的文档说明✨</a></h1>
14
-
15
- ✨ 只需开启插件,就可以解析B站视频的链接啦~ ✨
16
-
17
- 向bot发送B站视频链接吧~
18
-
19
- 会返回视频信息与视频哦
20
-
21
- ---
22
-
23
- #### ⚠️ **如果你使用不了本项目,请优先检查:** ⚠️
24
- #### 若无注册的指令,请关开一下[command插件](/market?keyword=commands+email:shigma10826@gmail.com)(没有指令也不影响解析别人的链接)
25
- #### 视频内容是否为B站的大会员专属视频/付费视频/充电专属视频
26
- #### 接入方法是否支持获取网址链接/小程序卡片消息
27
- #### 接入方法是否支持视频元素的发送
28
- #### 发送视频超时/其他网络问题
29
- #### 视频内容被平台屏蔽/其他平台因素
30
-
31
- ---
32
-
33
- ### 注意,点播功能需要使用 puppeteer 服务
34
-
35
- 点播功能是为了方便群友一起刷B站哦~
36
-
37
- 比如:搜索 “遠い空へ” 的第二页,并且结果以语音格式返回
38
-
39
- 示例:\`点播 遠い空へ -a -p 2\`
40
-
41
-
42
- ---
43
-
44
- ### 特别鸣谢 💖
45
-
46
- 特别鸣谢以下项目的支持:
47
-
48
- - [@summonhim/koishi-plugin-bili-parser](/market?keyword=bili-parser)
49
-
50
- ---
51
-
52
- `;
53
-
54
- exports.Config = Schema.intersect([
55
- Schema.object({
56
- demand: Schema.boolean().default(true).description("开启点播指令功能<br>`其实点播登录不登录 都搜不准,登录只是写着玩的`"),
57
- }).description('点播设置(需要puppeteer服务)'),
58
- Schema.union([
59
- Schema.object({
60
- demand: Schema.const(false).required(),
61
- }),
62
- Schema.object({
63
- demand: Schema.const(true),
64
- timeout: Schema.number().role('slider').min(1).max(300).step(1).default(60).description('指定播放视频的输入时限。`单位 秒`'),
65
- point: Schema.tuple([Number, Number]).description('序号标注位置。分别表示`距离顶部 距离左侧`的百分比').default([50, 50]),
66
- enable: Schema.boolean().description('是否开启自动解析`选择对应视频 会自动解析视频内容`').default(true),
67
- }),
68
- ]),
69
-
70
- Schema.object({
71
- enablebilianalysis: Schema.boolean().default(true).description("开启解析功能<br>`关闭后,解析功能将关闭`"),
72
- }).description('视频解析 - 功能开关'),
73
- Schema.union([
74
- Schema.object({
75
- enablebilianalysis: Schema.const(false).required(),
76
- }),
77
- Schema.intersect([
78
- Schema.object({
79
- enablebilianalysis: Schema.const(true),
80
- waitTip_Switch: Schema.union([
81
- Schema.const().description('不返回文字提示'),
82
- Schema.string().description('返回文字提示(请在右侧填写文字内容)'),
83
- ]).description("是否返回等待提示。开启后,会发送`等待提示语`"),
84
- linktextParsing: Schema.boolean().default(true).description("是否返回 视频图文数据 `开启后,才发送视频数据的图文解析。`"),
85
- VideoParsing_ToLink: Schema.union([
86
- Schema.const('1').description('不返回视频/视频直链'),
87
- Schema.const('2').description('仅返回视频'),
88
- Schema.const('3').description('仅返回视频直链'),
89
- Schema.const('4').description('返回视频和视频直链'),
90
- Schema.const('5').description('返回视频,仅在日志记录视频直链'),
91
- ]).role('radio').default('2').description("是否返回` 视频/视频直链 `"),
92
- BVnumberParsing: Schema.boolean().default(true).description("是否允许根据`独立的BV、AV号`解析视频 `开启后,可以通过视频的BV、AV号解析视频。` <br> [触发说明见README](https://www.npmjs.com/package/koishi-plugin-bilibili-videolink-analysis)"),
93
- MinimumTimeInterval: Schema.number().default(180).description("若干`秒`内 不再处理相同链接 `防止多bot互相触发 导致的刷屏/性能浪费`").min(1),
94
- }),
95
-
96
- Schema.object({
97
- enablebilianalysis: Schema.const(true),
98
- Minimumduration: Schema.number().default(0).description("允许解析的视频最小时长(分钟)`低于这个时长 就不会发视频内容`").min(0),
99
- Minimumduration_tip: Schema.union([
100
- Schema.const('return').description('不返回文字提示'),
101
- Schema.object({
102
- tipcontent: Schema.string().default('视频太短啦!不看不看~').description("文字提示内容"),
103
- tipanalysis: Schema.boolean().default(true).description("是否进行图文解析(不会返回视频链接)"),
104
- }).description('返回文字提示'),
105
- ]).description("对`过短视频`的文字提示内容").default({}),
106
- Maximumduration: Schema.number().default(25).description("允许解析的视频最大时长(分钟)`超过这个时长 就不会发视频内容`").min(1),
107
- Maximumduration_tip: Schema.union([
108
- Schema.const('return').description('不返回文字提示'),
109
- Schema.object({
110
- tipcontent: Schema.string().default('视频太长啦!内容还是去B站看吧~').description("文字提示内容"),
111
- tipanalysis: Schema.boolean().default(true).description("是否进行图文解析(不会返回视频链接)"),
112
- }).description('返回文字提示'),
113
- ]).description("对`过长视频`的文字提示内容").default({}),
114
- }).description("视频解析 - 内容限制"),
115
-
116
- Schema.object({
117
- parseLimit: Schema.number().default(3).description("单对话多链接解析上限").hidden(),
118
- useNumeral: Schema.boolean().default(true).description("使用格式化数字").hidden(),
119
- showError: Schema.boolean().default(false).description("当链接不正确时提醒发送者").hidden(),
120
- bVideoIDPreference: Schema.union([
121
- Schema.const("bv").description("BV 号"),
122
- Schema.const("av").description("AV "),
123
- ]).default("bv").description("ID 偏好").hidden(),
124
-
125
- bVideo_area: Schema.string().role('textarea', { rows: [8, 16] }).description("图文解析的返回格式<br>注意变量格式,以及变量名称。<br>比如 `${标题}` 不可以变成`${标题123}`,你可以直接删掉但是不能修改变量名称哦<br>当然变量也不能无中生有,下面的默认值内容 就是所有变量了,你仅可以删去变量 或者修改变量之外的格式。<br>· 特殊变量`${~~~}`表示分割线,会把上下内容分为两个信息单独发送。")
126
- .default("${标题} --- ${UP主}\n${简介}\n点赞:${点赞} --- 投币:${投币}\n收藏:${收藏} --- 转发:${转发}\n观看:${观看} --- 弹幕:${弹幕}\n${~~~}\n${封面}"),
127
- bVideoShowLink: Schema.boolean().default(false).description("在末尾显示视频的链接地址 `开启可能会导致其他bot循环解析`"),
128
- bVideoShowIntroductionTofixed: Schema.number().default(50).description("视频的`简介`最大的字符长度<br>超出部分会使用 `...` 代替"),
129
- }).description("链接的图文解析设置"),
130
-
131
- Schema.object({
132
- isfigure: Schema.boolean().default(false).description("是否开启合并转发 `仅支持 onebot 适配器` 其他平台开启 无效").experimental(),
133
- filebuffer: Schema.boolean().default(true).description("是否将视频链接下载后再发送 (以解决部分onebot协议端的问题)<br>否则使用视频直链发送").experimental(),
134
- middleware: Schema.boolean().default(false).description("前置中间件模式"),
135
- 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"),
136
- }).description("调试设置"),
137
- ]),
138
- ]),
139
-
140
-
141
- Schema.object({
142
- pageclose: Schema.boolean().default(true).description("自动`page.close()`<br>非开发者请勿改动").experimental(),
143
- loggerinfo: Schema.boolean().default(false).description("日志调试输出 `日常使用无需开启`<br>非开发者请勿改动").experimental(),
144
- loggerinfofulljson: Schema.boolean().default(false).description("打印完整的机器人发送的json输出").experimental(),
145
- }).description("开发者选项"),
146
- ]);
147
-
148
- function apply(ctx, config) {
149
-
150
- function logInfo(message, message2) {
151
- if (config.loggerinfo) {
152
- if (message2) {
153
- ctx.logger.info(message, message2)
154
- } else {
155
- ctx.logger.info(message);
156
- }
157
- }
158
- }
159
-
160
- if (config.enablebilianalysis) {
161
- ctx.middleware(async (session, next) => {
162
- let sessioncontent = session.content;
163
- // 如果允许解析 BV 号,则进行解析
164
- if (config.BVnumberParsing) {
165
- const bvUrls = convertBVToUrl(sessioncontent);
166
- if (bvUrls.length > 0) {
167
- sessioncontent += '\n' + bvUrls.join('\n');
168
- }
169
- }
170
- const links = await isProcessLinks(sessioncontent); // 判断是否需要解析
171
- if (links) {
172
- const ret = await extractLinks(session, config, ctx, links); // 提取链接
173
- if (ret && !isLinkProcessedRecently(ret, lastProcessedUrls, config, session.channelId)) {
174
- await processVideoFromLink(session, config, ctx, lastProcessedUrls, logger, ret); // 解析视频并返回
175
- }
176
- }
177
- return next();
178
- }, config.middleware);
179
- }
180
-
181
- if (config.demand) {
182
- ctx.command('B站点播/退出登录', '退出B站账号')
183
- .action(async ({ session }) => {
184
- const page = await ctx.puppeteer.page();
185
- await page.goto('https://www.bilibili.com/', { waitUntil: 'networkidle2' });
186
-
187
- const loginButtonSelector = '.right-entry__outside.go-login-btn';
188
- const isLoggedIn = await page.$(loginButtonSelector) === null;
189
-
190
- if (!isLoggedIn) {
191
- await page.close();
192
- await session.send(h.text('您尚未登录。'))
193
- return;
194
- }
195
-
196
- const avatarLinkSelector = '.header-entry-mini';
197
- const logoutButtonSelector = '.logout-item';
198
-
199
- try {
200
- const avatarElement = await page.$(avatarLinkSelector);
201
- if (avatarElement) {
202
- await avatarElement.hover();
203
- await page.waitForSelector(logoutButtonSelector, { visible: true });
204
-
205
- await page.click(logoutButtonSelector);
206
-
207
- await new Promise(resolve => setTimeout(resolve, 1000));
208
-
209
- await page.close();
210
- await session.send(h.text('已成功退出登录。'))
211
- return;
212
- } else {
213
- await page.close();
214
- await session.send(h.text('找不到用户头像,无法退出登录。'))
215
- return;
216
- }
217
- } catch (error) {
218
- await page.close();
219
- logger.error('Error during logout:', error);
220
- await session.send(h.text('退出登录时出错。'))
221
- return;
222
- }
223
- });
224
-
225
- ctx.command('B站点播/登录', '登录B站账号')
226
- .alias("登陆")
227
- .action(async ({ session }) => {
228
- const page = await ctx.puppeteer.page();
229
- await page.goto('https://www.bilibili.com/', { waitUntil: 'networkidle2' });
230
-
231
- const loginButtonSelector = '.right-entry__outside.go-login-btn';
232
- const isLoggedIn = await page.$(loginButtonSelector) === null;
233
-
234
- if (isLoggedIn) {
235
- await page.close();
236
- await session.send(h.text('您已经登录了。'))
237
- return;
238
- }
239
-
240
- await page.click(loginButtonSelector);
241
-
242
- const qrCodeSelector = '.login-scan-box img';
243
- await page.waitForSelector(qrCodeSelector);
244
- const qrCodeUrl = await page.$eval(qrCodeSelector, img => img.src);
245
-
246
- await session.send(h.image(qrCodeUrl, 'image/png'));
247
- await session.send('请扫描二维码进行登录。');
248
-
249
- let attempts = 0;
250
- let loginSuccessful = false;
251
-
252
- while (attempts < 6) {
253
- await new Promise(resolve => setTimeout(resolve, 5000)); // Wait
254
- const isStillLoggedIn = await page.$(loginButtonSelector) === null;
255
-
256
- if (isStillLoggedIn) {
257
- loginSuccessful = true;
258
- break;
259
- }
260
-
261
- attempts++;
262
- }
263
-
264
- await page.close();
265
- await session.send(h.text(loginSuccessful ? '登录成功!' : '登录失败,请重试。'))
266
- return;
267
- });
268
-
269
- ctx.command('B站点播 [keyword]', '点播B站视频')
270
- .option('video', '-v 解析返回视频')
271
- .option('audio', '-a 解析返回语音')
272
- .option('link', '-l 解析返回链接')
273
- .option('page', '-p <page:number> 指定页数', { fallback: '1' })
274
- .example('点播 遠い空へ -v')
275
- .action(async ({ options, session }, keyword) => {
276
- if (!keyword) {
277
- await session.execute('点播 -h')
278
- await session.send(h.text('没输入点播内容'))
279
- return
280
- }
281
-
282
-
283
- const url = `https://search.bilibili.com/video?keyword=${encodeURIComponent(keyword)}&page=${options.page}&o=30`
284
- const page = await ctx.puppeteer.page()
285
-
286
- await page.goto(url, {
287
- waitUntil: 'networkidle2'
288
- })
289
-
290
- await page.addStyleTag({
291
- content: `
292
- div.bili-header,
293
- div.login-tip,
294
- div.v-popover,
295
- div.right-entry__outside {
296
- display: none !important;
297
- }
298
- `
299
- })
300
- // 获取视频列表并为每个视频元素添加序号
301
- const videos = await page.evaluate((point) => {
302
- const items = Array.from(document.querySelectorAll('.video-list-item:not([style*="display: none"])'))
303
- return items.map((item, index) => {
304
- const link = item.querySelector('a')
305
- const href = link?.getAttribute('href') || ''
306
- const idMatch = href.match(/\/video\/(BV\w+)\//)
307
- const id = idMatch ? idMatch[1] : ''
308
-
309
- if (!id) {
310
- // 如果没有提取到视频ID,隐藏这个元素
311
- //const htmlElement = item as HTMLElement
312
- const htmlElement = item
313
- htmlElement.style.display = 'none'
314
- } else {
315
- // 创建一个包含序号的元素,并将其插入到视频元素的正中央
316
- const overlay = document.createElement('div')
317
- overlay.style.position = 'absolute'
318
- overlay.style.top = `${point[0]}%`
319
- overlay.style.left = `${point[1]}%`
320
- overlay.style.transform = 'translate(-50%, -50%)'
321
- overlay.style.fontSize = '48px'
322
- overlay.style.fontWeight = 'bold'
323
- overlay.style.color = 'black'
324
- overlay.style.zIndex = '10'
325
- overlay.style.backgroundColor = 'rgba(255, 255, 255, 0.7)' // 半透明白色背景,确保数字清晰可见
326
- overlay.style.padding = '10px'
327
- overlay.style.borderRadius = '8px'
328
- overlay.textContent = `${index + 1}` // 序号
329
-
330
- // 确保父元素有 `position: relative` 以正确定位
331
- //const videoElement = item as HTMLElement
332
- const videoElement = item
333
- videoElement.style.position = 'relative'
334
- videoElement.appendChild(overlay)
335
- }
336
-
337
- return { id }
338
- }).filter(video => video.id)
339
- }, config.point) // 传递配置的 point 参数
340
-
341
- // 如果开启了日志调试模式,打印获取到的视频信息
342
-
343
- logInfo(options)
344
- logInfo(`共找到 ${videos.length} 个视频:`)
345
- videos.forEach((video, index) => {
346
- logInfo(`序号 ${index + 1}: ID - ${video.id}`)
347
- })
348
-
349
-
350
- if (videos.length === 0) {
351
- await page.close()
352
- await session.send(h.text('未找到相关视频。'))
353
- return
354
- }
355
-
356
- // 动态调整窗口大小以适应视频数量
357
- const viewportHeight = 200 + videos.length * 100
358
- await page.setViewport({
359
- width: 1440,
360
- height: viewportHeight
361
- })
362
- logInfo("窗口:宽度:")
363
- logInfo(1440)
364
-
365
- logInfo("窗口:高度:")
366
- logInfo(viewportHeight)
367
- let msg;
368
-
369
- // 截图
370
- const videoListElement = await page.$('.video-list.row')
371
- if (videoListElement) {
372
- const imgBuf = await videoListElement.screenshot({
373
- captureBeyondViewport: false
374
- })
375
- msg = h.image(imgBuf, 'image/png')
376
- }
377
- if (page && config.pageclose) {
378
- await page.close()
379
- }
380
-
381
- // 发送截图
382
- await session.send(msg)
383
-
384
- // 提示用户输入
385
- await session.send(`请选择视频的序号:`)
386
-
387
- // 等待用户输入
388
- const userChoice = await session.prompt(config.timeout * 1000)
389
- const choiceIndex = parseInt(userChoice) - 1
390
- if (isNaN(choiceIndex) || choiceIndex < 0 || choiceIndex >= videos.length) {
391
- await session.send(h.text('输入无效,请输入正确的序号。'))
392
- return
393
- }
394
-
395
- // 返回用户选择的视频ID
396
- const chosenVideo = videos[choiceIndex]
397
-
398
- // 如果开启了日志调试模式,打印用户选择的视频信息
399
- logInfo(`渲染序号设置\noverlay.style.top = ${config.point[0]}% \noverlay.style.left = ${config.point[1]}%`)
400
- logInfo(`用户选择了序号 ${choiceIndex + 1}: ID - ${chosenVideo.id}`)
401
-
402
-
403
- if (config.enable) { // 开启自动解析了
404
-
405
- const ret = await extractLinks(session, config, ctx, [{ type: 'Video', id: chosenVideo.id }], logger); // 提取链接
406
- if (ret && !isLinkProcessedRecently(ret, lastProcessedUrls, config, session.channelId)) {
407
- await processVideoFromLink(session, config, ctx, lastProcessedUrls, logger, ret, options); // 解析视频并返回
408
- }
409
- }
410
- })
411
- }
412
-
413
- //判断是否需要解析
414
- async function isProcessLinks(sessioncontent) {
415
- // 解析内容中的链接
416
- const links = link_type_parser(sessioncontent);
417
- if (links.length === 0) {
418
- return false; // 如果没有找到链接,返回 false
419
- }
420
- return links; // 返回解析出的链接
421
- }
422
-
423
- //提取链接
424
- async function extractLinks(session, config, ctx, links) {
425
- let ret = "";
426
- if (!config.isfigure) {
427
- ret += [(0, h)("quote", { id: session.messageId })];
428
- }
429
- let countLink = 0;
430
- let tp_ret;
431
-
432
- // 循环检测链接类型
433
- for (const element of links) {
434
- if (countLink >= 1) ret += "\n";
435
- if (countLink >= config.parseLimit) {
436
- ret += "已达到解析上限…";
437
- break;
438
- }
439
- tp_ret = await (0, type_processer)(ctx, config, element);
440
- if (tp_ret == "") {
441
- if (config.showError)
442
- ret = "无法解析链接信息。可能是 ID 不存在,或该类型可能暂不支持。";
443
- else
444
- ret = null;
445
- } else {
446
- ret += tp_ret;
447
- }
448
- countLink++;
449
- }
450
- return ret;
451
- }
452
-
453
- //判断链接是否已经处理过
454
- function isLinkProcessedRecently(ret, lastProcessedUrls, config, channelId) {
455
- const lastretUrl = extractLastUrl(ret); // 提取 ret 最后一个 http 链接作为解析目标
456
- const currentTime = Date.now();
457
-
458
- // channelId 作为 key 的一部分,分频道鉴别
459
- const channelKey = `${channelId}:${lastretUrl}`;
460
-
461
- if (lastProcessedUrls[channelKey] && (currentTime - lastProcessedUrls[channelKey] < config.MinimumTimeInterval * 1000)) {
462
- ctx.logger.info(`重复出现,略过处理:\n ${lastretUrl} (频道 ${channelId})`);
463
-
464
- return true; // 已经处理过
465
- }
466
-
467
- // 更新该链接的最后处理时间,使用 channelKey
468
- lastProcessedUrls[channelKey] = currentTime;
469
- return false; // 没有处理过
470
- }
471
-
472
- async function processVideoFromLink(session, config, ctx, lastProcessedUrls, logger, ret, options = { video: true }) {
473
- const lastretUrl = extractLastUrl(ret);
474
-
475
- // 等待提示语单独发送
476
- if (config.waitTip_Switch) {
477
- await session.send(config.waitTip_Switch);
478
- }
479
-
480
- let videoElements = []; // 用于存储视频相关元素
481
- let textElements = []; // 用于存储图文解析元素
482
- let shouldPerformTextParsing = config.linktextParsing; // 默认根据配置决定是否进行图文解析
483
-
484
- // 先进行图文解析
485
- if (shouldPerformTextParsing) {
486
- let fullText;
487
- if (config.bVideoShowLink) {
488
- fullText = ret; // 发送完整信息
489
- } else {
490
- // 去掉最后一个链接
491
- fullText = ret.replace(lastretUrl, '');
492
- }
493
-
494
- // 分割文本
495
- const textParts = fullText.split('${~~~}');
496
-
497
- // 循环处理每个分割后的部分
498
- for (const part of textParts) {
499
- const trimmedPart = part.trim(); // 去除首尾空格
500
- if (trimmedPart) { // 确保不是空字符串
501
- // 使用 h.parse 解析文本为消息元素
502
- const parsedElements = h.parse(trimmedPart);
503
-
504
- // 创建 message 元素
505
- const messageElement = h('message', {
506
- userId: session.userId,
507
- nickname: session.author?.nickname || session.username,
508
- }, parsedElements);
509
-
510
- // 添加 message 元素到 textElements
511
- textElements.push(messageElement);
512
- }
513
- }
514
- }
515
-
516
- // 视频/链接解析
517
- if (config.VideoParsing_ToLink) {
518
- const fullAPIurl = `http://api.xingzhige.cn/API/b_parse/?url=${encodeURIComponent(lastretUrl)}`;
519
-
520
- try {
521
- const responseData = await ctx.http.get(fullAPIurl);
522
-
523
- if (responseData.code === 0 && responseData.msg === "video" && responseData.data) {
524
- const { bvid, cid, video } = responseData.data;
525
- const bilibiliUrl = `https://api.bilibili.com/x/player/playurl?fnval=80&cid=${cid}&bvid=${bvid}`;
526
- const playData = await ctx.http.get(bilibiliUrl);
527
-
528
- logInfo(bilibiliUrl);
529
-
530
- if (playData.code === 0 && playData.data && playData.data.dash.duration) {
531
- const videoDurationSeconds = playData.data.dash.duration;
532
- const videoDurationMinutes = videoDurationSeconds / 60;
533
-
534
- // 检查视频是否太短
535
- if (videoDurationMinutes < config.Minimumduration) {
536
-
537
- // 根据 Minimumduration_tip 的值决定行为
538
- if (config.Minimumduration_tip === 'return') {
539
- // 不返回文字提示,直接返回
540
- return;
541
- } else if (typeof config.Minimumduration_tip === 'object') {
542
- // 返回文字提示
543
- if (config.Minimumduration_tip.tipcontent) {
544
- if (config.Minimumduration_tip.tipanalysis) {
545
- videoElements.push(h.text(config.Minimumduration_tip.tipcontent));
546
- } else {
547
- await session.send(config.Minimumduration_tip.tipcontent);
548
- }
549
- }
550
-
551
- // 决定是否进行图文解析
552
- shouldPerformTextParsing = config.Minimumduration_tip.tipanalysis === true;
553
-
554
- // 如果不进行图文解析,清空已准备的文本元素
555
- if (!shouldPerformTextParsing) {
556
- textElements = [];
557
- }
558
- }
559
- }
560
- // 检查视频是否太长
561
- else if (videoDurationMinutes > config.Maximumduration) {
562
-
563
- // 根据 Maximumduration_tip 的值决定行为
564
- if (config.Maximumduration_tip === 'return') {
565
- // 不返回文字提示,直接返回
566
- return;
567
- } else if (typeof config.Maximumduration_tip === 'object') {
568
- // 返回文字提示
569
- if (config.Maximumduration_tip.tipcontent) {
570
- if (config.Maximumduration_tip.tipanalysis) {
571
- videoElements.push(h.text(config.Maximumduration_tip.tipcontent));
572
- } else {
573
- await session.send(config.Maximumduration_tip.tipcontent);
574
- }
575
- }
576
-
577
- // 决定是否进行图文解析
578
- shouldPerformTextParsing = config.Maximumduration_tip.tipanalysis === true;
579
-
580
- // 如果不进行图文解析,清空已准备的文本元素
581
- if (!shouldPerformTextParsing) {
582
- textElements = [];
583
- }
584
- }
585
- } else {
586
- // 视频时长在允许范围内,处理视频
587
- let videoData = video.url; // 使用新变量名,避免覆盖原始URL
588
- logInfo(videoData);
589
-
590
- if (config.filebuffer) {
591
- try {
592
- const videoFileBuffer = await ctx.http.file(video.url);
593
- logInfo(videoFileBuffer);
594
-
595
- // 检查文件类型
596
- if (videoFileBuffer && videoFileBuffer.data) {
597
- // 将ArrayBuffer转换为Buffer
598
- const buffer = Buffer.from(videoFileBuffer.data);
599
-
600
- // 获取MIME类型
601
- const mimeType = videoFileBuffer.type || videoFileBuffer.mime || 'video/mp4';
602
-
603
- // 创建data URI
604
- const base64Data = buffer.toString('base64');
605
- videoData = `data:${mimeType};base64,${base64Data}`;
606
-
607
- logInfo("成功使用 ctx.http.file 将视频URL 转换为data URI格式");
608
- } else {
609
- logInfo("文件数据无效,使用原始URL");
610
- }
611
- } catch (error) {
612
- logger.error("获取视频文件失败:", error);
613
- // 出错时继续使用原始URL
614
- }
615
- }
616
-
617
- if (videoData) {
618
- if (options.link) {
619
- // 如果是链接选项,仍然使用原始URL
620
- videoElements.push(h.text(video.url));
621
- } else if (options.audio) {
622
- videoElements.push(h.audio(videoData));
623
- } else {
624
- switch (config.VideoParsing_ToLink) {
625
- case '1':
626
- break;
627
- case '2':
628
- videoElements.push(h.video(videoData));
629
- break;
630
- case '3':
631
- videoElements.push(h.text(video.url));
632
- break;
633
- case '4':
634
- videoElements.push(h.text(video.url));
635
- videoElements.push(h.video(videoData));
636
- break;
637
- case '5':
638
- logger.info(video.url);
639
- videoElements.push(h.video(videoData));
640
- break;
641
- default:
642
- break;
643
- }
644
- }
645
- } else {
646
- throw new Error("解析视频直链失败");
647
- }
648
-
649
- }
650
- } else {
651
- throw new Error("获取播放数据失败");
652
- }
653
- } else {
654
- throw new Error("解析视频信息失败或非视频类型内容");
655
- }
656
- } catch (error) {
657
- logger.error("请求解析 API 失败或处理出错:", error);
658
- }
659
- }
660
-
661
- // 准备发送的所有元素
662
- let allElements = [...textElements, ...videoElements];
663
-
664
- // 如果没有任何元素要发送,则直接返回
665
- if (allElements.length === 0) {
666
- return;
667
- }
668
-
669
- // 合并转发处理
670
- if (config.isfigure && (session.platform === "onebot" || session.platform === "red")) {
671
- logInfo(`使用合并转发,正在合并消息。`);
672
-
673
- // 创建 figure 元素
674
- const figureContent = h('figure', {
675
- children: allElements
676
- });
677
-
678
- if (config.loggerinfofulljson) {
679
- logInfo(JSON.stringify(figureContent, null, 2));
680
- }
681
-
682
- // 发送合并转发消息
683
- await session.send(figureContent);
684
- } else {
685
- // 没有启用合并转发,按顺序发送所有元素
686
- for (const element of allElements) {
687
- await session.send(element);
688
- }
689
- }
690
-
691
- logInfo(`机器人已发送完整消息。`);
692
- return;
693
- }
694
-
695
-
696
- // 提取最后一个URL
697
- function extractLastUrl(text) {
698
- const urlPattern = /https?:\/\/[^\s]+/g;
699
- const urls = text.match(urlPattern);
700
- return urls ? urls.pop() : null;
701
- }
702
-
703
- // 检测BV / AV 号并转换为URL
704
- function convertBVToUrl(text) {
705
- const bvPattern = /(?:^|\s)(BV\w{10})(?:\s|$)/g;
706
- const avPattern = /(?:^|\s)(av\d+)(?:\s|$)/g; // 新增 AV 号的正则表达式
707
- const matches = [];
708
- let match;
709
-
710
- // 查找 BV
711
- while ((match = bvPattern.exec(text)) !== null) {
712
- matches.push(`https://www.bilibili.com/video/${match[1]}`);
713
- }
714
-
715
- // 查找 AV 号
716
- while ((match = avPattern.exec(text)) !== null) {
717
- matches.push(`https://www.bilibili.com/video/${match[1]}`);
718
- }
719
-
720
- return matches;
721
- }
722
-
723
- // 记录上次处理链接的时间
724
- const lastProcessedUrls = {};
725
-
726
- /////////////////////////////////////////////////////////////////////////////////////////////////////////
727
-
728
- function numeral(number, config) {
729
- if (config.useNumeral) {
730
- if (number >= 10000 && number < 100000000) {
731
- return (number / 10000).toFixed(1) + "万";
732
- }
733
- else if (number >= 100000000) {
734
- return (number / 100000000).toFixed(1) + "亿";
735
- }
736
- else {
737
- return number.toString();
738
- }
739
- }
740
- else {
741
- return number;
742
- }
743
- }
744
-
745
- class Bili_Video {
746
- ctx;
747
- config;
748
- constructor(ctx, config) {
749
- this.ctx = ctx;
750
- this.config = config;
751
- }
752
- /**
753
- * 解析 ID 类型
754
- * @param id 视频 ID
755
- * @returns type: ID 类型, id: 视频 ID
756
- */
757
- vid_type_parse(id) {
758
- var idRegex = [
759
- {
760
- pattern: /av([0-9]+)/i,
761
- type: "av",
762
- },
763
- {
764
- pattern: /bv([0-9a-zA-Z]+)/i,
765
- type: "bv",
766
- },
767
- ];
768
- for (const rule of idRegex) {
769
- var match = id.match(rule.pattern);
770
- if (match) {
771
- return {
772
- type: rule.type,
773
- id: match[1],
774
- };
775
- }
776
- }
777
- return {
778
- type: null,
779
- id: null,
780
- };
781
- }
782
- /**
783
- * 根据视频 ID 查找视频信息
784
- * @param id 视频 ID
785
- * @returns 视频信息 Json
786
- */
787
- async fetch_video_info(id) {
788
- var ret;
789
- const vid = this.vid_type_parse(id);
790
- switch (vid["type"]) {
791
- case "av":
792
- ret = await this.ctx.http.get("https://api.bilibili.com/x/web-interface/view?aid=" + vid["id"], {
793
- headers: {
794
- "User-Agent": this.config.userAgent,
795
- },
796
- });
797
- break;
798
- case "bv":
799
- ret = await this.ctx.http.get("https://api.bilibili.com/x/web-interface/view?bvid=" + vid["id"], {
800
- headers: {
801
- "User-Agent": this.config.userAgent,
802
- },
803
- });
804
- break;
805
- default:
806
- ret = null;
807
- break;
808
- }
809
- return ret;
810
- }
811
- /**
812
- * 生成视频信息
813
- * @param id 视频 ID
814
- * @returns 文字视频信息
815
- */
816
- async gen_context(id) {
817
- const info = await this.fetch_video_info(id);
818
- if (!info || !info["data"])
819
- return null;
820
-
821
- let description = info["data"]["desc"];
822
- // 根据配置处理简介
823
- const maxLength = config.bVideoShowIntroductionTofixed;
824
- if (description.length > maxLength) {
825
- description = description.substring(0, maxLength) + '...';
826
- }
827
- // 定义占位符对应的数据
828
- const placeholders = {
829
- '${标题}': info["data"]["title"],
830
- '${UP主}': info["data"]["owner"]["name"],
831
- '${封面}': `<img src="${info["data"]["pic"]}"/>`,
832
- '${简介}': description, // 使用处理后的简介
833
- '${点赞}': `${(0, numeral)(info["data"]["stat"]["like"], this.config)}`,
834
- '${投币}': `${(0, numeral)(info["data"]["stat"]["coin"], this.config)}`,
835
- '${收藏}': `${(0, numeral)(info["data"]["stat"]["favorite"], this.config)}`,
836
- '${转发}': `${(0, numeral)(info["data"]["stat"]["share"], this.config)}`,
837
- '${观看}': `${(0, numeral)(info["data"]["stat"]["view"], this.config)}`,
838
- '${弹幕}': `${(0, numeral)(info["data"]["stat"]["danmaku"], this.config)}`,
839
- };
840
-
841
- // 根据配置项中的格式替换占位符
842
- let ret = this.config.bVideo_area;
843
- for (const [placeholder, value] of Object.entries(placeholders)) {
844
- ret = ret.replace(new RegExp(placeholder.replace(/\$/g, '\\$'), 'g'), value);
845
- }
846
-
847
- // 根据 ID 偏好添加视频链接
848
- switch (this.config.bVideoIDPreference) {
849
- case "bv":
850
- ret += `\nhttps://www.bilibili.com/video/${info["data"]["bvid"]}`;
851
- break;
852
- case "av":
853
- ret += `\nhttps://www.bilibili.com/video/av${info["data"]["aid"]}`;
854
- break;
855
- default:
856
- break;
857
- }
858
-
859
- return ret;
860
- }
861
-
862
-
863
- }
864
-
865
- /**
866
- * 链接类型解析
867
- * @param content 传入消息
868
- * @returns type: "链接类型", id :"内容ID"
869
- */
870
- function link_type_parser(content) {
871
- // 先替换转义斜杠
872
- content = content.replace(/\\\//g, '/');
873
- var linkRegex = [
874
- {
875
- pattern: /bilibili\.com\/video\/([ab]v[0-9a-zA-Z]+)/gim,
876
- type: "Video",
877
- },
878
- {
879
- pattern: /b23\.tv(?:\\)?\/([0-9a-zA-Z]+)/gim,
880
- type: "Short",
881
- },
882
- {
883
- pattern: /bili(?:22|23|33)\.cn\/([0-9a-zA-Z]+)/gim,
884
- type: "Short",
885
- },
886
- {
887
- pattern: /bili2233\.cn\/([0-9a-zA-Z]+)/gim,
888
- type: "Short",
889
- },
890
- ];
891
- var ret = [];
892
- for (const rule of linkRegex) {
893
- var match;
894
- let lastID;
895
- while ((match = rule.pattern.exec(content)) !== null) {
896
- if (lastID == match[1])
897
- continue;
898
- ret.push({
899
- type: rule.type,
900
- id: match[1],
901
- });
902
- lastID = match[1];
903
- }
904
- }
905
- return ret;
906
- }
907
-
908
- /**
909
- * 类型执行器
910
- * @param ctx Context
911
- * @param config Config
912
- * @param element 链接列表
913
- * @returns 解析来的文本
914
- */
915
- async function type_processer(ctx, config, element) {
916
- var ret = "";
917
- switch (element["type"]) {
918
- case "Video":
919
- const bili_video = new Bili_Video(ctx, config);
920
- const video_info = await bili_video.gen_context(element["id"]);
921
- if (video_info != null)
922
- ret += video_info;
923
- break;
924
-
925
- case "Short":
926
- const bili_short = new Bili_Short(ctx, config);
927
- const typed_link = link_type_parser(await bili_short.get_redir_url(element["id"]));
928
- for (const element of typed_link) {
929
- const final_info = await type_processer(ctx, config, element);
930
- if (final_info != null)
931
- ret += final_info;
932
- break;
933
- }
934
- break;
935
- }
936
- return ret;
937
- }
938
-
939
- class Bili_Short {
940
- ctx;
941
- config;
942
- constructor(ctx, config) {
943
- this.ctx = ctx;
944
- this.config = config;
945
- }
946
- /**
947
- * 根据短链接重定向获取正常链接
948
- * @param id 短链接 ID
949
- * @returns 正常链接
950
- */
951
- async get_redir_url(id) {
952
- var data = await this.ctx.http.get("https://b23.tv/" + id, {
953
- redirect: "manual",
954
- headers: {
955
- "User-Agent": this.config.userAgent,
956
- },
957
- });
958
- const match = data.match(/<a\s+(?:[^>]*?\s+)?href="([^"]*)"/i);
959
- if (match)
960
- return match[1];
961
- else
962
- return null;
963
- }
964
- }
965
- }
966
- exports.apply = apply;
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Config: () => Config,
24
+ apply: () => apply,
25
+ inject: () => inject,
26
+ name: () => name,
27
+ usage: () => usage
28
+ });
29
+ module.exports = __toCommonJS(src_exports);
30
+ var import_koishi = require("koishi");
31
+ var logger = new import_koishi.Logger("bilibili-videolink-analysis");
32
+ var name = "bilibili-videolink-analysis";
33
+ var inject = {
34
+ optional: ["puppeteer"]
35
+ // required: ['BiliBiliVideo']
36
+ };
37
+ var usage = `
38
+
39
+ <h2>→ <a href="https://www.npmjs.com/package/koishi-plugin-bilibili-videolink-analysis" target="_blank">可以点击这里查看详细的文档说明✨</a></h2>
40
+
41
+ ✨ 只需开启插件,就可以解析B站视频的链接啦~ ✨
42
+
43
+ 向bot发送B站视频链接吧~
44
+
45
+ 会返回视频信息与视频哦
46
+
47
+ ---
48
+
49
+ #### ⚠️ **如果你使用不了本项目,请优先检查:** ⚠️
50
+ #### 若无注册的指令,请关开一下[command插件](/market?keyword=commands+email:shigma10826@gmail.com)(没有指令也不影响解析别人的链接)
51
+ #### 视频内容是否为B站的大会员专属视频/付费视频/充电专属视频
52
+ #### 接入方法是否支持获取网址链接/小程序卡片消息
53
+ #### 接入方法是否支持视频元素的发送
54
+ #### 发送视频超时/其他网络问题
55
+ #### 视频内容被平台屏蔽/其他平台因素
56
+
57
+ ---
58
+
59
+ ### 注意,点播功能需要使用 puppeteer 服务
60
+
61
+ 点播功能是为了方便群友一起刷B站哦~
62
+
63
+ 比如:搜索 “遠い空へ” 的第二页,并且结果以语音格式返回
64
+
65
+ 示例:\`点播 遠い空へ -a -p 2\`
66
+
67
+
68
+ ---
69
+
70
+ ### 特别鸣谢 💖
71
+
72
+ 特别鸣谢以下项目的支持:
73
+
74
+ - [@summonhim/koishi-plugin-bili-parser](/market?keyword=bili-parser)
75
+
76
+ ---
77
+
78
+ `;
79
+ var Config = import_koishi.Schema.intersect([
80
+ import_koishi.Schema.object({
81
+ demand: import_koishi.Schema.boolean().default(true).description("开启点播指令功能<br>`其实点播登录不登录 都搜不准,登录只是写着玩的`")
82
+ }).description("点播设置(需要puppeteer服务)"),
83
+ import_koishi.Schema.union([
84
+ import_koishi.Schema.object({
85
+ demand: import_koishi.Schema.const(false).required()
86
+ }),
87
+ import_koishi.Schema.object({
88
+ demand: import_koishi.Schema.const(true),
89
+ timeout: import_koishi.Schema.number().role("slider").min(1).max(300).step(1).default(60).description("指定播放视频的输入时限。`单位 秒`"),
90
+ point: import_koishi.Schema.tuple([Number, Number]).description("序号标注位置。分别表示`距离顶部 距离左侧`的百分比").default([50, 50]),
91
+ enable: import_koishi.Schema.boolean().description("是否开启自动解析`选择对应视频 会自动解析视频内容`").default(true)
92
+ })
93
+ ]),
94
+ import_koishi.Schema.object({
95
+ enablebilianalysis: import_koishi.Schema.boolean().default(true).description("开启解析功能<br>`关闭后,解析功能将关闭`")
96
+ }).description("视频解析 - 功能开关"),
97
+ import_koishi.Schema.union([
98
+ import_koishi.Schema.object({
99
+ enablebilianalysis: import_koishi.Schema.const(false).required()
100
+ }),
101
+ import_koishi.Schema.intersect([
102
+ import_koishi.Schema.object({
103
+ enablebilianalysis: import_koishi.Schema.const(true),
104
+ // @ts-ignore // 摸了摸了
105
+ waitTip_Switch: import_koishi.Schema.union([
106
+ import_koishi.Schema.const(null).description("不返回文字提示"),
107
+ import_koishi.Schema.string().description("返回文字提示(请在右侧填写文字内容)").default("正在解析B站链接...")
108
+ ]).description("是否返回等待提示。开启后,会发送`等待提示语`"),
109
+ linktextParsing: import_koishi.Schema.boolean().default(true).description("是否返回 视频图文数据 `开启后,才发送视频数据的图文解析。`"),
110
+ VideoParsing_ToLink: import_koishi.Schema.union([
111
+ import_koishi.Schema.const("1").description("不返回视频/视频直链"),
112
+ import_koishi.Schema.const("2").description("仅返回视频"),
113
+ import_koishi.Schema.const("3").description("仅返回视频直链"),
114
+ import_koishi.Schema.const("4").description("返回视频和视频直链"),
115
+ import_koishi.Schema.const("5").description("返回视频,仅在日志记录视频直链")
116
+ ]).role("radio").default("2").description("是否返回` 视频/视频直链 `"),
117
+ BVnumberParsing: import_koishi.Schema.boolean().default(true).description("是否允许根据`独立的BV、AV号`解析视频 `开启后,可以通过视频的BV、AV号解析视频。` <br> [触发说明见README](https://www.npmjs.com/package/koishi-plugin-bilibili-videolink-analysis)"),
118
+ MinimumTimeInterval: import_koishi.Schema.number().default(180).description("若干`秒`内 不再处理相同链接 `防止多bot互相触发 导致的刷屏/性能浪费`").min(1)
119
+ }),
120
+ import_koishi.Schema.object({
121
+ enablebilianalysis: import_koishi.Schema.const(true),
122
+ Minimumduration: import_koishi.Schema.number().default(0).description("允许解析的视频最小时长(分钟)`低于这个时长 就不会发视频内容`").min(0),
123
+ Minimumduration_tip: import_koishi.Schema.union([
124
+ import_koishi.Schema.const("return").description("不返回文字提示"),
125
+ import_koishi.Schema.object({
126
+ tipcontent: import_koishi.Schema.string().default("视频太短啦!不看不看~").description("文字提示内容"),
127
+ tipanalysis: import_koishi.Schema.boolean().default(true).description("是否进行图文解析(不会返回视频链接)")
128
+ }).description("返回文字提示"),
129
+ import_koishi.Schema.const(null)
130
+ ]).description("对`过短视频`的文字提示内容").default(null),
131
+ Maximumduration: import_koishi.Schema.number().default(25).description("允许解析的视频最大时长(分钟)`超过这个时长 就不会发视频内容`").min(1),
132
+ Maximumduration_tip: import_koishi.Schema.union([
133
+ import_koishi.Schema.const("return").description("不返回文字提示"),
134
+ import_koishi.Schema.object({
135
+ tipcontent: import_koishi.Schema.string().default("视频太长啦!内容还是去B站看吧~").description("文字提示内容"),
136
+ tipanalysis: import_koishi.Schema.boolean().default(true).description("是否进行图文解析(不会返回视频链接)")
137
+ }).description("返回文字提示"),
138
+ import_koishi.Schema.const(null)
139
+ ]).description("对`过长视频`的文字提示内容").default(null)
140
+ }).description("视频解析 - 内容限制"),
141
+ import_koishi.Schema.object({
142
+ parseLimit: import_koishi.Schema.number().default(3).description("单对话多链接解析上限").hidden(),
143
+ useNumeral: import_koishi.Schema.boolean().default(true).description("使用格式化数字").hidden(),
144
+ showError: import_koishi.Schema.boolean().default(false).description("当链接不正确时提醒发送者").hidden(),
145
+ bVideoIDPreference: import_koishi.Schema.union([
146
+ import_koishi.Schema.const("bv").description("BV 号"),
147
+ import_koishi.Schema.const("av").description("AV 号")
148
+ ]).default("bv").description("ID 偏好").hidden(),
149
+ bVideo_area: import_koishi.Schema.string().role("textarea", { rows: [8, 16] }).default("${标题} --- ${UP主}\n${简介}\n点赞:${点赞} --- 投币:${投币}\n收藏:${收藏} --- 转发:${转发}\n观看:${观看} --- 弹幕:${弹幕}\n${~~~}\n${封面}").description(`图文解析的返回格式<br>
150
+ 注意变量格式,以及变量名称。<br>比如 \`\${标题}\` 不可以变成\`\${标题123}\`,你可以直接删掉但是不能修改变量名称哦<br>
151
+ 当然变量也不能无中生有,下面的默认值内容 就是所有变量了,你仅可以删去变量 或者修改变量之外的格式。<br>
152
+ · 特殊变量\`\${~~~}\`表示分割线,会把上下内容分为两个信息单独发送。\`\${tab}\`表示制表符。`),
153
+ bVideoShowLink: import_koishi.Schema.boolean().default(false).description("在末尾显示视频的链接地址 `开启可能会导致其他bot循环解析`"),
154
+ bVideoShowIntroductionTofixed: import_koishi.Schema.number().default(50).description("视频的`简介`最大的字符长度<br>超出部分会使用 `...` 代替")
155
+ }).description("链接的图文解析设置"),
156
+ import_koishi.Schema.object({
157
+ isfigure: import_koishi.Schema.boolean().default(false).description("是否开启合并转发 `仅支持 onebot 适配器` 其他平台开启 无效").experimental(),
158
+ filebuffer: import_koishi.Schema.boolean().default(true).description("是否将视频链接下载后再发送 (以解决部分onebot协议端的问题)<br>否则使用视频直链发送").experimental(),
159
+ middleware: import_koishi.Schema.boolean().default(false).description("前置中间件模式"),
160
+ userAgent: import_koishi.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")
161
+ }).description("调试设置")
162
+ ])
163
+ ]),
164
+ import_koishi.Schema.object({
165
+ pageclose: import_koishi.Schema.boolean().default(true).description("自动`page.close()`<br>非开发者请勿改动").experimental(),
166
+ loggerinfo: import_koishi.Schema.boolean().default(false).description("日志调试输出 `日常使用无需开启`<br>非开发者请勿改动").experimental(),
167
+ loggerinfofulljson: import_koishi.Schema.boolean().default(false).description("打印完整的机器人发送的json输出").experimental()
168
+ }).description("开发者选项")
169
+ ]);
170
+ function apply(ctx, config) {
171
+ const lastProcessedUrls = {};
172
+ if (config.enablebilianalysis) {
173
+ ctx.middleware(async (session, next) => {
174
+ let sessioncontent = session.stripped.content;
175
+ if (config.BVnumberParsing) {
176
+ const bvUrls = convertBVToUrl(sessioncontent);
177
+ if (bvUrls.length > 0) {
178
+ sessioncontent += "\n" + bvUrls.join("\n");
179
+ }
180
+ }
181
+ const links = await isProcessLinks(sessioncontent);
182
+ if (links) {
183
+ const ret = await extractLinks(session, links);
184
+ if (ret && !isLinkProcessedRecently(ret, session.channelId)) {
185
+ await processVideoFromLink(session, ret);
186
+ }
187
+ }
188
+ return next();
189
+ }, config.middleware);
190
+ }
191
+ if (config.demand) {
192
+ ctx.command("B站点播 [keyword]", "点播B站视频").option("video", "-v 解析返回视频").option("audio", "-a 解析返回语音").option("link", "-l 解析返回链接").option("page", "-p <page:number> 指定页数", { fallback: "1" }).example("B站点播 遠い空へ -v").action(async ({ options, session }, keyword) => {
193
+ if (!keyword) {
194
+ await session.send(import_koishi.h.text("告诉我 你想要点播的关键词吧~"));
195
+ keyword = await session.prompt(30 * 1e3);
196
+ }
197
+ const url = `https://search.bilibili.com/video?keyword=${encodeURIComponent(keyword)}&page=${options.page}&o=30`;
198
+ const page = await ctx.puppeteer.page();
199
+ await page.goto(url, {
200
+ waitUntil: "networkidle2"
201
+ });
202
+ await page.addStyleTag({
203
+ content: `
204
+ div.bili-header,
205
+ div.login-tip,
206
+ div.v-popover,
207
+ div.right-entry__outside {
208
+ display: none !important;
209
+ }
210
+ `
211
+ });
212
+ const videos = await page.evaluate((point) => {
213
+ const items = Array.from(document.querySelectorAll('.video-list-item:not([style*="display: none"])'));
214
+ return items.map((item, index) => {
215
+ const link = item.querySelector("a");
216
+ const href = link?.getAttribute("href") || "";
217
+ const idMatch = href.match(/\/video\/(BV\w+)\//);
218
+ const id = idMatch ? idMatch[1] : "";
219
+ if (!id) {
220
+ const htmlElement = item;
221
+ htmlElement.style.display = "none";
222
+ } else {
223
+ const overlay = document.createElement("div");
224
+ overlay.style.position = "absolute";
225
+ overlay.style.top = `${point[0]}%`;
226
+ overlay.style.left = `${point[1]}%`;
227
+ overlay.style.transform = "translate(-50%, -50%)";
228
+ overlay.style.fontSize = "48px";
229
+ overlay.style.fontWeight = "bold";
230
+ overlay.style.color = "black";
231
+ overlay.style.zIndex = "10";
232
+ overlay.style.backgroundColor = "rgba(255, 255, 255, 0.7)";
233
+ overlay.style.padding = "10px";
234
+ overlay.style.borderRadius = "8px";
235
+ overlay.textContent = `${index + 1}`;
236
+ const videoElement = item;
237
+ videoElement.style.position = "relative";
238
+ videoElement.appendChild(overlay);
239
+ }
240
+ return { id };
241
+ }).filter((video) => video.id);
242
+ }, config.point);
243
+ logInfo(options);
244
+ logInfo(`共找到 ${videos.length} 个视频:`);
245
+ videos.forEach((video, index) => {
246
+ logInfo(`序号 ${index + 1}: ID - ${video.id}`);
247
+ });
248
+ if (videos.length === 0) {
249
+ await page.close();
250
+ await session.send(import_koishi.h.text("未找到相关视频。"));
251
+ return;
252
+ }
253
+ const viewportHeight = 200 + videos.length * 100;
254
+ await page.setViewport({
255
+ width: 1440,
256
+ height: viewportHeight
257
+ });
258
+ logInfo("窗口:宽度:");
259
+ logInfo(1440);
260
+ logInfo("窗口:高度:");
261
+ logInfo(viewportHeight);
262
+ let msg;
263
+ const videoListElement = await page.$(".video-list.row");
264
+ if (videoListElement) {
265
+ const imgBuf = await videoListElement.screenshot({
266
+ captureBeyondViewport: false
267
+ });
268
+ msg = import_koishi.h.image(imgBuf, "image/png");
269
+ }
270
+ if (page && config.pageclose) {
271
+ await page.close();
272
+ }
273
+ await session.send(msg + import_koishi.h.text(`请选择视频的序号:`));
274
+ const userChoice = await session.prompt(config.timeout * 1e3);
275
+ const choiceIndex = parseInt(userChoice) - 1;
276
+ if (isNaN(choiceIndex) || choiceIndex < 0 || choiceIndex >= videos.length) {
277
+ await session.send(import_koishi.h.text("输入无效,请输入正确的序号。"));
278
+ return;
279
+ }
280
+ const chosenVideo = videos[choiceIndex];
281
+ logInfo(`渲染序号设置
282
+ overlay.style.top = ${config.point[0]}%
283
+ overlay.style.left = ${config.point[1]}%`);
284
+ logInfo(`用户选择了序号 ${choiceIndex + 1}: ID - ${chosenVideo.id}`);
285
+ if (config.enable) {
286
+ const ret = await extractLinks(session, [{ type: "Video", id: chosenVideo.id }]);
287
+ if (ret && !isLinkProcessedRecently(ret, session.channelId)) {
288
+ await processVideoFromLink(session, ret, options);
289
+ }
290
+ }
291
+ });
292
+ }
293
+ function logInfo(...args) {
294
+ if (config.loggerinfo) {
295
+ logger.info(...args);
296
+ }
297
+ }
298
+ __name(logInfo, "logInfo");
299
+ async function isProcessLinks(sessioncontent) {
300
+ const links = link_type_parser(sessioncontent);
301
+ if (links.length === 0) {
302
+ return false;
303
+ }
304
+ return links;
305
+ }
306
+ __name(isProcessLinks, "isProcessLinks");
307
+ async function extractLinks(session, links) {
308
+ let ret = "";
309
+ if (!config.isfigure) {
310
+ ret += (0, import_koishi.h)("quote", { id: session.messageId });
311
+ }
312
+ let countLink = 0;
313
+ let tp_ret;
314
+ for (const element of links) {
315
+ if (countLink >= 1) ret += "\n";
316
+ if (countLink >= config.parseLimit) {
317
+ ret += "已达到解析上限…";
318
+ break;
319
+ }
320
+ tp_ret = await type_processer(element);
321
+ if (tp_ret == "") {
322
+ if (config.showError)
323
+ ret = "无法解析链接信息。可能是 ID 不存在,或该类型可能暂不支持。";
324
+ else
325
+ ret = null;
326
+ } else {
327
+ ret += tp_ret;
328
+ }
329
+ countLink++;
330
+ }
331
+ return ret;
332
+ }
333
+ __name(extractLinks, "extractLinks");
334
+ function isLinkProcessedRecently(ret, channelId) {
335
+ const lastretUrl = extractLastUrl(ret);
336
+ const currentTime = Date.now();
337
+ const channelKey = `${channelId}:${lastretUrl}`;
338
+ if (lastretUrl && lastProcessedUrls[channelKey] && currentTime - lastProcessedUrls[channelKey] < config.MinimumTimeInterval * 1e3) {
339
+ ctx.logger.info(`重复出现,略过处理:
340
+ ${lastretUrl} (频道 ${channelId})`);
341
+ return true;
342
+ }
343
+ if (lastretUrl) {
344
+ lastProcessedUrls[channelKey] = currentTime;
345
+ }
346
+ return false;
347
+ }
348
+ __name(isLinkProcessedRecently, "isLinkProcessedRecently");
349
+ async function processVideoFromLink(session, ret, options = { video: true }) {
350
+ const lastretUrl = extractLastUrl(ret);
351
+ let waitTipMsgId = null;
352
+ if (config.waitTip_Switch) {
353
+ const result = await session.send(`${import_koishi.h.quote(session.messageId)}${config.waitTip_Switch}`);
354
+ waitTipMsgId = Array.isArray(result) ? result[0] : result;
355
+ }
356
+ let videoElements = [];
357
+ let textElements = [];
358
+ let shouldPerformTextParsing = config.linktextParsing;
359
+ if (shouldPerformTextParsing) {
360
+ let fullText;
361
+ if (config.bVideoShowLink) {
362
+ fullText = ret;
363
+ } else {
364
+ fullText = ret.replace(lastretUrl, "");
365
+ }
366
+ const textParts = fullText.split("${~~~}");
367
+ for (const part of textParts) {
368
+ const trimmedPart = part.trim();
369
+ if (trimmedPart) {
370
+ const parsedElements = import_koishi.h.parse(trimmedPart);
371
+ const messageElement = (0, import_koishi.h)("message", {
372
+ userId: session.userId,
373
+ nickname: session.author?.nickname || session.username
374
+ }, parsedElements);
375
+ textElements.push(messageElement);
376
+ }
377
+ }
378
+ }
379
+ if (config.VideoParsing_ToLink) {
380
+ const fullAPIurl = `http://api.xingzhige.cn/API/b_parse/?url=${encodeURIComponent(lastretUrl)}`;
381
+ try {
382
+ const responseData = await ctx.http.get(fullAPIurl);
383
+ if (responseData.code === 0 && responseData.msg === "video" && responseData.data) {
384
+ const { bvid, cid, video } = responseData.data;
385
+ const bilibiliUrl = `https://api.bilibili.com/x/player/playurl?fnval=80&cid=${cid}&bvid=${bvid}`;
386
+ const playData = await ctx.http.get(bilibiliUrl);
387
+ logInfo(bilibiliUrl);
388
+ if (playData.code === 0 && playData.data && playData.data.dash && playData.data.dash.duration) {
389
+ const videoDurationSeconds = playData.data.dash.duration;
390
+ const videoDurationMinutes = videoDurationSeconds / 60;
391
+ if (videoDurationMinutes < config.Minimumduration) {
392
+ if (config.Minimumduration_tip === "return") {
393
+ return;
394
+ } else if (typeof config.Minimumduration_tip === "object" && config.Minimumduration_tip !== null) {
395
+ if (config.Minimumduration_tip.tipcontent) {
396
+ if (config.Minimumduration_tip.tipanalysis) {
397
+ videoElements.push(import_koishi.h.text(config.Minimumduration_tip.tipcontent));
398
+ } else {
399
+ await session.send(config.Minimumduration_tip.tipcontent);
400
+ }
401
+ }
402
+ shouldPerformTextParsing = config.Minimumduration_tip.tipanalysis === true;
403
+ if (!shouldPerformTextParsing) {
404
+ textElements = [];
405
+ }
406
+ }
407
+ } else if (videoDurationMinutes > config.Maximumduration) {
408
+ if (config.Maximumduration_tip === "return") {
409
+ return;
410
+ } else if (typeof config.Maximumduration_tip === "object" && config.Maximumduration_tip !== null) {
411
+ if (config.Maximumduration_tip.tipcontent) {
412
+ if (config.Maximumduration_tip.tipanalysis) {
413
+ videoElements.push(import_koishi.h.text(config.Maximumduration_tip.tipcontent));
414
+ } else {
415
+ await session.send(config.Maximumduration_tip.tipcontent);
416
+ }
417
+ }
418
+ shouldPerformTextParsing = config.Maximumduration_tip.tipanalysis === true;
419
+ if (!shouldPerformTextParsing) {
420
+ textElements = [];
421
+ }
422
+ }
423
+ } else {
424
+ let videoData = video.url;
425
+ logInfo(videoData);
426
+ if (config.filebuffer) {
427
+ try {
428
+ const videoFileBuffer = await ctx.http.file(video.url);
429
+ logInfo(videoFileBuffer);
430
+ if (videoFileBuffer && videoFileBuffer.data) {
431
+ const buffer = Buffer.from(videoFileBuffer.data);
432
+ const mimeType = videoFileBuffer.type || videoFileBuffer.mime || "video/mp4";
433
+ const base64Data = buffer.toString("base64");
434
+ videoData = `data:${mimeType};base64,${base64Data}`;
435
+ logInfo("成功使用 ctx.http.file 将视频URL 转换为data URI格式");
436
+ } else {
437
+ logInfo("文件数据无效,使用原始URL");
438
+ }
439
+ } catch (error) {
440
+ logger.error("获取视频文件失败:", error);
441
+ }
442
+ }
443
+ if (videoData) {
444
+ if (options.link) {
445
+ videoElements.push(import_koishi.h.text(video.url));
446
+ } else if (options.audio) {
447
+ videoElements.push(import_koishi.h.audio(videoData));
448
+ } else {
449
+ switch (config.VideoParsing_ToLink) {
450
+ case "1":
451
+ break;
452
+ case "2":
453
+ videoElements.push(import_koishi.h.video(videoData));
454
+ break;
455
+ case "3":
456
+ videoElements.push(import_koishi.h.text(video.url));
457
+ break;
458
+ case "4":
459
+ videoElements.push(import_koishi.h.text(video.url));
460
+ videoElements.push(import_koishi.h.video(videoData));
461
+ break;
462
+ case "5":
463
+ logger.info(video.url);
464
+ videoElements.push(import_koishi.h.video(videoData));
465
+ break;
466
+ default:
467
+ break;
468
+ }
469
+ }
470
+ } else {
471
+ throw new Error("解析视频直链失败");
472
+ }
473
+ }
474
+ } else {
475
+ throw new Error("获取播放数据失败");
476
+ }
477
+ } else {
478
+ throw new Error("解析视频信息失败或非视频类型内容");
479
+ }
480
+ } catch (error) {
481
+ logger.error("请求解析 API 失败或处理出错:", error);
482
+ }
483
+ }
484
+ let allElements = [...textElements, ...videoElements];
485
+ if (allElements.length === 0) {
486
+ return;
487
+ }
488
+ if (config.isfigure && (session.platform === "onebot" || session.platform === "red")) {
489
+ logInfo(`使用合并转发,正在合并消息。`);
490
+ const figureContent = (0, import_koishi.h)("figure", {
491
+ children: allElements
492
+ });
493
+ if (config.loggerinfofulljson) {
494
+ logInfo(JSON.stringify(figureContent, null, 2));
495
+ }
496
+ await session.send(figureContent);
497
+ } else {
498
+ for (const element of allElements) {
499
+ await session.send(element);
500
+ }
501
+ }
502
+ logInfo(`机器人已发送完整消息。`);
503
+ if (waitTipMsgId) {
504
+ await session.bot.deleteMessage(session.channelId, waitTipMsgId);
505
+ }
506
+ return;
507
+ }
508
+ __name(processVideoFromLink, "processVideoFromLink");
509
+ function extractLastUrl(text) {
510
+ const urlPattern = /https?:\/\/[^\s]+/g;
511
+ const urls = text.match(urlPattern);
512
+ return urls ? urls.pop() : null;
513
+ }
514
+ __name(extractLastUrl, "extractLastUrl");
515
+ function convertBVToUrl(text) {
516
+ const bvPattern = /(?:^|\s)(BV\w{10})(?:\s|$)/g;
517
+ const avPattern = /(?:^|\s)(av\d+)(?:\s|$)/g;
518
+ const matches = [];
519
+ let match;
520
+ while ((match = bvPattern.exec(text)) !== null) {
521
+ matches.push(`https://www.bilibili.com/video/${match[1]}`);
522
+ }
523
+ while ((match = avPattern.exec(text)) !== null) {
524
+ matches.push(`https://www.bilibili.com/video/${match[1]}`);
525
+ }
526
+ return matches;
527
+ }
528
+ __name(convertBVToUrl, "convertBVToUrl");
529
+ function numeral(number) {
530
+ if (config.useNumeral) {
531
+ if (number >= 1e4 && number < 1e8) {
532
+ return (number / 1e4).toFixed(1) + "万";
533
+ } else if (number >= 1e8) {
534
+ return (number / 1e8).toFixed(1) + "亿";
535
+ } else {
536
+ return number.toString();
537
+ }
538
+ } else {
539
+ return number;
540
+ }
541
+ }
542
+ __name(numeral, "numeral");
543
+ function vid_type_parse(id) {
544
+ var idRegex = [
545
+ {
546
+ pattern: /av([0-9]+)/i,
547
+ type: "av"
548
+ },
549
+ {
550
+ pattern: /bv([0-9a-zA-Z]+)/i,
551
+ type: "bv"
552
+ }
553
+ ];
554
+ for (const rule of idRegex) {
555
+ var match = id.match(rule.pattern);
556
+ if (match) {
557
+ return {
558
+ type: rule.type,
559
+ id: match[1]
560
+ };
561
+ }
562
+ }
563
+ return {
564
+ type: null,
565
+ id: null
566
+ };
567
+ }
568
+ __name(vid_type_parse, "vid_type_parse");
569
+ async function fetch_video_info(id) {
570
+ var ret;
571
+ const vid = vid_type_parse(id);
572
+ switch (vid["type"]) {
573
+ case "av":
574
+ ret = await ctx.http.get("https://api.bilibili.com/x/web-interface/view?aid=" + vid["id"], {
575
+ headers: {
576
+ "User-Agent": config.userAgent
577
+ }
578
+ });
579
+ break;
580
+ case "bv":
581
+ ret = await ctx.http.get("https://api.bilibili.com/x/web-interface/view?bvid=" + vid["id"], {
582
+ headers: {
583
+ "User-Agent": config.userAgent
584
+ }
585
+ });
586
+ break;
587
+ default:
588
+ ret = null;
589
+ break;
590
+ }
591
+ return ret;
592
+ }
593
+ __name(fetch_video_info, "fetch_video_info");
594
+ async function gen_context(id) {
595
+ const info = await fetch_video_info(id);
596
+ if (!info || !info["data"])
597
+ return null;
598
+ let description = info["data"]["desc"];
599
+ const maxLength = config.bVideoShowIntroductionTofixed;
600
+ if (description.length > maxLength) {
601
+ description = description.substring(0, maxLength) + "...";
602
+ }
603
+ const placeholders = {
604
+ "${标题}": info["data"]["title"],
605
+ "${UP主}": info["data"]["owner"]["name"],
606
+ "${封面}": `<img src="${info["data"]["pic"]}"/>`,
607
+ "${简介}": description,
608
+ // 使用处理后的简介
609
+ "${点赞}": `${numeral(info["data"]["stat"]["like"])}`,
610
+ "${投币}": `${numeral(info["data"]["stat"]["coin"])}`,
611
+ "${收藏}": `${numeral(info["data"]["stat"]["favorite"])}`,
612
+ "${转发}": `${numeral(info["data"]["stat"]["share"])}`,
613
+ "${观看}": `${numeral(info["data"]["stat"]["view"])}`,
614
+ "${弹幕}": `${numeral(info["data"]["stat"]["danmaku"])}`,
615
+ "${tab}": `<pre> </pre>`
616
+ };
617
+ let ret = config.bVideo_area;
618
+ for (const [placeholder, value] of Object.entries(placeholders)) {
619
+ ret = ret.replace(new RegExp(placeholder.replace(/\$/g, "\\$"), "g"), value);
620
+ }
621
+ switch (config.bVideoIDPreference) {
622
+ case "bv":
623
+ ret += `
624
+ https://www.bilibili.com/video/${info["data"]["bvid"]}`;
625
+ break;
626
+ case "av":
627
+ ret += `
628
+ https://www.bilibili.com/video/av${info["data"]["aid"]}`;
629
+ break;
630
+ default:
631
+ break;
632
+ }
633
+ return ret;
634
+ }
635
+ __name(gen_context, "gen_context");
636
+ function link_type_parser(content) {
637
+ content = content.replace(/\\\//g, "/");
638
+ var linkRegex = [
639
+ {
640
+ pattern: /bilibili\.com\/video\/([ab]v[0-9a-zA-Z]+)/gim,
641
+ type: "Video"
642
+ },
643
+ {
644
+ pattern: /b23\.tv(?:\\)?\/([0-9a-zA-Z]+)/gim,
645
+ type: "Short"
646
+ },
647
+ {
648
+ pattern: /bili(?:22|23|33)\.cn\/([0-9a-zA-Z]+)/gim,
649
+ type: "Short"
650
+ },
651
+ {
652
+ pattern: /bili2233\.cn\/([0-9a-zA-Z]+)/gim,
653
+ type: "Short"
654
+ }
655
+ ];
656
+ var ret = [];
657
+ for (const rule of linkRegex) {
658
+ var match;
659
+ let lastID;
660
+ while ((match = rule.pattern.exec(content)) !== null) {
661
+ if (lastID == match[1])
662
+ continue;
663
+ ret.push({
664
+ type: rule.type,
665
+ id: match[1]
666
+ });
667
+ lastID = match[1];
668
+ }
669
+ }
670
+ return ret;
671
+ }
672
+ __name(link_type_parser, "link_type_parser");
673
+ async function type_processer(element) {
674
+ var ret = "";
675
+ switch (element["type"]) {
676
+ case "Video":
677
+ const video_info = await gen_context(element["id"]);
678
+ if (video_info != null)
679
+ ret += video_info;
680
+ break;
681
+ case "Short":
682
+ const typed_link = link_type_parser(await get_redir_url(element["id"]));
683
+ for (const element2 of typed_link) {
684
+ const final_info = await type_processer(element2);
685
+ if (final_info != null)
686
+ ret += final_info;
687
+ break;
688
+ }
689
+ break;
690
+ }
691
+ return ret;
692
+ }
693
+ __name(type_processer, "type_processer");
694
+ async function get_redir_url(id) {
695
+ var data = await ctx.http.get("https://b23.tv/" + id, {
696
+ redirect: "manual",
697
+ headers: {
698
+ "User-Agent": config.userAgent
699
+ }
700
+ });
701
+ const match = data.match(/<a\s+(?:[^>]*?\s+)?href="([^"]*)"/i);
702
+ if (match)
703
+ return match[1];
704
+ else
705
+ return null;
706
+ }
707
+ __name(get_redir_url, "get_redir_url");
708
+ }
709
+ __name(apply, "apply");
710
+ // Annotate the CommonJS export names for ESM import in node:
711
+ 0 && (module.exports = {
712
+ Config,
713
+ apply,
714
+ inject,
715
+ name,
716
+ usage
717
+ });