koishi-plugin-bilibili-notify 3.2.6-alpha.0 → 3.2.7-alpha.1

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
@@ -94412,6 +94412,11 @@ var BiliAPI = class extends koishi.Service {
94412
94412
  loginNotifier;
94413
94413
  refreshCookieTimer;
94414
94414
  loginInfoIsLoaded = false;
94415
+ wbiSign = {
94416
+ img_key: "",
94417
+ sub_key: ""
94418
+ };
94419
+ updateJob;
94415
94420
  constructor(ctx, config) {
94416
94421
  super(ctx, "ba");
94417
94422
  this.apiConfig = config;
@@ -94422,11 +94427,29 @@ var BiliAPI = class extends koishi.Service {
94422
94427
  this.cacheable.install(node_http.default.globalAgent);
94423
94428
  this.cacheable.install(node_https.default.globalAgent);
94424
94429
  await this.createNewClient();
94425
- this.loadCookiesFromDatabase();
94430
+ await this.loadCookiesFromDatabase();
94431
+ this.updateJob = new cron.CronJob("0 0 * * *", async () => {
94432
+ await this.updateBiliTicket();
94433
+ });
94434
+ this.updateJob.start();
94435
+ await this.updateBiliTicket();
94426
94436
  }
94427
94437
  stop() {
94428
94438
  this.cacheable.uninstall(node_http.default.globalAgent);
94429
94439
  this.cacheable.uninstall(node_https.default.globalAgent);
94440
+ this.updateJob.stop();
94441
+ }
94442
+ async updateBiliTicket() {
94443
+ try {
94444
+ const csrf = this.getCSRF();
94445
+ const ticket = await this.getBiliTicket(csrf);
94446
+ if (ticket.code !== 0) throw new Error(`获取BiliTicket失败: ${ticket.message}`);
94447
+ this.jar.setCookieSync(`bili_ticket=${ticket.data.ticket}; path=/; domain=.bilibili.com`, "https://www.bilibili.com");
94448
+ this.wbiSign.img_key = ticket.data.nav.img.slice(ticket.data.nav.img.lastIndexOf("/") + 1, ticket.data.nav.img.lastIndexOf("."));
94449
+ this.wbiSign.sub_key = ticket.data.nav.sub.slice(ticket.data.nav.sub.lastIndexOf("/") + 1, ticket.data.nav.sub.lastIndexOf("."));
94450
+ } catch (e$1) {
94451
+ this.logger.error(`更新BiliTicket失败: ${e$1.message}`);
94452
+ }
94430
94453
  }
94431
94454
  getMixinKey = (orig) => mixinKeyEncTab.map((n$2) => orig[n$2]).join("").slice(0, 32);
94432
94455
  encWbi(params, img_key, sub_key) {
@@ -94442,7 +94465,7 @@ var BiliAPI = class extends koishi.Service {
94442
94465
  return `${query}&w_rid=${wbi_sign}`;
94443
94466
  }
94444
94467
  async getWbi(params) {
94445
- const web_keys = await this.getWbiKeys();
94468
+ const web_keys = this.wbiSign || await this.getWbiKeys();
94446
94469
  const img_key = web_keys.img_key;
94447
94470
  const sub_key = web_keys.sub_key;
94448
94471
  const query = this.encWbi(params, img_key, sub_key);
@@ -94587,16 +94610,39 @@ var BiliAPI = class extends koishi.Service {
94587
94610
  disposeNotifier() {
94588
94611
  if (this.loginNotifier) this.loginNotifier.dispose();
94589
94612
  }
94590
- getRandomUserAgent() {
94591
- const userAgents = [
94592
- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
94593
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
94594
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
94595
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0",
94596
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
94597
- ];
94598
- const index = Math.floor(Math.random() * userAgents.length);
94599
- return userAgents[index];
94613
+ /**
94614
+ * Generate HMAC-SHA256 signature
94615
+ * @param {string} key The key string to use for the HMAC-SHA256 hash
94616
+ * @param {string} message The message string to hash
94617
+ * @returns {string} The HMAC-SHA256 signature as a hex string
94618
+ */
94619
+ hmacSha256(key, message$1) {
94620
+ const hmac = node_crypto.default.createHmac("sha256", key);
94621
+ hmac.update(message$1);
94622
+ return hmac.digest("hex");
94623
+ }
94624
+ /**
94625
+ * Get Bilibili web ticket
94626
+ * @param {string} csrf CSRF token, can be empty or null
94627
+ * @returns {Promise<any>} Promise of the ticket response in JSON format
94628
+ */
94629
+ async getBiliTicket(csrf) {
94630
+ const ts = Math.floor(Date.now() / 1e3);
94631
+ const hexSign = this.hmacSha256("XgwSnGZ1p", `ts${ts}`);
94632
+ const params = new URLSearchParams({
94633
+ key_id: "ec02",
94634
+ hexsign: hexSign,
94635
+ "context[ts]": ts.toString(),
94636
+ csrf: csrf || ""
94637
+ });
94638
+ const url = "https://api.bilibili.com/bapis/bilibili.api.ticket.v1.Ticket/GenWebTicket";
94639
+ const resp = await this.client.post(`${url}?${params.toString()}`, {}, { headers: {
94640
+ "Content-Type": "application/x-www-form-urlencoded",
94641
+ "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0"
94642
+ } }).catch((e$1) => {
94643
+ throw e$1;
94644
+ });
94645
+ return resp.data;
94600
94646
  }
94601
94647
  async createNewClient() {
94602
94648
  const wrapper = (await import("axios-cookiejar-support")).wrapper;
@@ -94605,7 +94651,7 @@ var BiliAPI = class extends koishi.Service {
94605
94651
  jar: this.jar,
94606
94652
  headers: {
94607
94653
  "Content-Type": "application/json",
94608
- "User-Agent": this.apiConfig.userAgent !== "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" ? this.apiConfig.userAgent : this.getRandomUserAgent(),
94654
+ "User-Agent": this.apiConfig.userAgent || "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0",
94609
94655
  Origin: "https://www.bilibili.com",
94610
94656
  Referer: "https://www.bilibili.com/"
94611
94657
  }
@@ -95113,7 +95159,7 @@ const Config = koishi.Schema.object({
95113
95159
  masterAccountGuildId: koishi.Schema.string().role("secret").description("主人账号所在的群组ID,只有在QQ频道、Discord这样的环境才需要填写,请使用inspect插件获取群组ID")
95114
95160
  }), koishi.Schema.object({})])]),
95115
95161
  basicSettings: koishi.Schema.object({}).description("基本设置"),
95116
- userAgent: koishi.Schema.string().required().description("设置请求头User-Agen,请求出现-352时可以尝试修改,UA获取方法可参考:https://blog.csdn.net/qq_44503987/article/details/104929111"),
95162
+ userAgent: koishi.Schema.string().description("设置请求头User-Agen,请求出现-352时可以尝试修改,UA获取方法可参考:https://blog.csdn.net/qq_44503987/article/details/104929111"),
95117
95163
  subTitle: koishi.Schema.object({}).description("订阅配置"),
95118
95164
  sub: koishi.Schema.array(koishi.Schema.object({
95119
95165
  name: koishi.Schema.string().description("订阅用户昵称,只是给你自己看的(相当于备注),可填可不填"),
package/lib/index.mjs CHANGED
@@ -94414,6 +94414,11 @@ var BiliAPI = class extends Service {
94414
94414
  loginNotifier;
94415
94415
  refreshCookieTimer;
94416
94416
  loginInfoIsLoaded = false;
94417
+ wbiSign = {
94418
+ img_key: "",
94419
+ sub_key: ""
94420
+ };
94421
+ updateJob;
94417
94422
  constructor(ctx, config) {
94418
94423
  super(ctx, "ba");
94419
94424
  this.apiConfig = config;
@@ -94424,11 +94429,29 @@ var BiliAPI = class extends Service {
94424
94429
  this.cacheable.install(http.globalAgent);
94425
94430
  this.cacheable.install(https.globalAgent);
94426
94431
  await this.createNewClient();
94427
- this.loadCookiesFromDatabase();
94432
+ await this.loadCookiesFromDatabase();
94433
+ this.updateJob = new CronJob("0 0 * * *", async () => {
94434
+ await this.updateBiliTicket();
94435
+ });
94436
+ this.updateJob.start();
94437
+ await this.updateBiliTicket();
94428
94438
  }
94429
94439
  stop() {
94430
94440
  this.cacheable.uninstall(http.globalAgent);
94431
94441
  this.cacheable.uninstall(https.globalAgent);
94442
+ this.updateJob.stop();
94443
+ }
94444
+ async updateBiliTicket() {
94445
+ try {
94446
+ const csrf = this.getCSRF();
94447
+ const ticket = await this.getBiliTicket(csrf);
94448
+ if (ticket.code !== 0) throw new Error(`获取BiliTicket失败: ${ticket.message}`);
94449
+ this.jar.setCookieSync(`bili_ticket=${ticket.data.ticket}; path=/; domain=.bilibili.com`, "https://www.bilibili.com");
94450
+ this.wbiSign.img_key = ticket.data.nav.img.slice(ticket.data.nav.img.lastIndexOf("/") + 1, ticket.data.nav.img.lastIndexOf("."));
94451
+ this.wbiSign.sub_key = ticket.data.nav.sub.slice(ticket.data.nav.sub.lastIndexOf("/") + 1, ticket.data.nav.sub.lastIndexOf("."));
94452
+ } catch (e$1) {
94453
+ this.logger.error(`更新BiliTicket失败: ${e$1.message}`);
94454
+ }
94432
94455
  }
94433
94456
  getMixinKey = (orig) => mixinKeyEncTab.map((n$2) => orig[n$2]).join("").slice(0, 32);
94434
94457
  encWbi(params, img_key, sub_key) {
@@ -94444,7 +94467,7 @@ var BiliAPI = class extends Service {
94444
94467
  return `${query}&w_rid=${wbi_sign}`;
94445
94468
  }
94446
94469
  async getWbi(params) {
94447
- const web_keys = await this.getWbiKeys();
94470
+ const web_keys = this.wbiSign || await this.getWbiKeys();
94448
94471
  const img_key = web_keys.img_key;
94449
94472
  const sub_key = web_keys.sub_key;
94450
94473
  const query = this.encWbi(params, img_key, sub_key);
@@ -94589,16 +94612,39 @@ var BiliAPI = class extends Service {
94589
94612
  disposeNotifier() {
94590
94613
  if (this.loginNotifier) this.loginNotifier.dispose();
94591
94614
  }
94592
- getRandomUserAgent() {
94593
- const userAgents = [
94594
- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
94595
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
94596
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
94597
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0",
94598
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
94599
- ];
94600
- const index = Math.floor(Math.random() * userAgents.length);
94601
- return userAgents[index];
94615
+ /**
94616
+ * Generate HMAC-SHA256 signature
94617
+ * @param {string} key The key string to use for the HMAC-SHA256 hash
94618
+ * @param {string} message The message string to hash
94619
+ * @returns {string} The HMAC-SHA256 signature as a hex string
94620
+ */
94621
+ hmacSha256(key, message$1) {
94622
+ const hmac = crypto.createHmac("sha256", key);
94623
+ hmac.update(message$1);
94624
+ return hmac.digest("hex");
94625
+ }
94626
+ /**
94627
+ * Get Bilibili web ticket
94628
+ * @param {string} csrf CSRF token, can be empty or null
94629
+ * @returns {Promise<any>} Promise of the ticket response in JSON format
94630
+ */
94631
+ async getBiliTicket(csrf) {
94632
+ const ts = Math.floor(Date.now() / 1e3);
94633
+ const hexSign = this.hmacSha256("XgwSnGZ1p", `ts${ts}`);
94634
+ const params = new URLSearchParams({
94635
+ key_id: "ec02",
94636
+ hexsign: hexSign,
94637
+ "context[ts]": ts.toString(),
94638
+ csrf: csrf || ""
94639
+ });
94640
+ const url = "https://api.bilibili.com/bapis/bilibili.api.ticket.v1.Ticket/GenWebTicket";
94641
+ const resp = await this.client.post(`${url}?${params.toString()}`, {}, { headers: {
94642
+ "Content-Type": "application/x-www-form-urlencoded",
94643
+ "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0"
94644
+ } }).catch((e$1) => {
94645
+ throw e$1;
94646
+ });
94647
+ return resp.data;
94602
94648
  }
94603
94649
  async createNewClient() {
94604
94650
  const wrapper = (await import("axios-cookiejar-support")).wrapper;
@@ -94607,7 +94653,7 @@ var BiliAPI = class extends Service {
94607
94653
  jar: this.jar,
94608
94654
  headers: {
94609
94655
  "Content-Type": "application/json",
94610
- "User-Agent": this.apiConfig.userAgent !== "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" ? this.apiConfig.userAgent : this.getRandomUserAgent(),
94656
+ "User-Agent": this.apiConfig.userAgent || "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0",
94611
94657
  Origin: "https://www.bilibili.com",
94612
94658
  Referer: "https://www.bilibili.com/"
94613
94659
  }
@@ -95115,7 +95161,7 @@ const Config = Schema.object({
95115
95161
  masterAccountGuildId: Schema.string().role("secret").description("主人账号所在的群组ID,只有在QQ频道、Discord这样的环境才需要填写,请使用inspect插件获取群组ID")
95116
95162
  }), Schema.object({})])]),
95117
95163
  basicSettings: Schema.object({}).description("基本设置"),
95118
- userAgent: Schema.string().required().description("设置请求头User-Agen,请求出现-352时可以尝试修改,UA获取方法可参考:https://blog.csdn.net/qq_44503987/article/details/104929111"),
95164
+ userAgent: Schema.string().description("设置请求头User-Agen,请求出现-352时可以尝试修改,UA获取方法可参考:https://blog.csdn.net/qq_44503987/article/details/104929111"),
95119
95165
  subTitle: Schema.object({}).description("订阅配置"),
95120
95166
  sub: Schema.array(Schema.object({
95121
95167
  name: Schema.string().description("订阅用户昵称,只是给你自己看的(相当于备注),可填可不填"),
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": "3.2.6-alpha.0",
4
+ "version": "3.2.7-alpha.1",
5
5
  "contributors": [
6
6
  "Akokko <admin@akokko.com>"
7
7
  ],
package/readme.md CHANGED
@@ -277,26 +277,24 @@ uid为必填参数,为要推送的UP主的UID,index为可选参数,为要
277
277
  > - ver 3.2.3 优化:移除不必要的代码;
278
278
  > - ver 3.2.4-alpha.0 优化:选项 `pushImgsInDynamic` 发送多图会以转发消息的格式发> 送; 新增:选项 `dynamicVideoUrlToBV` 开启后将链接转换为bv号以用作特殊用途;
279
279
  > - ver 3.2.4 修复:第一次使用插件时,使用登录指令报错; 插件配置页新增和删除提示信息;
280
- >
281
- > > [!WARNING]
282
- > > `3.2.5-alpha.x` 目前均为测试版本,请不要更新🙅
283
- > > - ver 3.2.5-alpha.0 优化:新增 `DNS` 缓存,以减少DNS错误;
284
- > > - ver 3.2.5-alpha.1 测试版本
285
- > > - ver 3.2.5-alpha.2 测试版本
286
- > > - ver 3.2.5-alpha.3 更新依赖版本
287
- > > - ver 3.2.5-alpha.4 测试版本
288
- > > - ver 3.2.5-alpha.5 测试版本
289
- > > - ver 3.2.5-alpha.6 测试版本
290
- > > - ver 3.2.5-alpha.7 测试版本
291
- > > - ver 3.2.5-alpha.8 测试版本
292
- > > - ver 3.2.5-alpha.9 测试版本
293
- > > - ver 3.2.5-alpha.10 测试版本
294
- > > - ver 3.2.5-alpha.11 测试版本
295
- > > - ver 3.2.5-alpha.12 测试版本
296
- > > - ver 3.2.5-alpha.13 测试版本
297
- >
280
+ > - ver 3.2.5-alpha.0 优化:新增 `DNS` 缓存,以减少DNS错误;
281
+ > - ver 3.2.5-alpha.1 测试版本
282
+ > - ver 3.2.5-alpha.2 测试版本
283
+ > - ver 3.2.5-alpha.3 更新依赖版本
284
+ > - ver 3.2.5-alpha.4 测试版本
285
+ > - ver 3.2.5-alpha.5 测试版本
286
+ > - ver 3.2.5-alpha.6 测试版本
287
+ > - ver 3.2.5-alpha.7 测试版本
288
+ > - ver 3.2.5-alpha.8 测试版本
289
+ > - ver 3.2.5-alpha.9 测试版本
290
+ > - ver 3.2.5-alpha.10 测试版本
291
+ > - ver 3.2.5-alpha.11 测试版本
292
+ > - ver 3.2.5-alpha.12 测试版本
293
+ > - ver 3.2.5-alpha.13 测试版本
298
294
  > - ver 3.2.5 重构:消息推送逻辑; 新增:选项 `dynamicCron`;
299
295
  > - ver 3.2.6-alpha.0 优化:更新依赖; 修复:长图动态向下箭头无法显示的bug;
296
+ > - ver 3.2.7-alpha.0 优化:将选项 `userAgent` 更改为可选项,在 `cookies` 中添加 `bili_ticket` 降低风控概率;
297
+ > - ver 3.2.7-alpha.1 修复:加载 `bili_ticket` 失败会导致插件加载失败;
300
298
 
301
299
  ## 交流群
302
300