koishi-plugin-bilibili-notify 1.0.0-rc.1 → 1.0.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/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Akokko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/lib/biliAPI.js CHANGED
@@ -136,8 +136,21 @@ class BiliAPI extends koishi_1.Service {
136
136
  // 没有数据则直接返回
137
137
  if (data === undefined)
138
138
  return;
139
- // 解密数据
140
- const decryptedCookies = this.ctx.wbi.decrypt(data.bili_cookies);
139
+ // 定义解密信息
140
+ let decryptedCookies;
141
+ let decryptedRefreshToken;
142
+ try {
143
+ // 解密数据
144
+ decryptedCookies = this.ctx.wbi.decrypt(data.bili_cookies);
145
+ // 解密refresh_token
146
+ decryptedRefreshToken = this.ctx.wbi.decrypt(data.bili_refresh_token);
147
+ }
148
+ catch (e) {
149
+ // 解密失败,删除数据库登录信息
150
+ await this.ctx.database.remove('loginBili', [1]);
151
+ // 直接返回
152
+ return;
153
+ }
141
154
  // 解析从数据库读到的cookies
142
155
  const cookies = JSON.parse(decryptedCookies);
143
156
  // 定义CSRF Token
@@ -160,8 +173,6 @@ class BiliAPI extends koishi_1.Service {
160
173
  });
161
174
  this.jar.setCookieSync(cookie, `http${cookie.secure ? 's' : ''}://${cookie.domain}${cookie.path}`, {});
162
175
  });
163
- // 解密refresh_token
164
- const decryptedRefreshToken = this.ctx.wbi.decrypt(data.bili_refresh_token);
165
176
  // Check if token need refresh
166
177
  this.checkIfTokenNeedRefresh(decryptedRefreshToken, csrf);
167
178
  }
@@ -23,13 +23,13 @@ declare class ComRegister {
23
23
  getSubFromDatabase(ctx: Context): Promise<void>;
24
24
  unsubSingle(ctx: Context, id: string, type: number): string;
25
25
  checkIfIsLogin(ctx: Context): Promise<boolean>;
26
- test_dynamicDetect(ctx: Context, session: Session, uid: string): () => Promise<void>;
27
26
  }
28
27
  declare namespace ComRegister {
29
28
  interface Config {
30
29
  pushTime: number;
31
30
  liveLoopTime: number;
32
31
  dynamicLoopTime: number;
32
+ dynamicCheckNumber: number;
33
33
  }
34
34
  const Config: Schema<Config>;
35
35
  }
@@ -534,8 +534,8 @@ class ComRegister {
534
534
  }
535
535
  // 获取数据内容
536
536
  const items = content.data.items;
537
- // 发送请求 只查看前五条数据
538
- for (let num = 4; num >= 0; num--) {
537
+ // 发送请求 默认只查看前五条数据
538
+ for (let num = this.config.dynamicCheckNumber - 1; num >= 0; num--) {
539
539
  // 没有动态内容则直接跳过
540
540
  if (!items[num])
541
541
  continue;
@@ -849,82 +849,13 @@ class ComRegister {
849
849
  }
850
850
  return false;
851
851
  }
852
- test_dynamicDetect(ctx, session, uid) {
853
- let firstSubscription = true;
854
- let timePoint;
855
- // Test code
856
- let timer = 0;
857
- return async () => {
858
- // Test code
859
- console.log('timer:' + timer++);
860
- console.log('firstSubscription:' + firstSubscription);
861
- console.log(`timePoint: ${timePoint}`);
862
- console.log(`timePoint: ${ctx.gimg.unixTimestampToString(timePoint)}`);
863
- // 第一次订阅判断
864
- if (firstSubscription) {
865
- // 设置第一次的时间点
866
- timePoint = Math.floor(Date.now() / 1000);
867
- // 设置第一次为false
868
- firstSubscription = false;
869
- return;
870
- }
871
- // 获取用户空间动态数据
872
- let content;
873
- try {
874
- content = await ctx.biliAPI.getUserSpaceDynamic(uid);
875
- }
876
- catch (e) {
877
- return this.logger.error('dynamicDetect getUserSpaceDynamic() 网络请求失败');
878
- }
879
- // 判断是否出现其他问题
880
- if (content.code !== 0) {
881
- switch (content.code) {
882
- case -101: { // 账号未登录
883
- await session.send('账号未登录,请登录后重新订阅动态');
884
- }
885
- default: { // 未知错误
886
- await session.send('未知错误,请重新订阅动态');
887
- }
888
- }
889
- // 取消订阅
890
- this.unsubSingle(ctx, uid, 1); /* 1为取消动态订阅 */
891
- return;
892
- }
893
- // 获取数据内容
894
- const items = content.data.items;
895
- // 发送请求 只查看前五条数据
896
- for (let num = 4; num >= 0; num--) {
897
- // 没有动态内容则直接跳过
898
- if (!items[num])
899
- continue;
900
- // Test code
901
- console.log(`items[${num}].modules.module_author.pub_ts: ${ctx.gimg.unixTimestampToString(items[num].modules.module_author.pub_ts)}`);
902
- // 寻找发布时间比时间点时间更晚的动态
903
- if (items[num].modules.module_author.pub_ts > timePoint) {
904
- // 如果这是遍历的最后一条,将时间点设置为这条动态的发布时间
905
- /* if (num === 1) timePoint = items[num].modules.module_author.pub_ts
906
- if (num === 0) {
907
- timePoint = items[num].modules.module_author.pub_ts
908
- } */
909
- switch (num) {
910
- // 如果是置顶动态,则跳过
911
- case 0: if (items[num].modules.module_tag)
912
- continue;
913
- case 1: timePoint = items[num].modules.module_author.pub_ts;
914
- }
915
- // 推送该条动态
916
- const [pic] = await ctx.gimg.generateDynamicImg(items[num]);
917
- await session.send(pic);
918
- }
919
- }
920
- };
921
- }
922
852
  }
923
853
  (function (ComRegister) {
924
854
  ComRegister.Config = koishi_1.Schema.object({
925
855
  pushTime: koishi_1.Schema.number().required(),
926
856
  liveLoopTime: koishi_1.Schema.number().default(10),
927
- dynamicLoopTime: koishi_1.Schema.number().default(60)
857
+ dynamicLoopTime: koishi_1.Schema.number().default(60),
858
+ dynamicCheckNumber: koishi_1.Schema.number().required()
928
859
  });
929
860
  })(ComRegister || (ComRegister = {}));
930
861
  exports.default = ComRegister;
package/lib/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { Context, Schema } from 'koishi';
2
+ export declare const inject: string[];
2
3
  export declare const name = "bilibili-notify";
3
4
  export interface Config {
4
5
  pushTime: number;
6
+ dynamicCheckNumber: number;
5
7
  cardColorStart: string;
6
8
  cardColorEnd: string;
7
9
  key: string;
package/lib/index.js CHANGED
@@ -26,10 +26,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.apply = exports.Config = exports.name = void 0;
29
+ exports.apply = exports.Config = exports.name = exports.inject = void 0;
30
30
  const koishi_1 = require("koishi");
31
- // import crypto
32
- // import crypto from 'crypto'
33
31
  // import plugins
34
32
  // import Authority from './authority'
35
33
  const comRegister_1 = __importDefault(require("./comRegister"));
@@ -38,6 +36,7 @@ const Database = __importStar(require("./database"));
38
36
  const wbi_1 = __importDefault(require("./wbi"));
39
37
  const generateImg_1 = __importDefault(require("./generateImg"));
40
38
  const biliAPI_1 = __importDefault(require("./biliAPI"));
39
+ exports.inject = ['puppeteer', 'database', 'notifier'];
41
40
  exports.name = 'bilibili-notify';
42
41
  exports.Config = koishi_1.Schema.object({
43
42
  pushTime: koishi_1.Schema.number()
@@ -46,6 +45,13 @@ exports.Config = koishi_1.Schema.object({
46
45
  .step(0.5)
47
46
  .default(1)
48
47
  .description('设定隔多长时间推送一次直播状态,单位为小时,默认为一小时'),
48
+ dynamicCheckNumber: koishi_1.Schema.number()
49
+ .min(2)
50
+ .max(10)
51
+ .role('slider')
52
+ .step(1)
53
+ .default(5)
54
+ .description('设定每次检查动态的数量。若订阅的UP主经常在短时间内连着发多条动态可以将该值提高,若订阅的UP主有置顶动态,在计算该值时应-1。默认值为5条'),
49
55
  cardColorStart: koishi_1.Schema.string()
50
56
  .pattern(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
51
57
  .default('#F38AB5')
@@ -61,6 +67,9 @@ exports.Config = koishi_1.Schema.object({
61
67
  .description('请输入一个32位小写字母的十六进制密钥(例如:9b8db7ae562b9864efefe06289cc5530),使用此密钥将你的B站登录信息存储在数据库中,请一定保存好此密钥。如果你忘记了此密钥,必须重新登录。你可以自行生成,或到这个网站生成:https://www.sexauth.com/')
62
68
  });
63
69
  function apply(ctx, config) {
70
+ ctx.notifier.create({
71
+ content: '请记得使用Auth插件创建超级管理员账号,没有权限将无法使用该插件提供的指令。'
72
+ });
64
73
  // load database
65
74
  ctx.plugin(Database);
66
75
  // Regist server
@@ -69,7 +78,7 @@ function apply(ctx, config) {
69
78
  ctx.plugin(biliAPI_1.default);
70
79
  // load plugin
71
80
  // ctx.plugin(Authority)
72
- ctx.plugin(comRegister_1.default, { pushTime: config.pushTime });
81
+ ctx.plugin(comRegister_1.default, { pushTime: config.pushTime, dynamicCheckNumber: config.dynamicCheckNumber });
73
82
  // 当用户输入“恶魔兔,启动!”时,执行 help 指令
74
83
  ctx.middleware((session, next) => {
75
84
  if (session.content === '恶魔兔,启动!') {
@@ -81,6 +90,3 @@ function apply(ctx, config) {
81
90
  });
82
91
  }
83
92
  exports.apply = apply;
84
- /* function generateKey(): string {
85
- return crypto.randomBytes(32).toString('hex');
86
- } */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-bilibili-notify",
3
3
  "description": "Koishi bilibili notify plugin",
4
- "version": "1.0.0-rc.1",
4
+ "version": "1.0.0",
5
5
  "contributors": [
6
6
  "Akokko <admin@akokko.com>"
7
7
  ],
@@ -16,6 +16,7 @@
16
16
  "type": "git",
17
17
  "url": "git+https://github.com/Akokk0/koishi-plugin-bilibili-notify"
18
18
  },
19
+ "homepage": "https://www.npmjs.com/package/koishi-plugin-bilibili-notify",
19
20
  "scripts": {},
20
21
  "keywords": [
21
22
  "chatbot",
@@ -36,22 +37,16 @@
36
37
  "devDependencies": {
37
38
  "koishi-plugin-puppeteer": "^3.7.3"
38
39
  },
39
- "service": {
40
- "required": [
41
- "puppeteer",
42
- "database"
43
- ]
44
- },
45
40
  "koishi": {
46
- "preview": true,
47
41
  "service": {
48
42
  "required": [
49
43
  "database",
50
- "puppeteer"
44
+ "puppeteer",
45
+ "notifier"
51
46
  ]
52
47
  },
53
48
  "description": {
54
- "zh": "开发中的Bilibili直播通知,动态推送插件。目前还只是个雏形,不建议使用,发布做测试用"
49
+ "zh": "Bilibili动态推送,直播通知插件。具体使用方法请参考readme.md"
55
50
  },
56
51
  "locales": [
57
52
  "zh"
package/readme.md CHANGED
@@ -1,15 +1,80 @@
1
- # koishi-plugin-bilibili-notify
1
+ <div align="center" style="font-size:45px;">
2
+ Bilibili-Notify
3
+ </div>
2
4
 
3
- [![npm](https://img.shields.io/npm/v/koishi-plugin-bilibili-notify?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-bilibili-notify)
5
+ 基于 [koishi](../../../../koishijs/koishi) 框架的B站推送插件
4
6
 
5
- 此插件依赖于"database"和"puppeteer"服务,同时受权限控制,需要具备authority:3及以上的权限才能使用本插件提供的指令,你可以参考下面链接中的方法得到一个超级管理员账号(具有authority:5的最高权限)
6
- https://koishi.chat/zh-CN/manual/usage/platform.html#%E9%85%8D%E7%BD%AE%E7%99%BB%E5%BD%95%E6%8F%92%E4%BB%B6
7
+ ---
7
8
 
8
- 您还可以安装admin插件,给其他用户授予权限,操作方法请参考下面的链接
9
- https://koishi.chat/zh-CN/manual/usage/customize.html
9
+ - **koishi-plugin-bilibili-notify** [![npm](https://img.shields.io/npm/v/koishi-plugin-bilibili-notify?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-bilibili-notify)
10
+ - [功能](#功能)
11
+ - [安装](#安装)
12
+ - [使用方法](#使用方法)
13
+ - [注意事项](#注意事项)
14
+ - [感谢](#感谢)
15
+ - [License](#License)
10
16
 
11
- 指令使用方法请参考 help bili,子命令使用方法请加-h,例如bili login -h
17
+ ## 功能
12
18
 
13
- 本插件功能均建立在B站账号登录之上,所有操作之前请先登录
19
+ 订阅B站UP主动态
14
20
 
15
- 登录方式为二维码,输入命令bili login之后扫码登录,您的登录凭证将存储在您的本地数据库,并由您自己填写的密钥加密,所以请保管好你的密钥
21
+ 订阅B站UP主直播
22
+
23
+ ## 安装
24
+
25
+ 1. 下载插件运行平台 [Koishi](https://koishi.chat/)
26
+ 2. 在插件平台的 **`插件市场`** 中搜索 **`bilibili-notify`** 并安装
27
+
28
+ ## 使用方法
29
+
30
+ **登录B站:**进行任何操作前,请先登录B站
31
+
32
+ - 使用指令 `bili login` 获取登录二维码,使用B站扫码登录
33
+
34
+ **订阅UP主:**订阅你想要推送的UP主
35
+
36
+ - 使用指令 `bili sub <uid>` 订阅需要订阅的UP主
37
+ - 参数说明:`uid` 为必填参数,为 `up主` 的 `uid`
38
+ - 选项说明:`bili sub <uid>` 有两个选项:-l 和 -d
39
+ - `-l` 为订阅UP主直播间,包括直播开播通知,定时推送直播内容,下播通知
40
+ - `-d` 为订阅UP主动态推送,目前实现推送的动态类型有:普通图文动态,转发动态,直播预约动态
41
+
42
+ - 例如:
43
+ - `bili sub 1194210119 ` 订阅UID为1194210119的UP主
44
+ - `bili sub 1194210119 -d` 订阅UID为1194210119的UP主动态推送
45
+ - `bili sub 1194210119 -ld` 订阅UID为1194210119的UP主动态推送和直播间
46
+ - Tips:
47
+ - 除非使用指令 `bili sub 1194210119 -ld` ,没有加选项或只加了一个选项的指令都会再次询问是否需要订阅另一项。例如:使用指令 `bili sub 1194210119 -d` 机器人会询问是否需要订阅直播间
48
+
49
+ **取消订阅UP主:**取消订阅不需要推送的UP主
50
+
51
+ - 使用指令 `bili unsub <uid>` 取消订阅不需要订阅的UP主
52
+ - 参数说明:`uid` 为必填参数,为 `up主` 的 `uid`
53
+ - 选项说明:`bili unsub <uid>` 有两个选项:-l 和 -d
54
+ - `-l` 为取消订阅UP主直播间
55
+ - `-d` 为取消订阅UP主动态
56
+ - 例如:
57
+ - `bili unsub 123456` 取消订阅UID为123456的UP主动态推送和直播间
58
+ - `bili unsub 123456 -d` 取消订阅UID为123456的UP主动态推送
59
+ - `bili unsub 123456 -dl` 取消订阅UID为123456的UP主动态推送和直播间
60
+
61
+ ## 注意事项
62
+
63
+ 1. 此插件依赖于 `database` 和 `puppeteer` 服务,同时受权限控制,需要具备 `authority:3` 及以上的权限才能使用本插件提供的指令,你可以参考下方配置登录插件中的方法得到一个超级管理员账号(具有 `authority:5` 的最高权限)
64
+
65
+ [配置登录插件](https://koishi.chat/zh-CN/manual/usage/platform.html#%E9%85%8D%E7%BD%AE%E7%99%BB%E5%BD%95%E6%8F%92%E4%BB%B6)
66
+
67
+ 2. 您还可以安装 `admin` 插件,给其他用户授予权限,操作方法请参考下方的权限管理
68
+
69
+ [权限管理](https://koishi.chat/zh-CN/manual/usage/customize.html)
70
+
71
+ 3. 指令使用方法请参考 `help bili`,子命令使用方法请加 `-h` ,例如 `bili login -h`
72
+ 4. 登录方式为二维码,输入命令 `bili login` 之后扫码登录,您的登录凭证将存储在您的本地数据库,并由您自己填写的密钥加密,所以请保管好你的密钥
73
+
74
+ ## 感谢
75
+
76
+ 感谢 [koishijs](https://github.com/koishijs/koishi) 官方提供的插件开发框架, 以及技术指导
77
+
78
+ ## License
79
+
80
+ MIT