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/utils.d.ts ADDED
@@ -0,0 +1,63 @@
1
+ import { Logger, Context, Session } from "koishi";
2
+ import type { Config } from './index';
3
+ export declare class BilibiliParser {
4
+ private ctx;
5
+ private config;
6
+ private logger;
7
+ private lastProcessedUrls;
8
+ constructor(ctx: Context, config: Config, logger: Logger);
9
+ logInfo(...args: any[]): void;
10
+ isProcessLinks(sessioncontent: string): Promise<false | {
11
+ type: string;
12
+ id: string;
13
+ }[]>;
14
+ extractLinks(session: Session, links: {
15
+ type: string;
16
+ id: string;
17
+ }[]): Promise<string>;
18
+ isLinkProcessedRecently(ret: string, channelId: string): boolean;
19
+ processVideoFromLink(session: Session, ret: string, options?: {
20
+ video?: boolean;
21
+ audio?: boolean;
22
+ link?: boolean;
23
+ }): Promise<void>;
24
+ private extractLastUrl;
25
+ convertBVToUrl(text: string): string[];
26
+ private numeral;
27
+ /**
28
+ * 解析 ID 类型
29
+ * @param id 视频 ID
30
+ * @returns type: ID 类型, id: 视频 ID
31
+ */
32
+ private vid_type_parse;
33
+ /**
34
+ * 根据视频 ID 查找视频信息
35
+ * @param id 视频 ID
36
+ * @returns 视频信息 Json
37
+ */
38
+ private fetch_video_info;
39
+ /**
40
+ * 生成视频信息
41
+ * @param id 视频 ID
42
+ * @returns 文字视频信息
43
+ */
44
+ private gen_context;
45
+ /**
46
+ * 链接类型解析
47
+ * @param content 传入消息
48
+ * @returns type: "链接类型", id :"内容ID"
49
+ */
50
+ private link_type_parser;
51
+ /**
52
+ * 类型执行器
53
+ * @param element 链接列表
54
+ * @returns 解析来的文本
55
+ */
56
+ private type_processer;
57
+ /**
58
+ * 根据短链接重定向获取正常链接
59
+ * @param id 短链接 ID
60
+ * @returns 正常链接
61
+ */
62
+ private get_redir_url;
63
+ }
package/package.json CHANGED
@@ -2,12 +2,12 @@
2
2
  "name": "koishi-plugin-bilibili-videolink-analysis",
3
3
  "description": "[<ruby>Bilibili视频解析<rp>(</rp><rt>点我查看食用方法</rt><rp>)</rp></ruby>](https://www.npmjs.com/package/koishi-plugin-bilibili-videolink-analysis)解析B站链接(支持小程序卡片)支持搜索点播功能!灵感来自完美的 [bili-parser](/market?keyword=bili-parser) !",
4
4
  "license": "MIT",
5
- "version": "1.1.25",
5
+ "version": "1.3.0",
6
6
  "main": "lib/index.js",
7
7
  "typings": "lib/index.d.ts",
8
8
  "files": [
9
9
  "lib",
10
- "dist"
10
+ "src"
11
11
  ],
12
12
  "homepage": "https://github.com/shangxueink/koishi-shangxue-apps/tree/main/",
13
13
  "bugs": {
@@ -17,8 +17,7 @@
17
17
  "chatbot",
18
18
  "koishi",
19
19
  "plugin",
20
- "B站视频点播",
21
- "Bilibili视频链接解析",
20
+ "B站视频点播解析",
22
21
  "bilibili-videolink-analysis"
23
22
  ],
24
23
  "peerDependencies": {
package/src/index.ts ADDED
@@ -0,0 +1,355 @@
1
+ import { Schema, Logger, h, Context, Session } from "koishi";
2
+ import { } from "koishi-plugin-puppeteer";
3
+ import { BilibiliParser } from "./utils";
4
+
5
+ const logger = new Logger('bilibili-videolink-analysis');
6
+
7
+ export const name = 'bilibili-videolink-analysis';
8
+ export const inject = {
9
+ optional: ['puppeteer'],
10
+ // required: ['BiliBiliVideo']
11
+ }
12
+ export const usage = `
13
+
14
+ <h2>→ <a href="https://www.npmjs.com/package/koishi-plugin-bilibili-videolink-analysis" target="_blank">可以点击这里查看详细的文档说明✨</a></h2>
15
+
16
+ ✨ 只需开启插件,就可以解析B站视频的链接啦~ ✨
17
+
18
+ 向bot发送B站视频链接吧~
19
+
20
+ 会返回视频信息与视频哦
21
+
22
+ ---
23
+
24
+ #### ⚠️ **如果你使用不了本项目,请优先检查:** ⚠️
25
+ #### 若无注册的指令,请关开一下[command插件](/market?keyword=commands+email:shigma10826@gmail.com)(没有指令也不影响解析别人的链接)
26
+ #### 视频内容是否为B站的大会员专属视频/付费视频/充电专属视频
27
+ #### 接入方法是否支持获取网址链接/小程序卡片消息
28
+ #### 接入方法是否支持视频元素的发送
29
+ #### 发送视频超时/其他网络问题
30
+ #### 视频内容被平台屏蔽/其他平台因素
31
+
32
+ ---
33
+
34
+ ### 注意,点播功能需要使用 puppeteer 服务
35
+
36
+ 点播功能是为了方便群友一起刷B站哦~
37
+
38
+ 比如:搜索 “遠い空へ” 的第二页,并且结果以语音格式返回
39
+
40
+ 示例:\`点播 遠い空へ -a -p 2\`
41
+
42
+
43
+ ---
44
+
45
+ ### 特别鸣谢 💖
46
+
47
+ 特别鸣谢以下项目的支持:
48
+
49
+ - [@summonhim/koishi-plugin-bili-parser](/market?keyword=bili-parser)
50
+
51
+ ---
52
+
53
+ `;
54
+
55
+ export interface Config {
56
+ demand: boolean;
57
+ timeout?: number;
58
+ point?: [number, number];
59
+ enable?: boolean;
60
+ enablebilianalysis: boolean;
61
+ videoParseMode: string[];
62
+ waitTip_Switch?: string | null;
63
+ videoParseComponents: string[];
64
+ BVnumberParsing: boolean;
65
+ MinimumTimeInterval: number;
66
+ Minimumduration: number;
67
+ Minimumduration_tip: 'return' | { tipcontent: string; tipanalysis: boolean } | null;
68
+ Maximumduration: number;
69
+ Maximumduration_tip: 'return' | { tipcontent: string; tipanalysis: boolean } | null;
70
+ parseLimit: number;
71
+ useNumeral: boolean;
72
+ showError: boolean;
73
+ bVideoIDPreference: "bv" | "av";
74
+ bVideo_area: string;
75
+ bVideoShowLink: boolean;
76
+ bVideoShowIntroductionTofixed: number;
77
+ isfigure: boolean;
78
+ filebuffer: boolean;
79
+ middleware: boolean;
80
+ userAgent: string;
81
+ pageclose: boolean;
82
+ loggerinfo: boolean;
83
+ loggerinfofulljson: boolean;
84
+ }
85
+
86
+ export const Config = Schema.intersect([
87
+ Schema.object({
88
+ demand: Schema.boolean().default(true).description("开启点播指令功能<br>`其实点播登录不登录 都搜不准,登录只是写着玩的`"),
89
+ }).description('点播设置(需要puppeteer服务)'),
90
+ Schema.union([
91
+ Schema.object({
92
+ demand: Schema.const(false).required(),
93
+ }),
94
+ Schema.object({
95
+ demand: Schema.const(true),
96
+ timeout: Schema.number().role('slider').min(1).max(300).step(1).default(60).description('指定播放视频的输入时限。`单位 秒`'),
97
+ point: Schema.tuple([Number, Number]).description('序号标注位置。分别表示`距离顶部 距离左侧`的百分比').default([50, 50]),
98
+ enable: Schema.boolean().description('是否开启自动解析`选择对应视频 会自动解析视频内容`').default(true),
99
+ }),
100
+ ]),
101
+
102
+ Schema.object({
103
+ enablebilianalysis: Schema.boolean().default(true).description("开启解析功能<br>`关闭后,解析功能将关闭`"),
104
+ }).description('视频解析 - 功能开关'),
105
+ Schema.union([
106
+ Schema.object({
107
+ enablebilianalysis: Schema.const(true),
108
+ waitTip_Switch: Schema.union([
109
+ Schema.const(null).description('不返回文字提示'),
110
+ Schema.string().description('返回文字提示(请在右侧填写文字内容)').default('正在解析B站链接...'),
111
+ ]).description("是否返回等待提示。开启后,会发送`等待提示语`"),
112
+ videoParseMode: Schema.array(Schema.union([
113
+ Schema.const('link').description('解析链接'),
114
+ Schema.const('card').description('解析哔哩哔哩分享卡片'),
115
+ ]))
116
+ .default(['link', 'card'])
117
+ .role('checkbox')
118
+ .description('选择解析来源'),
119
+ videoParseComponents: Schema.array(Schema.union([
120
+ Schema.const('log').description('记录日志'),
121
+ Schema.const('text').description('返回图文'),
122
+ Schema.const('link').description('返回视频直链'),
123
+ Schema.const('video').description('返回视频'),
124
+ ]))
125
+ .default(['text', 'video'])
126
+ .role('checkbox')
127
+ .description('选择要返回的内容组件'),
128
+ BVnumberParsing: Schema.boolean().default(true).description("是否允许根据`独立的BV、AV号`解析视频 `开启后,可以通过视频的BV、AV号解析视频。` <br> [触发说明见README](https://www.npmjs.com/package/koishi-plugin-bilibili-videolink-analysis)"),
129
+ MinimumTimeInterval: Schema.number().default(180).description("若干`秒`内 不再处理相同链接 `防止多bot互相触发 导致的刷屏/性能浪费`").min(1),
130
+ Minimumduration: Schema.number().default(0).description("允许解析的视频最小时长(分钟)`低于这个时长 就不会发视频内容`").min(0),
131
+ Minimumduration_tip: Schema.union([
132
+ Schema.const('return').description('不返回文字提示'),
133
+ Schema.object({
134
+ tipcontent: Schema.string().default('视频太短啦!不看不看~').description("文字提示内容"),
135
+ tipanalysis: Schema.boolean().default(true).description("是否进行图文解析(不会返回视频链接)"),
136
+ }).description('返回文字提示'),
137
+ Schema.const(null),
138
+ ]).description("对`过短视频`的文字提示内容").default(null),
139
+ Maximumduration: Schema.number().default(25).description("允许解析的视频最大时长(分钟)`超过这个时长 就不会发视频内容`").min(1),
140
+ Maximumduration_tip: Schema.union([
141
+ Schema.const('return').description('不返回文字提示'),
142
+ Schema.object({
143
+ tipcontent: Schema.string().default('视频太长啦!内容还是去B站看吧~').description("文字提示内容"),
144
+ tipanalysis: Schema.boolean().default(true).description("是否进行图文解析(不会返回视频链接)"),
145
+ }).description('返回文字提示'),
146
+ Schema.const(null),
147
+ ]).description("对`过长视频`的文字提示内容").default(null),
148
+ parseLimit: Schema.number().default(3).description("单对话多链接解析上限").hidden(),
149
+ useNumeral: Schema.boolean().default(true).description("使用格式化数字").hidden(),
150
+ showError: Schema.boolean().default(false).description("当链接不正确时提醒发送者").hidden(),
151
+ bVideoIDPreference: Schema.union([
152
+ Schema.const("bv").description("BV 号"),
153
+ Schema.const("av").description("AV 号"),
154
+ ]).default("bv").description("ID 偏好").hidden(),
155
+ bVideo_area: Schema.string().role('textarea', { rows: [8, 16] })
156
+ .default("${标题} ${tab} ${UP主}\n${简介}\n点赞:${点赞} ${tab} 投币:${投币}\n收藏:${收藏} ${tab} 转发:${转发}\n观看:${观看} ${tab} 弹幕:${弹幕}\n${~~~}\n${封面}")
157
+ .description(`图文解析的返回格式<br>
158
+ 注意变量格式,以及变量名称。<br>比如 \`\${标题}\` 不可以变成\`\${标题123}\`,你可以直接删掉但是不能修改变量名称哦<br>
159
+ 当然变量也不能无中生有,下面的默认值内容 就是所有变量了,你仅可以删去变量 或者修改变量之外的格式。<br>
160
+ · 特殊变量\`\${~~~}\`表示分割线,会把上下内容分为两个信息单独发送。\`\${tab}\`表示制表符。`),
161
+ bVideoShowLink: Schema.boolean().default(false).description("在末尾显示视频的链接地址 `开启可能会导致其他bot循环解析`"),
162
+ bVideoShowIntroductionTofixed: Schema.number().default(50).description("视频的`简介`最大的字符长度<br>超出部分会使用 `...` 代替"),
163
+ isfigure: Schema.boolean().default(true).description("是否开启合并转发 `仅支持 onebot 适配器` 其他平台开启 无效").experimental(),
164
+ filebuffer: Schema.boolean().default(true).description("是否将视频链接下载后再发送 (以解决部分onebot协议端的问题)<br>否则使用视频直链发送").experimental(),
165
+ middleware: Schema.boolean().default(false).description("前置中间件模式"),
166
+ 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"),
167
+ }),
168
+ Schema.object({
169
+ enablebilianalysis: Schema.const(false).required(),
170
+ }),
171
+ ]),
172
+
173
+ Schema.object({
174
+ pageclose: Schema.boolean().default(true).description("自动`page.close()`<br>非开发者请勿改动").experimental(),
175
+ loggerinfo: Schema.boolean().default(false).description("日志调试输出 `日常使用无需开启`<br>非开发者请勿改动").experimental(),
176
+ loggerinfofulljson: Schema.boolean().default(false).description("打印完整的机器人发送的json输出").experimental(),
177
+ }).description("开发者选项"),
178
+ ]);
179
+
180
+ export function apply(ctx: Context, config: Config) {
181
+ const bilibiliParser = new BilibiliParser(ctx, config, logger);
182
+
183
+ if (config.enablebilianalysis) {
184
+ ctx.middleware(async (session, next) => {
185
+ // 尝试解析JSON卡片
186
+ let isCard = false;
187
+ try {
188
+ if (session.stripped.content.startsWith('<json data=')) {
189
+ isCard = true;
190
+ }
191
+ } catch (e) {
192
+ // Not a valid JSON card
193
+ }
194
+
195
+ if (isCard) {
196
+ if (!config.videoParseMode.includes('card')) {
197
+ return next();
198
+ }
199
+ } else {
200
+ if (!config.videoParseMode.includes('link')) {
201
+ return next();
202
+ }
203
+ }
204
+
205
+ let sessioncontent = session.stripped.content;
206
+ ctx.logger.info(sessioncontent)
207
+ if (config.BVnumberParsing) {
208
+ const bvUrls = bilibiliParser.convertBVToUrl(sessioncontent);
209
+ if (bvUrls.length > 0) {
210
+ sessioncontent += '\n' + bvUrls.join('\n');
211
+ }
212
+ }
213
+ const links = await bilibiliParser.isProcessLinks(sessioncontent); // 判断是否需要解析
214
+ if (links) {
215
+ const ret = await bilibiliParser.extractLinks(session, links); // 提取链接
216
+ if (ret && !bilibiliParser.isLinkProcessedRecently(ret, session.channelId)) {
217
+ await bilibiliParser.processVideoFromLink(session, ret); // 解析视频并返回
218
+ }
219
+ }
220
+ return next();
221
+ }, config.middleware);
222
+ }
223
+
224
+ if (config.demand) {
225
+ ctx.command('B站点播 [keyword]', '点播B站视频')
226
+ .option('video', '-v 解析返回视频')
227
+ .option('audio', '-a 解析返回语音')
228
+ .option('link', '-l 解析返回链接')
229
+ .option('page', '-p <page:number> 指定页数', { fallback: '1' })
230
+ .example('B站点播 遠い空へ -v')
231
+ .action(async ({ options, session }, keyword) => {
232
+ if (!keyword) {
233
+ await session.send(h.text('告诉我 你想要点播的关键词吧~'))
234
+ keyword = await session.prompt(30 * 1000)
235
+ }
236
+ const url = `https://search.bilibili.com/video?keyword=${encodeURIComponent(keyword)}&page=${options.page}&o=30`
237
+ const page = await ctx.puppeteer.page()
238
+
239
+ await page.goto(url, {
240
+ waitUntil: 'networkidle2'
241
+ })
242
+
243
+ await page.addStyleTag({
244
+ content: `
245
+ div.bili-header,
246
+ div.login-tip,
247
+ div.v-popover,
248
+ div.right-entry__outside {
249
+ display: none !important;
250
+ }
251
+ `
252
+ })
253
+ // 获取视频列表并为每个视频元素添加序号
254
+ const videos = await page.evaluate((point: [number, number]) => {
255
+ const items = Array.from(document.querySelectorAll('.video-list-item:not([style*="display: none"])'))
256
+ return items.map((item, index) => {
257
+ const link = item.querySelector('a')
258
+ const href = link?.getAttribute('href') || ''
259
+ const idMatch = href.match(/\/video\/(BV\w+)\//)
260
+ const id = idMatch ? idMatch[1] : ''
261
+
262
+ if (!id) {
263
+ // 如果没有提取到视频ID,隐藏这个元素
264
+ const htmlElement = item as HTMLElement
265
+ htmlElement.style.display = 'none'
266
+ } else {
267
+ // 创建一个包含序号的元素,并将其插入到视频元素的正中央
268
+ const overlay = document.createElement('div')
269
+ overlay.style.position = 'absolute'
270
+ overlay.style.top = `${point[0]}%`
271
+ overlay.style.left = `${point[1]}%`
272
+ overlay.style.transform = 'translate(-50%, -50%)'
273
+ overlay.style.fontSize = '48px'
274
+ overlay.style.fontWeight = 'bold'
275
+ overlay.style.color = 'black'
276
+ overlay.style.zIndex = '10'
277
+ overlay.style.backgroundColor = 'rgba(255, 255, 255, 0.7)' // 半透明白色背景,确保数字清晰可见
278
+ overlay.style.padding = '10px'
279
+ overlay.style.borderRadius = '8px'
280
+ overlay.textContent = `${index + 1}` // 序号
281
+
282
+ // 确保父元素有 `position: relative` 以正确定位
283
+ const videoElement = item as HTMLElement
284
+ videoElement.style.position = 'relative'
285
+ videoElement.appendChild(overlay)
286
+ }
287
+
288
+ return { id }
289
+ }).filter(video => video.id)
290
+ }, config.point) // 传递配置的 point 参数
291
+
292
+ bilibiliParser.logInfo(options)
293
+ bilibiliParser.logInfo(`共找到 ${videos.length} 个视频:`)
294
+ videos.forEach((video: any, index: number) => {
295
+ bilibiliParser.logInfo(`序号 ${index + 1}: ID - ${video.id}`)
296
+ })
297
+
298
+
299
+ if (videos.length === 0) {
300
+ await page.close()
301
+ await session.send(h.text('未找到相关视频。'))
302
+ return
303
+ }
304
+
305
+ // 动态调整窗口大小以适应视频数量
306
+ const viewportHeight = 200 + videos.length * 100
307
+ await page.setViewport({
308
+ width: 1440,
309
+ height: viewportHeight
310
+ })
311
+ bilibiliParser.logInfo("窗口:宽度:")
312
+ bilibiliParser.logInfo(1440)
313
+
314
+ bilibiliParser.logInfo("窗口:高度:")
315
+ bilibiliParser.logInfo(viewportHeight)
316
+ let msg: any;
317
+
318
+ // 截图
319
+ const videoListElement = await page.$('.video-list.row')
320
+ if (videoListElement) {
321
+ const imgBuf = await videoListElement.screenshot({
322
+ captureBeyondViewport: false
323
+ }) as Buffer
324
+ msg = h.image(imgBuf, 'image/png')
325
+ }
326
+ if (page && config.pageclose) {
327
+ await page.close()
328
+ }
329
+
330
+ // 发送截图
331
+ await session.send(msg + h.text(`请选择视频的序号:`))
332
+ // 等待用户输入
333
+ const userChoice = await session.prompt(config.timeout * 1000)
334
+ const choiceIndex = parseInt(userChoice) - 1
335
+ if (isNaN(choiceIndex) || choiceIndex < 0 || choiceIndex >= videos.length) {
336
+ await session.send(h.text('输入无效,请输入正确的序号。'))
337
+ return
338
+ }
339
+
340
+ // 返回用户选择的视频ID
341
+ const chosenVideo = videos[choiceIndex]
342
+
343
+ bilibiliParser.logInfo(`渲染序号设置\noverlay.style.top = ${config.point[0]}% \noverlay.style.left = ${config.point[1]}%`)
344
+ bilibiliParser.logInfo(`用户选择了序号 ${choiceIndex + 1}: ID - ${chosenVideo.id}`)
345
+
346
+ // 开启自动解析了
347
+ if (config.enable) {
348
+ const ret = await bilibiliParser.extractLinks(session, [{ type: 'Video', id: chosenVideo.id }]); // 提取链接
349
+ if (ret && !bilibiliParser.isLinkProcessedRecently(ret, session.channelId)) {
350
+ await bilibiliParser.processVideoFromLink(session, ret, options); // 解析视频并返回
351
+ }
352
+ }
353
+ })
354
+ }
355
+ }