koishi-plugin-xlon 1.1.0 → 1.1.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/config.d.ts CHANGED
@@ -22,5 +22,7 @@ export interface Config {
22
22
  subscriptions: SubscriptionItem[];
23
23
  outputLogs: boolean;
24
24
  detectXLinks: boolean;
25
+ /** 抓取失败时是否保存网页截图(保存至 {baseDir}/data/xlon/debug) */
26
+ debugScreenshot: boolean;
25
27
  }
26
28
  export declare const Config: Schema<Schemastery.ObjectS<{}>, {} & import("cosmokit").Dict>;
package/lib/index.js CHANGED
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
8
  var __export = (target, all) => {
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
@@ -447,8 +457,42 @@ function createPoller(ctx, config, repo, fetcher, deps) {
447
457
  __name(createPoller, "createPoller");
448
458
 
449
459
  // src/service/fetcher.ts
460
+ var fs = __toESM(require("fs"));
461
+ var path = __toESM(require("path"));
450
462
  var import_koishi7 = require("koishi");
451
463
  var logger7 = new import_koishi7.Logger("xlon/fetcher");
464
+ var DEBUG_SCREENSHOT_MAX = 3;
465
+ async function saveFailureScreenshot(page, username, attempt, dir) {
466
+ if (!dir || !page) return;
467
+ try {
468
+ await fs.promises.mkdir(dir, { recursive: true });
469
+ const ts = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
470
+ const filename = `xlon-fail-${username}-attempt${attempt}-${ts}.png`;
471
+ const filepath = path.join(dir, filename);
472
+ const buf = await page.screenshot({ type: "png", fullPage: true });
473
+ await fs.promises.writeFile(filepath, buf);
474
+ logger7.info(`[debug] 已保存失败截图: ${filepath}`);
475
+ const entries = await fs.promises.readdir(dir);
476
+ const debugFiles = entries.filter(
477
+ (f) => f.startsWith("xlon-fail-") && f.endsWith(".png")
478
+ );
479
+ if (debugFiles.length > DEBUG_SCREENSHOT_MAX) {
480
+ const withStat = await Promise.all(
481
+ debugFiles.map(async (f) => ({
482
+ name: f,
483
+ mtime: (await fs.promises.stat(path.join(dir, f))).mtimeMs
484
+ }))
485
+ );
486
+ withStat.sort((a, b) => a.mtime - b.mtime).slice(0, debugFiles.length - DEBUG_SCREENSHOT_MAX).forEach(
487
+ ({ name: name2 }) => fs.promises.unlink(path.join(dir, name2)).catch(() => {
488
+ })
489
+ );
490
+ }
491
+ } catch (err) {
492
+ logger7.warn(`[debug] 保存失败截图时出错: ${err}`);
493
+ }
494
+ }
495
+ __name(saveFailureScreenshot, "saveFailureScreenshot");
452
496
  function toAbsoluteUrl(input) {
453
497
  const raw = (input || "").trim();
454
498
  if (!raw) return raw;
@@ -667,6 +711,10 @@ var XFetcher = class {
667
711
  return result;
668
712
  } catch (error) {
669
713
  logger7.error(`抓取最新推文失败(第 ${attempt} 次): ${username}`, error);
714
+ if (this.config.debugScreenshot && page) {
715
+ const debugDir = path.join(this.ctx.baseDir, "data", "xlon", "debug");
716
+ await saveFailureScreenshot(page, username, attempt, debugDir);
717
+ }
670
718
  if (attempt >= maxRetries) return { tweets: [], wordContent: "" };
671
719
  await this.ctx.sleep(2e3 * attempt);
672
720
  } finally {
@@ -805,7 +853,8 @@ var Config = import_koishi8.Schema.intersect([
805
853
  }).description("订阅列表"),
806
854
  import_koishi8.Schema.object({
807
855
  outputLogs: import_koishi8.Schema.boolean().default(true),
808
- detectXLinks: import_koishi8.Schema.boolean().default(true)
856
+ detectXLinks: import_koishi8.Schema.boolean().default(true),
857
+ debugScreenshot: import_koishi8.Schema.boolean().default(false).description("抓取失败时保存网页截图(保存至 {baseDir}/data/xlon/debug,保留最近 3 张)")
809
858
  }).description("调试设置")
810
859
  ]).i18n({
811
860
  "en-US": en_US_default._config,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-xlon",
3
3
  "description": "x subscriber",
4
- "version": "1.1.0",
4
+ "version": "1.1.1",
5
5
  "contributors": [
6
6
  "Logthm <logthm@outlook.com>"
7
7
  ],