koishi-plugin-bilibili-videolink-analysis 1.1.25 → 1.3.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,752 @@
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_koishi2 = require("koishi");
31
+
32
+ // src/utils.ts
33
+ var import_koishi = require("koishi");
34
+ var BilibiliParser = class {
35
+ constructor(ctx, config, logger2) {
36
+ this.ctx = ctx;
37
+ this.config = config;
38
+ this.logger = logger2;
39
+ }
40
+ static {
41
+ __name(this, "BilibiliParser");
42
+ }
43
+ lastProcessedUrls = {};
44
+ logInfo(...args) {
45
+ if (this.config.loggerinfo) {
46
+ this.logger.info(...args);
47
+ }
48
+ }
49
+ // 判断是否需要解析
50
+ async isProcessLinks(sessioncontent) {
51
+ const links = this.link_type_parser(sessioncontent);
52
+ if (links.length === 0) {
53
+ return false;
54
+ }
55
+ return links;
56
+ }
57
+ //提取链接
58
+ async extractLinks(session, links) {
59
+ let ret = "";
60
+ if (!this.config.isfigure) {
61
+ ret += (0, import_koishi.h)("quote", { id: session.messageId });
62
+ }
63
+ let countLink = 0;
64
+ let tp_ret;
65
+ for (const element of links) {
66
+ if (countLink >= 1) ret += "\n";
67
+ if (countLink >= this.config.parseLimit) {
68
+ ret += "已达到解析上限…";
69
+ break;
70
+ }
71
+ tp_ret = await this.type_processer(element);
72
+ if (tp_ret == "") {
73
+ if (this.config.showError)
74
+ ret = "无法解析链接信息。可能是 ID 不存在,或该类型可能暂不支持。";
75
+ else
76
+ ret = null;
77
+ } else {
78
+ ret += tp_ret;
79
+ }
80
+ countLink++;
81
+ }
82
+ return ret;
83
+ }
84
+ //判断链接是否已经处理过
85
+ isLinkProcessedRecently(ret, channelId) {
86
+ const lastretUrl = this.extractLastUrl(ret);
87
+ const currentTime = Date.now();
88
+ const channelKey = `${channelId}:${lastretUrl}`;
89
+ if (lastretUrl && this.lastProcessedUrls[channelKey] && currentTime - this.lastProcessedUrls[channelKey] < this.config.MinimumTimeInterval * 1e3) {
90
+ this.ctx.logger.info(`重复出现,略过处理:
91
+ ${lastretUrl} (频道 ${channelId})`);
92
+ return true;
93
+ }
94
+ if (lastretUrl) {
95
+ this.lastProcessedUrls[channelKey] = currentTime;
96
+ }
97
+ return false;
98
+ }
99
+ async processVideoFromLink(session, ret, options = { video: true }) {
100
+ const lastretUrl = this.extractLastUrl(ret);
101
+ let waitTipMsgId = null;
102
+ if (this.config.waitTip_Switch) {
103
+ const result = await session.send(`${import_koishi.h.quote(session.messageId)}${this.config.waitTip_Switch}`);
104
+ waitTipMsgId = Array.isArray(result) ? result[0] : result;
105
+ }
106
+ let videoElements = [];
107
+ let textElements = [];
108
+ let shouldPerformTextParsing = this.config.videoParseComponents.includes("text");
109
+ if (shouldPerformTextParsing) {
110
+ let fullText;
111
+ if (this.config.bVideoShowLink) {
112
+ fullText = ret;
113
+ } else {
114
+ fullText = ret.replace(lastretUrl, "");
115
+ }
116
+ const textParts = fullText.split("${~~~}");
117
+ for (const part of textParts) {
118
+ const trimmedPart = part.trim();
119
+ if (trimmedPart) {
120
+ const parsedElements = import_koishi.h.parse(trimmedPart);
121
+ const messageElement = (0, import_koishi.h)("message", {
122
+ userId: session.userId,
123
+ nickname: session.author?.nickname || session.username
124
+ }, parsedElements);
125
+ textElements.push(messageElement);
126
+ }
127
+ }
128
+ }
129
+ if (this.config.videoParseComponents.length > 0) {
130
+ const fullAPIurl = `http://api.xingzhige.cn/API/b_parse/?url=${encodeURIComponent(lastretUrl)}`;
131
+ try {
132
+ const responseData = await this.ctx.http.get(fullAPIurl);
133
+ if (responseData.code === 0 && responseData.msg === "video" && responseData.data) {
134
+ const { bvid, cid, video } = responseData.data;
135
+ const bilibiliUrl = `https://api.bilibili.com/x/player/playurl?fnval=80&cid=${cid}&bvid=${bvid}`;
136
+ const playData = await this.ctx.http.get(bilibiliUrl);
137
+ this.logInfo(bilibiliUrl);
138
+ if (playData.code === 0 && playData.data && playData.data.dash && playData.data.dash.duration) {
139
+ const videoDurationSeconds = playData.data.dash.duration;
140
+ const videoDurationMinutes = videoDurationSeconds / 60;
141
+ if (videoDurationMinutes < this.config.Minimumduration) {
142
+ if (this.config.Minimumduration_tip === "return") {
143
+ return;
144
+ } else if (typeof this.config.Minimumduration_tip === "object" && this.config.Minimumduration_tip !== null) {
145
+ if (this.config.Minimumduration_tip.tipcontent) {
146
+ if (this.config.Minimumduration_tip.tipanalysis) {
147
+ videoElements.push(import_koishi.h.text(this.config.Minimumduration_tip.tipcontent));
148
+ } else {
149
+ await session.send(this.config.Minimumduration_tip.tipcontent);
150
+ }
151
+ }
152
+ shouldPerformTextParsing = this.config.Minimumduration_tip.tipanalysis === true;
153
+ if (!shouldPerformTextParsing) {
154
+ textElements = [];
155
+ }
156
+ }
157
+ } else if (videoDurationMinutes > this.config.Maximumduration) {
158
+ if (this.config.Maximumduration_tip === "return") {
159
+ return;
160
+ } else if (typeof this.config.Maximumduration_tip === "object" && this.config.Maximumduration_tip !== null) {
161
+ if (this.config.Maximumduration_tip.tipcontent) {
162
+ if (this.config.Maximumduration_tip.tipanalysis) {
163
+ videoElements.push(import_koishi.h.text(this.config.Maximumduration_tip.tipcontent));
164
+ } else {
165
+ await session.send(this.config.Maximumduration_tip.tipcontent);
166
+ }
167
+ }
168
+ shouldPerformTextParsing = this.config.Maximumduration_tip.tipanalysis === true;
169
+ if (!shouldPerformTextParsing) {
170
+ textElements = [];
171
+ }
172
+ }
173
+ } else {
174
+ let videoData = video.url;
175
+ this.logInfo(videoData);
176
+ if (this.config.filebuffer) {
177
+ try {
178
+ const videoFileBuffer = await this.ctx.http.file(video.url);
179
+ this.logInfo(videoFileBuffer);
180
+ if (videoFileBuffer && videoFileBuffer.data) {
181
+ const buffer = Buffer.from(videoFileBuffer.data);
182
+ const mimeType = videoFileBuffer.type || videoFileBuffer.mime || "video/mp4";
183
+ const base64Data = buffer.toString("base64");
184
+ videoData = `data:${mimeType};base64,${base64Data}`;
185
+ this.logInfo("成功使用 ctx.http.file 将视频URL 转换为data URI格式");
186
+ } else {
187
+ this.logInfo("文件数据无效,使用原始URL");
188
+ }
189
+ } catch (error) {
190
+ this.logger.error("获取视频文件失败:", error);
191
+ }
192
+ }
193
+ if (videoData) {
194
+ if (options.link) {
195
+ videoElements.push(import_koishi.h.text(video.url));
196
+ } else if (options.audio) {
197
+ videoElements.push(import_koishi.h.audio(videoData));
198
+ } else {
199
+ if (this.config.videoParseComponents.includes("log")) {
200
+ this.logger.info(video.url);
201
+ }
202
+ if (this.config.videoParseComponents.includes("link")) {
203
+ videoElements.push(import_koishi.h.text(video.url));
204
+ }
205
+ if (this.config.videoParseComponents.includes("video")) {
206
+ videoElements.push(import_koishi.h.video(videoData));
207
+ }
208
+ }
209
+ } else {
210
+ throw new Error("解析视频直链失败");
211
+ }
212
+ }
213
+ } else {
214
+ throw new Error("获取播放数据失败");
215
+ }
216
+ } else {
217
+ throw new Error("解析视频信息失败或非视频类型内容");
218
+ }
219
+ } catch (error) {
220
+ this.logger.error("请求解析 API 失败或处理出错:", error);
221
+ }
222
+ }
223
+ let allElements = [...textElements, ...videoElements];
224
+ if (allElements.length === 0) {
225
+ return;
226
+ }
227
+ if (this.config.isfigure && (session.platform === "onebot" || session.platform === "red")) {
228
+ this.logInfo(`使用合并转发,正在合并消息。`);
229
+ const figureContent = (0, import_koishi.h)("figure", {
230
+ children: allElements
231
+ });
232
+ if (this.config.loggerinfofulljson) {
233
+ this.logInfo(JSON.stringify(figureContent, null, 2));
234
+ }
235
+ await session.send(figureContent);
236
+ } else {
237
+ for (const element of allElements) {
238
+ await session.send(element);
239
+ }
240
+ }
241
+ this.logInfo(`机器人已发送完整消息。`);
242
+ if (waitTipMsgId) {
243
+ await session.bot.deleteMessage(session.channelId, waitTipMsgId);
244
+ }
245
+ return;
246
+ }
247
+ // 提取最后一个URL
248
+ extractLastUrl(text) {
249
+ const urlPattern = /https?:\/\/[^\s]+/g;
250
+ const urls = text.match(urlPattern);
251
+ return urls ? urls.pop() : null;
252
+ }
253
+ // 检测BV / AV 号并转换为URL
254
+ convertBVToUrl(text) {
255
+ const bvPattern = /(?:^|\s)(BV\w{10})(?:\s|$)/g;
256
+ const avPattern = /(?:^|\s)(av\d+)(?:\s|$)/g;
257
+ const matches = [];
258
+ let match;
259
+ while ((match = bvPattern.exec(text)) !== null) {
260
+ matches.push(`https://www.bilibili.com/video/${match[1]}`);
261
+ }
262
+ while ((match = avPattern.exec(text)) !== null) {
263
+ matches.push(`https://www.bilibili.com/video/${match[1]}`);
264
+ }
265
+ return matches;
266
+ }
267
+ numeral(number) {
268
+ if (this.config.useNumeral) {
269
+ if (number >= 1e4 && number < 1e8) {
270
+ return (number / 1e4).toFixed(1) + "万";
271
+ } else if (number >= 1e8) {
272
+ return (number / 1e8).toFixed(1) + "亿";
273
+ } else {
274
+ return number.toString();
275
+ }
276
+ } else {
277
+ return number;
278
+ }
279
+ }
280
+ /**
281
+ * 解析 ID 类型
282
+ * @param id 视频 ID
283
+ * @returns type: ID 类型, id: 视频 ID
284
+ */
285
+ vid_type_parse(id) {
286
+ var idRegex = [
287
+ {
288
+ pattern: /av([0-9]+)/i,
289
+ type: "av"
290
+ },
291
+ {
292
+ pattern: /bv([0-9a-zA-Z]+)/i,
293
+ type: "bv"
294
+ }
295
+ ];
296
+ for (const rule of idRegex) {
297
+ var match = id.match(rule.pattern);
298
+ if (match) {
299
+ return {
300
+ type: rule.type,
301
+ id: match[1]
302
+ };
303
+ }
304
+ }
305
+ return {
306
+ type: null,
307
+ id: null
308
+ };
309
+ }
310
+ /**
311
+ * 根据视频 ID 查找视频信息
312
+ * @param id 视频 ID
313
+ * @returns 视频信息 Json
314
+ */
315
+ async fetch_video_info(id) {
316
+ var ret;
317
+ const vid = this.vid_type_parse(id);
318
+ switch (vid["type"]) {
319
+ case "av":
320
+ ret = await this.ctx.http.get("https://api.bilibili.com/x/web-interface/view?aid=" + vid["id"], {
321
+ headers: {
322
+ "User-Agent": this.config.userAgent
323
+ }
324
+ });
325
+ break;
326
+ case "bv":
327
+ ret = await this.ctx.http.get("https://api.bilibili.com/x/web-interface/view?bvid=" + vid["id"], {
328
+ headers: {
329
+ "User-Agent": this.config.userAgent
330
+ }
331
+ });
332
+ break;
333
+ default:
334
+ ret = null;
335
+ break;
336
+ }
337
+ return ret;
338
+ }
339
+ /**
340
+ * 生成视频信息
341
+ * @param id 视频 ID
342
+ * @returns 文字视频信息
343
+ */
344
+ async gen_context(id) {
345
+ const info = await this.fetch_video_info(id);
346
+ if (!info || !info["data"])
347
+ return null;
348
+ let description = info["data"]["desc"];
349
+ const maxLength = this.config.bVideoShowIntroductionTofixed;
350
+ if (description.length > maxLength) {
351
+ description = description.substring(0, maxLength) + "...";
352
+ }
353
+ const placeholders = {
354
+ "${标题}": info["data"]["title"],
355
+ "${UP主}": info["data"]["owner"]["name"],
356
+ "${封面}": `<img src="${info["data"]["pic"]}"/>`,
357
+ "${简介}": description,
358
+ // 使用处理后的简介
359
+ "${点赞}": `${this.numeral(info["data"]["stat"]["like"])}`,
360
+ "${投币}": `${this.numeral(info["data"]["stat"]["coin"])}`,
361
+ "${收藏}": `${this.numeral(info["data"]["stat"]["favorite"])}`,
362
+ "${转发}": `${this.numeral(info["data"]["stat"]["share"])}`,
363
+ "${观看}": `${this.numeral(info["data"]["stat"]["view"])}`,
364
+ "${弹幕}": `${this.numeral(info["data"]["stat"]["danmaku"])}`,
365
+ "${tab}": `<pre> </pre>`
366
+ };
367
+ let ret = this.config.bVideo_area;
368
+ for (const [placeholder, value] of Object.entries(placeholders)) {
369
+ ret = ret.replace(new RegExp(placeholder.replace(/\$/g, "\\$"), "g"), value);
370
+ }
371
+ switch (this.config.bVideoIDPreference) {
372
+ case "bv":
373
+ ret += `
374
+ https://www.bilibili.com/video/${info["data"]["bvid"]}`;
375
+ break;
376
+ case "av":
377
+ ret += `
378
+ https://www.bilibili.com/video/av${info["data"]["aid"]}`;
379
+ break;
380
+ default:
381
+ break;
382
+ }
383
+ return ret;
384
+ }
385
+ /**
386
+ * 链接类型解析
387
+ * @param content 传入消息
388
+ * @returns type: "链接类型", id :"内容ID"
389
+ */
390
+ link_type_parser(content) {
391
+ content = content.replace(/\\\//g, "/");
392
+ var linkRegex = [
393
+ {
394
+ pattern: /bilibili\.com\/video\/([ab]v[0-9a-zA-Z]+)/gim,
395
+ type: "Video"
396
+ },
397
+ {
398
+ pattern: /b23\.tv(?:\\)?\/([0-9a-zA-Z]+)/gim,
399
+ type: "Short"
400
+ },
401
+ {
402
+ pattern: /bili(?:22|23|33)\.cn\/([0-9a-zA-Z]+)/gim,
403
+ type: "Short"
404
+ },
405
+ {
406
+ pattern: /bili2233\.cn\/([0-9a-zA-Z]+)/gim,
407
+ type: "Short"
408
+ }
409
+ ];
410
+ var ret = [];
411
+ for (const rule of linkRegex) {
412
+ var match;
413
+ let lastID;
414
+ while ((match = rule.pattern.exec(content)) !== null) {
415
+ if (lastID == match[1])
416
+ continue;
417
+ ret.push({
418
+ type: rule.type,
419
+ id: match[1]
420
+ });
421
+ lastID = match[1];
422
+ }
423
+ }
424
+ return ret;
425
+ }
426
+ /**
427
+ * 类型执行器
428
+ * @param element 链接列表
429
+ * @returns 解析来的文本
430
+ */
431
+ async type_processer(element) {
432
+ var ret = "";
433
+ switch (element["type"]) {
434
+ case "Video":
435
+ const video_info = await this.gen_context(element["id"]);
436
+ if (video_info != null)
437
+ ret += video_info;
438
+ break;
439
+ case "Short":
440
+ const typed_link = this.link_type_parser(await this.get_redir_url(element["id"]));
441
+ for (const element2 of typed_link) {
442
+ const final_info = await this.type_processer(element2);
443
+ if (final_info != null)
444
+ ret += final_info;
445
+ break;
446
+ }
447
+ break;
448
+ }
449
+ return ret;
450
+ }
451
+ /**
452
+ * 根据短链接重定向获取正常链接
453
+ * @param id 短链接 ID
454
+ * @returns 正常链接
455
+ */
456
+ async get_redir_url(id) {
457
+ var data = await this.ctx.http.get("https://b23.tv/" + id, {
458
+ redirect: "manual",
459
+ headers: {
460
+ "User-Agent": this.config.userAgent
461
+ }
462
+ });
463
+ const match = data.match(/<a\s+(?:[^>]*?\s+)?href="([^"]*)"/i);
464
+ if (match)
465
+ return match[1];
466
+ else
467
+ return null;
468
+ }
469
+ };
470
+
471
+ // src/index.ts
472
+ var logger = new import_koishi2.Logger("bilibili-videolink-analysis");
473
+ var name = "bilibili-videolink-analysis";
474
+ var inject = {
475
+ optional: ["puppeteer"]
476
+ // required: ['BiliBiliVideo']
477
+ };
478
+ var usage = `
479
+
480
+ <h2>→ <a href="https://www.npmjs.com/package/koishi-plugin-bilibili-videolink-analysis" target="_blank">可以点击这里查看详细的文档说明✨</a></h2>
481
+
482
+ 只需开启插件,就可以解析B站视频的链接啦~
483
+
484
+ 向bot发送B站视频链接吧~
485
+
486
+ 会返回视频信息与视频哦
487
+
488
+ ---
489
+
490
+ #### ⚠️ **如果你使用不了本项目,请优先检查:** ⚠️
491
+ #### 若无注册的指令,请关开一下[command插件](/market?keyword=commands+email:shigma10826@gmail.com)(没有指令也不影响解析别人的链接)
492
+ #### 视频内容是否为B站的大会员专属视频/付费视频/充电专属视频
493
+ #### 接入方法是否支持获取网址链接/小程序卡片消息
494
+ #### 接入方法是否支持视频元素的发送
495
+ #### 发送视频超时/其他网络问题
496
+ #### 视频内容被平台屏蔽/其他平台因素
497
+
498
+ ---
499
+
500
+ ### 注意,点播功能需要使用 puppeteer 服务
501
+
502
+ 点播功能是为了方便群友一起刷B站哦~
503
+
504
+ 比如:搜索 “遠い空へ” 的第二页,并且结果以语音格式返回
505
+
506
+ 示例:\`点播 遠い空へ -a -p 2\`
507
+
508
+
509
+ ---
510
+
511
+ ### 特别鸣谢 💖
512
+
513
+ 特别鸣谢以下项目的支持:
514
+
515
+ - [@summonhim/koishi-plugin-bili-parser](/market?keyword=bili-parser)
516
+
517
+ ---
518
+
519
+ `;
520
+ var Config = import_koishi2.Schema.intersect([
521
+ import_koishi2.Schema.object({
522
+ demand: import_koishi2.Schema.boolean().default(true).description("开启点播指令功能<br>`其实点播登录不登录 都搜不准,登录只是写着玩的`")
523
+ }).description("点播设置(需要puppeteer服务)"),
524
+ import_koishi2.Schema.union([
525
+ import_koishi2.Schema.object({
526
+ demand: import_koishi2.Schema.const(false).required()
527
+ }),
528
+ import_koishi2.Schema.object({
529
+ demand: import_koishi2.Schema.const(true),
530
+ timeout: import_koishi2.Schema.number().role("slider").min(1).max(300).step(1).default(60).description("指定播放视频的输入时限。`单位 秒`"),
531
+ point: import_koishi2.Schema.tuple([Number, Number]).description("序号标注位置。分别表示`距离顶部 距离左侧`的百分比").default([50, 50]),
532
+ enable: import_koishi2.Schema.boolean().description("是否开启自动解析`选择对应视频 会自动解析视频内容`").default(true)
533
+ })
534
+ ]),
535
+ import_koishi2.Schema.object({
536
+ enablebilianalysis: import_koishi2.Schema.boolean().default(true).description("开启解析功能<br>`关闭后,解析功能将关闭`")
537
+ }).description("视频解析 - 功能开关"),
538
+ import_koishi2.Schema.union([
539
+ import_koishi2.Schema.object({
540
+ enablebilianalysis: import_koishi2.Schema.const(true),
541
+ waitTip_Switch: import_koishi2.Schema.union([
542
+ import_koishi2.Schema.const(null).description("不返回文字提示"),
543
+ import_koishi2.Schema.string().description("返回文字提示(请在右侧填写文字内容)").default("正在解析B站链接...")
544
+ ]).description("是否返回等待提示。开启后,会发送`等待提示语`"),
545
+ videoParseMode: import_koishi2.Schema.array(import_koishi2.Schema.union([
546
+ import_koishi2.Schema.const("link").description("解析链接"),
547
+ import_koishi2.Schema.const("card").description("解析哔哩哔哩分享卡片")
548
+ ])).default(["link", "card"]).role("checkbox").description("选择解析来源"),
549
+ videoParseComponents: import_koishi2.Schema.array(import_koishi2.Schema.union([
550
+ import_koishi2.Schema.const("log").description("记录日志"),
551
+ import_koishi2.Schema.const("text").description("返回图文"),
552
+ import_koishi2.Schema.const("link").description("返回视频直链"),
553
+ import_koishi2.Schema.const("video").description("返回视频")
554
+ ])).default(["text", "video"]).role("checkbox").description("选择要返回的内容组件"),
555
+ BVnumberParsing: import_koishi2.Schema.boolean().default(true).description("是否允许根据`独立的BV、AV号`解析视频 `开启后,可以通过视频的BV、AV号解析视频。` <br> [触发说明见README](https://www.npmjs.com/package/koishi-plugin-bilibili-videolink-analysis)"),
556
+ MinimumTimeInterval: import_koishi2.Schema.number().default(180).description("若干`秒`内 不再处理相同链接 `防止多bot互相触发 导致的刷屏/性能浪费`").min(1),
557
+ Minimumduration: import_koishi2.Schema.number().default(0).description("允许解析的视频最小时长(分钟)`低于这个时长 就不会发视频内容`").min(0),
558
+ Minimumduration_tip: import_koishi2.Schema.union([
559
+ import_koishi2.Schema.const("return").description("不返回文字提示"),
560
+ import_koishi2.Schema.object({
561
+ tipcontent: import_koishi2.Schema.string().default("视频太短啦!不看不看~").description("文字提示内容"),
562
+ tipanalysis: import_koishi2.Schema.boolean().default(true).description("是否进行图文解析(不会返回视频链接)")
563
+ }).description("返回文字提示"),
564
+ import_koishi2.Schema.const(null)
565
+ ]).description("对`过短视频`的文字提示内容").default(null),
566
+ Maximumduration: import_koishi2.Schema.number().default(25).description("允许解析的视频最大时长(分钟)`超过这个时长 就不会发视频内容`").min(1),
567
+ Maximumduration_tip: import_koishi2.Schema.union([
568
+ import_koishi2.Schema.const("return").description("不返回文字提示"),
569
+ import_koishi2.Schema.object({
570
+ tipcontent: import_koishi2.Schema.string().default("视频太长啦!内容还是去B站看吧~").description("文字提示内容"),
571
+ tipanalysis: import_koishi2.Schema.boolean().default(true).description("是否进行图文解析(不会返回视频链接)")
572
+ }).description("返回文字提示"),
573
+ import_koishi2.Schema.const(null)
574
+ ]).description("对`过长视频`的文字提示内容").default(null),
575
+ parseLimit: import_koishi2.Schema.number().default(3).description("单对话多链接解析上限").hidden(),
576
+ useNumeral: import_koishi2.Schema.boolean().default(true).description("使用格式化数字").hidden(),
577
+ showError: import_koishi2.Schema.boolean().default(false).description("当链接不正确时提醒发送者").hidden(),
578
+ bVideoIDPreference: import_koishi2.Schema.union([
579
+ import_koishi2.Schema.const("bv").description("BV 号"),
580
+ import_koishi2.Schema.const("av").description("AV 号")
581
+ ]).default("bv").description("ID 偏好").hidden(),
582
+ bVideo_area: import_koishi2.Schema.string().role("textarea", { rows: [8, 16] }).default("${标题} ${tab} ${UP主}\n${简介}\n点赞:${点赞} ${tab} 投币:${投币}\n收藏:${收藏} ${tab} 转发:${转发}\n观看:${观看} ${tab} 弹幕:${弹幕}\n${~~~}\n${封面}").description(`图文解析的返回格式<br>
583
+ 注意变量格式,以及变量名称。<br>比如 \`\${标题}\` 不可以变成\`\${标题123}\`,你可以直接删掉但是不能修改变量名称哦<br>
584
+ 当然变量也不能无中生有,下面的默认值内容 就是所有变量了,你仅可以删去变量 或者修改变量之外的格式。<br>
585
+ · 特殊变量\`\${~~~}\`表示分割线,会把上下内容分为两个信息单独发送。\`\${tab}\`表示制表符。`),
586
+ bVideoShowLink: import_koishi2.Schema.boolean().default(false).description("在末尾显示视频的链接地址 `开启可能会导致其他bot循环解析`"),
587
+ bVideoShowIntroductionTofixed: import_koishi2.Schema.number().default(50).description("视频的`简介`最大的字符长度<br>超出部分会使用 `...` 代替"),
588
+ isfigure: import_koishi2.Schema.boolean().default(true).description("是否开启合并转发 `仅支持 onebot 适配器` 其他平台开启 无效").experimental(),
589
+ filebuffer: import_koishi2.Schema.boolean().default(true).description("是否将视频链接下载后再发送 (以解决部分onebot协议端的问题)<br>否则使用视频直链发送").experimental(),
590
+ middleware: import_koishi2.Schema.boolean().default(false).description("前置中间件模式"),
591
+ userAgent: import_koishi2.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")
592
+ }),
593
+ import_koishi2.Schema.object({
594
+ enablebilianalysis: import_koishi2.Schema.const(false).required()
595
+ })
596
+ ]),
597
+ import_koishi2.Schema.object({
598
+ pageclose: import_koishi2.Schema.boolean().default(true).description("自动`page.close()`<br>非开发者请勿改动").experimental(),
599
+ loggerinfo: import_koishi2.Schema.boolean().default(false).description("日志调试输出 `日常使用无需开启`<br>非开发者请勿改动").experimental(),
600
+ loggerinfofulljson: import_koishi2.Schema.boolean().default(false).description("打印完整的机器人发送的json输出").experimental()
601
+ }).description("开发者选项")
602
+ ]);
603
+ function apply(ctx, config) {
604
+ const bilibiliParser = new BilibiliParser(ctx, config, logger);
605
+ if (config.enablebilianalysis) {
606
+ ctx.middleware(async (session, next) => {
607
+ let isCard = false;
608
+ try {
609
+ if (session.stripped.content.startsWith("<json data=")) {
610
+ isCard = true;
611
+ }
612
+ } catch (e) {
613
+ }
614
+ if (isCard) {
615
+ if (!config.videoParseMode.includes("card")) {
616
+ return next();
617
+ }
618
+ } else {
619
+ if (!config.videoParseMode.includes("link")) {
620
+ return next();
621
+ }
622
+ }
623
+ let sessioncontent = session.stripped.content;
624
+ ctx.logger.info(sessioncontent);
625
+ if (config.BVnumberParsing) {
626
+ const bvUrls = bilibiliParser.convertBVToUrl(sessioncontent);
627
+ if (bvUrls.length > 0) {
628
+ sessioncontent += "\n" + bvUrls.join("\n");
629
+ }
630
+ }
631
+ const links = await bilibiliParser.isProcessLinks(sessioncontent);
632
+ if (links) {
633
+ const ret = await bilibiliParser.extractLinks(session, links);
634
+ if (ret && !bilibiliParser.isLinkProcessedRecently(ret, session.channelId)) {
635
+ await bilibiliParser.processVideoFromLink(session, ret);
636
+ }
637
+ }
638
+ return next();
639
+ }, config.middleware);
640
+ }
641
+ if (config.demand) {
642
+ 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) => {
643
+ if (!keyword) {
644
+ await session.send(import_koishi2.h.text("告诉我 你想要点播的关键词吧~"));
645
+ keyword = await session.prompt(30 * 1e3);
646
+ }
647
+ const url = `https://search.bilibili.com/video?keyword=${encodeURIComponent(keyword)}&page=${options.page}&o=30`;
648
+ const page = await ctx.puppeteer.page();
649
+ await page.goto(url, {
650
+ waitUntil: "networkidle2"
651
+ });
652
+ await page.addStyleTag({
653
+ content: `
654
+ div.bili-header,
655
+ div.login-tip,
656
+ div.v-popover,
657
+ div.right-entry__outside {
658
+ display: none !important;
659
+ }
660
+ `
661
+ });
662
+ const videos = await page.evaluate((point) => {
663
+ const items = Array.from(document.querySelectorAll('.video-list-item:not([style*="display: none"])'));
664
+ return items.map((item, index) => {
665
+ const link = item.querySelector("a");
666
+ const href = link?.getAttribute("href") || "";
667
+ const idMatch = href.match(/\/video\/(BV\w+)\//);
668
+ const id = idMatch ? idMatch[1] : "";
669
+ if (!id) {
670
+ const htmlElement = item;
671
+ htmlElement.style.display = "none";
672
+ } else {
673
+ const overlay = document.createElement("div");
674
+ overlay.style.position = "absolute";
675
+ overlay.style.top = `${point[0]}%`;
676
+ overlay.style.left = `${point[1]}%`;
677
+ overlay.style.transform = "translate(-50%, -50%)";
678
+ overlay.style.fontSize = "48px";
679
+ overlay.style.fontWeight = "bold";
680
+ overlay.style.color = "black";
681
+ overlay.style.zIndex = "10";
682
+ overlay.style.backgroundColor = "rgba(255, 255, 255, 0.7)";
683
+ overlay.style.padding = "10px";
684
+ overlay.style.borderRadius = "8px";
685
+ overlay.textContent = `${index + 1}`;
686
+ const videoElement = item;
687
+ videoElement.style.position = "relative";
688
+ videoElement.appendChild(overlay);
689
+ }
690
+ return { id };
691
+ }).filter((video) => video.id);
692
+ }, config.point);
693
+ bilibiliParser.logInfo(options);
694
+ bilibiliParser.logInfo(`共找到 ${videos.length} 个视频:`);
695
+ videos.forEach((video, index) => {
696
+ bilibiliParser.logInfo(`序号 ${index + 1}: ID - ${video.id}`);
697
+ });
698
+ if (videos.length === 0) {
699
+ await page.close();
700
+ await session.send(import_koishi2.h.text("未找到相关视频。"));
701
+ return;
702
+ }
703
+ const viewportHeight = 200 + videos.length * 100;
704
+ await page.setViewport({
705
+ width: 1440,
706
+ height: viewportHeight
707
+ });
708
+ bilibiliParser.logInfo("窗口:宽度:");
709
+ bilibiliParser.logInfo(1440);
710
+ bilibiliParser.logInfo("窗口:高度:");
711
+ bilibiliParser.logInfo(viewportHeight);
712
+ let msg;
713
+ const videoListElement = await page.$(".video-list.row");
714
+ if (videoListElement) {
715
+ const imgBuf = await videoListElement.screenshot({
716
+ captureBeyondViewport: false
717
+ });
718
+ msg = import_koishi2.h.image(imgBuf, "image/png");
719
+ }
720
+ if (page && config.pageclose) {
721
+ await page.close();
722
+ }
723
+ await session.send(msg + import_koishi2.h.text(`请选择视频的序号:`));
724
+ const userChoice = await session.prompt(config.timeout * 1e3);
725
+ const choiceIndex = parseInt(userChoice) - 1;
726
+ if (isNaN(choiceIndex) || choiceIndex < 0 || choiceIndex >= videos.length) {
727
+ await session.send(import_koishi2.h.text("输入无效,请输入正确的序号。"));
728
+ return;
729
+ }
730
+ const chosenVideo = videos[choiceIndex];
731
+ bilibiliParser.logInfo(`渲染序号设置
732
+ overlay.style.top = ${config.point[0]}%
733
+ overlay.style.left = ${config.point[1]}%`);
734
+ bilibiliParser.logInfo(`用户选择了序号 ${choiceIndex + 1}: ID - ${chosenVideo.id}`);
735
+ if (config.enable) {
736
+ const ret = await bilibiliParser.extractLinks(session, [{ type: "Video", id: chosenVideo.id }]);
737
+ if (ret && !bilibiliParser.isLinkProcessedRecently(ret, session.channelId)) {
738
+ await bilibiliParser.processVideoFromLink(session, ret, options);
739
+ }
740
+ }
741
+ });
742
+ }
743
+ }
744
+ __name(apply, "apply");
745
+ // Annotate the CommonJS export names for ESM import in node:
746
+ 0 && (module.exports = {
747
+ Config,
748
+ apply,
749
+ inject,
750
+ name,
751
+ usage
752
+ });