koishi-plugin-history-of-today 0.0.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.d.ts +10 -0
- package/lib/index.js +134 -0
- package/package.json +24 -0
- package/readme.md +5 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Context, Schema } from 'koishi';
|
|
2
|
+
export declare const name = "history-of-today";
|
|
3
|
+
export interface Config {
|
|
4
|
+
count: number;
|
|
5
|
+
enableSchedule: boolean;
|
|
6
|
+
scheduleTime: string;
|
|
7
|
+
whitelist: string[];
|
|
8
|
+
}
|
|
9
|
+
export declare const Config: Schema<Config>;
|
|
10
|
+
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name2 in all)
|
|
10
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
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
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
Config: () => Config,
|
|
34
|
+
apply: () => apply,
|
|
35
|
+
name: () => name
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(src_exports);
|
|
38
|
+
var import_koishi = require("koishi");
|
|
39
|
+
var import_axios = __toESM(require("axios"));
|
|
40
|
+
var cheerio = __toESM(require("cheerio"));
|
|
41
|
+
var name = "history-of-today";
|
|
42
|
+
var Config = import_koishi.Schema.object({
|
|
43
|
+
count: import_koishi.Schema.number().min(1).max(50).default(20).description("返回历史事件的数量"),
|
|
44
|
+
enableSchedule: import_koishi.Schema.boolean().default(false).description("是否启用定时推送"),
|
|
45
|
+
scheduleTime: import_koishi.Schema.string().default("09:00").description("定时推送时间 (HH:MM 格式,默认 09:00)"),
|
|
46
|
+
whitelist: import_koishi.Schema.array(import_koishi.Schema.string()).default([]).description("白名单群聊 ID 列表")
|
|
47
|
+
});
|
|
48
|
+
function apply(ctx, config) {
|
|
49
|
+
async function fetchHistory() {
|
|
50
|
+
const response = await import_axios.default.get("https://tophub.today/n/KMZd7X3erO", {
|
|
51
|
+
headers: {
|
|
52
|
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
|
|
53
|
+
},
|
|
54
|
+
timeout: 1e4
|
|
55
|
+
});
|
|
56
|
+
const $ = cheerio.load(response.data);
|
|
57
|
+
const items = [];
|
|
58
|
+
$("tbody tr").each((_, row) => {
|
|
59
|
+
const cells = $(row).find("td");
|
|
60
|
+
if (cells.length >= 2) {
|
|
61
|
+
const eventText = $(cells[1]).text().trim();
|
|
62
|
+
const match = eventText.match(/^(\d{4})年-(.+)$/);
|
|
63
|
+
if (match) {
|
|
64
|
+
items.push({
|
|
65
|
+
year: match[1],
|
|
66
|
+
event: match[2]
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return items.slice(0, config.count);
|
|
72
|
+
}
|
|
73
|
+
__name(fetchHistory, "fetchHistory");
|
|
74
|
+
ctx.command("历史上的今天 [count:number]", "查询历史上的今天").alias("hot").alias("history").action(async ({ session }, count) => {
|
|
75
|
+
const limit = count ? Math.min(Math.max(1, count), config.count) : config.count;
|
|
76
|
+
await session.send("正在查询...");
|
|
77
|
+
try {
|
|
78
|
+
const items = await fetchHistory();
|
|
79
|
+
if (items.length === 0) {
|
|
80
|
+
return "暂时没有获取到历史数据。";
|
|
81
|
+
}
|
|
82
|
+
const today = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
|
|
83
|
+
return [
|
|
84
|
+
`📅 历史上的今天 (${today})`,
|
|
85
|
+
"",
|
|
86
|
+
...items.map((item, index) => `${index + 1}. ${item.year}年 ${item.event}`)
|
|
87
|
+
].join("\n");
|
|
88
|
+
} catch (error) {
|
|
89
|
+
ctx.logger.error("获取历史数据失败:", error);
|
|
90
|
+
return "获取历史数据失败,请稍后再试。";
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
let lastRunDate = "";
|
|
94
|
+
if (config.enableSchedule && config.whitelist.length > 0) {
|
|
95
|
+
ctx.setInterval(async () => {
|
|
96
|
+
const now = /* @__PURE__ */ new Date();
|
|
97
|
+
const currentTime = `${String(now.getHours()).padStart(2, "0")}:${String(now.getMinutes()).padStart(2, "0")}`;
|
|
98
|
+
const todayDate = now.toISOString().split("T")[0];
|
|
99
|
+
if (currentTime === config.scheduleTime && lastRunDate !== todayDate) {
|
|
100
|
+
try {
|
|
101
|
+
const items = await fetchHistory();
|
|
102
|
+
if (items.length === 0) {
|
|
103
|
+
ctx.logger.warn("定时推送失败: 未获取到历史数据");
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const message = [
|
|
107
|
+
`📅 历史上的今天 (${todayDate})`,
|
|
108
|
+
"",
|
|
109
|
+
...items.map((item, index) => `${index + 1}. ${item.year}年 ${item.event}`)
|
|
110
|
+
].join("\n");
|
|
111
|
+
for (const groupId of config.whitelist) {
|
|
112
|
+
try {
|
|
113
|
+
await ctx.broadcast([groupId], message);
|
|
114
|
+
ctx.logger.info(`已向群 ${groupId} 推送历史上的今天`);
|
|
115
|
+
} catch (err) {
|
|
116
|
+
ctx.logger.error(`向群 ${groupId} 推送失败:`, err);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
lastRunDate = todayDate;
|
|
120
|
+
} catch (error) {
|
|
121
|
+
ctx.logger.error("定时推送历史数据失败:", error);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}, 6e4);
|
|
125
|
+
ctx.logger.info(`定时推送已启用: 每天 ${config.scheduleTime}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
__name(apply, "apply");
|
|
129
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
130
|
+
0 && (module.exports = {
|
|
131
|
+
Config,
|
|
132
|
+
apply,
|
|
133
|
+
name
|
|
134
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "koishi-plugin-history-of-today",
|
|
3
|
+
"description": "历史上的今天",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"typings": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib",
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"chatbot",
|
|
14
|
+
"koishi",
|
|
15
|
+
"plugin"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"axios": "^1.6.0",
|
|
19
|
+
"cheerio": "^1.0.0"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"koishi": "4.18.10"
|
|
23
|
+
}
|
|
24
|
+
}
|