koishi-plugin-nmc-radar 1.2.0 → 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.d.ts CHANGED
@@ -1,7 +1,14 @@
1
- import type { Context } from 'koishi';
2
- import { Schema } from 'koishi';
1
+ import type { Awaitable, Context } from 'koishi';
2
+ import { h, Schema } from 'koishi';
3
3
  export declare const name = "nmc-radar";
4
+ export declare const inject: {
5
+ optional: string[];
6
+ };
7
+ type Resolver = (urls: string[], id: string) => Awaitable<h>;
8
+ declare const resolvers: Record<string, Resolver>;
4
9
  export interface Config {
10
+ defaultResolver: keyof typeof resolvers;
5
11
  }
6
12
  export declare const Config: Schema<Config>;
7
- export declare function apply(ctx: Context): void;
13
+ export declare function apply(ctx: Context, config: Config): void;
14
+ export {};
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
@@ -22,9 +32,12 @@ var src_exports = {};
22
32
  __export(src_exports, {
23
33
  Config: () => Config,
24
34
  apply: () => apply,
35
+ inject: () => inject,
25
36
  name: () => name
26
37
  });
27
38
  module.exports = __toCommonJS(src_exports);
39
+ var import_promises = require("node:fs/promises");
40
+ var import_node_path = __toESM(require("node:path"));
28
41
  var import_jsdom = require("jsdom");
29
42
  var import_koishi = require("koishi");
30
43
 
@@ -308,21 +321,52 @@ var radars_default = {
308
321
 
309
322
  // src/index.ts
310
323
  var name = "nmc-radar";
311
- var Config = import_koishi.Schema.object({});
312
- function apply(ctx) {
313
- ctx.command("radar <name:string>", "查看雷达图").alias("雷达").action(async ({ session }, name2) => {
324
+ var inject = {
325
+ optional: ["ffmpeg"]
326
+ };
327
+ var resolvers = {
328
+ img: /* @__PURE__ */ __name((urls) => import_koishi.h.img(urls[0]), "img"),
329
+ imgs: /* @__PURE__ */ __name((urls) => (0, import_koishi.h)(import_koishi.h.Fragment, ...urls.map((url) => import_koishi.h.img(url))), "imgs"),
330
+ url: /* @__PURE__ */ __name((urls) => import_koishi.h.text(urls[0]), "url"),
331
+ urls: /* @__PURE__ */ __name((urls) => import_koishi.h.text(urls.join("\n")), "urls")
332
+ };
333
+ var Config = import_koishi.Schema.object({
334
+ defaultResolver: import_koishi.Schema.union(Object.keys(resolvers)).default("img").description("默认输出类型。")
335
+ });
336
+ function apply(ctx, config) {
337
+ const command = ctx.command("radar <name:string>", "查看雷达图", { checkUnknown: true }).alias("雷达").option("type", "--type <type:string> 输出类型", { type: Object.keys(resolvers) }).option("type", "--img 输出单张图片", { value: "img" }).option("type", "--imgs 输出多张图片", { value: "imgs" }).option("type", "--url 输出 URL", { value: "url" }).option("type", "--urls 输出 URL 列表", { value: "urls" }).action(async ({ session, options }, name2) => {
338
+ if (!(name2 in radars_default))
339
+ return void session?.send("雷达站不存在,可使用 radar.list 查看所有雷达站。");
314
340
  const url = radars_default[name2];
315
- if (!url)
316
- return void session?.send("该雷达图不存在");
317
- const dom = new import_jsdom.JSDOM(await ctx.http.get(url));
318
- const image = dom.window.document.querySelector("div[data-img]");
319
- return (0, import_koishi.h)("img", { src: image?.dataset.img || "" });
341
+ const { window: { document } } = new import_jsdom.JSDOM(await ctx.http.get(url));
342
+ const nodes = document.querySelectorAll("div[data-img]");
343
+ const urls = Array.from(nodes).map((node) => node.dataset.img || "");
344
+ const resolver = resolvers[options?.type || config.defaultResolver];
345
+ return await resolver(urls, import_koishi.Random.id());
346
+ });
347
+ ctx.inject(["ffmpeg"], async (ctx2) => {
348
+ command.option("type", "--gif 输出 GIF 动画", { value: "gif" });
349
+ resolvers.gif = async (urls, id) => {
350
+ const baseDir = import_node_path.default.join(ctx2.baseDir, "temp", name, id);
351
+ await (0, import_promises.mkdir)(baseDir, { recursive: true });
352
+ await Promise.all(urls.map(async (url, i) => {
353
+ const response = await ctx2.http.get(url, { responseType: "stream" });
354
+ const filename = `${String(i + 1).padStart(3, "0")}.png`;
355
+ const filePath = import_node_path.default.join(baseDir, filename);
356
+ await (0, import_promises.writeFile)(filePath, response);
357
+ }));
358
+ const outputPath = import_node_path.default.join(baseDir, "..", `${id}.gif`);
359
+ await ctx2.ffmpeg.builder().input(import_node_path.default.join(baseDir, "%03d.png")).run("file", outputPath);
360
+ return import_koishi.h.img(`file:///${outputPath}`);
361
+ };
320
362
  });
363
+ command.subcommand(".list", "查看所有雷达站").action(() => Object.keys(radars_default).join(" "));
321
364
  }
322
365
  __name(apply, "apply");
323
366
  // Annotate the CommonJS export names for ESM import in node:
324
367
  0 && (module.exports = {
325
368
  Config,
326
369
  apply,
370
+ inject,
327
371
  name
328
372
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-nmc-radar",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "气象台雷达图插件",
5
5
  "typings": "lib/index.d.ts",
6
6
  "license": "MIT",
@@ -22,20 +22,16 @@
22
22
  "koishi": "4.18.11"
23
23
  },
24
24
  "dependencies": {
25
- "@zorner/gifencoder": "^1.0.1",
26
- "jsdom": "^29.1.1",
27
- "probe-image-size": "^7.3.0"
25
+ "jsdom": "^29.1.1"
28
26
  },
29
27
  "devDependencies": {
30
- "@koishijs/canvas": "^0.2.0",
31
- "@types/gifencoder": "^2.0.3",
32
28
  "@types/jsdom": "^28.0.3",
33
- "@types/probe-image-size": "^7"
29
+ "koishi-plugin-ffmpeg": "^1.1.0"
34
30
  },
35
31
  "koishi": {
36
32
  "service": {
37
33
  "optional": [
38
- "canvas"
34
+ "ffmpeg"
39
35
  ]
40
36
  }
41
37
  }