koishi-plugin-aka-60s-api 0.1.8 → 0.2.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 +1 -0
- package/lib/index.js +41 -64
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -31,6 +31,7 @@ var name = "aka-60s-api";
|
|
|
31
31
|
var inject = ["database"];
|
|
32
32
|
var Config = import_koishi.Schema.intersect([
|
|
33
33
|
import_koishi.Schema.object({
|
|
34
|
+
apiBaseUrl: import_koishi.Schema.string().default("http://172.0.0.1:4399").description("60s 服务 URL(不含 /v2 路径)"),
|
|
34
35
|
cooldownTime: import_koishi.Schema.number().default(30).min(5).max(300).description("冷却时间(秒)"),
|
|
35
36
|
enableLog: import_koishi.Schema.boolean().default(true).description("启用日志记录"),
|
|
36
37
|
scheduleWhitelist: import_koishi.Schema.array(String).default([]).description("定时发送群组白名单频道ID列表(为空=不发送)")
|
|
@@ -61,6 +62,8 @@ var Config = import_koishi.Schema.intersect([
|
|
|
61
62
|
]);
|
|
62
63
|
function apply(ctx, config) {
|
|
63
64
|
const logger = ctx.logger("aka-60s-api");
|
|
65
|
+
const normalizedApiBaseUrl = (config.apiBaseUrl || "http://172.0.0.1:4399").replace(/\/$/, "");
|
|
66
|
+
const buildApiUrl = /* @__PURE__ */ __name((path) => `${normalizedApiBaseUrl}${path}`, "buildApiUrl");
|
|
64
67
|
const cooldowns = /* @__PURE__ */ new Map();
|
|
65
68
|
let scheduleInterval = null;
|
|
66
69
|
let aiNewsScheduleInterval = null;
|
|
@@ -117,7 +120,7 @@ function apply(ctx, config) {
|
|
|
117
120
|
async function get60sNewsImage() {
|
|
118
121
|
try {
|
|
119
122
|
logInfo("60s API: 开始获取新闻图片");
|
|
120
|
-
const response = await ctx.http.get("
|
|
123
|
+
const response = await ctx.http.get(buildApiUrl("/v2/60s"), {
|
|
121
124
|
params: {
|
|
122
125
|
encoding: "image"
|
|
123
126
|
},
|
|
@@ -138,7 +141,7 @@ function apply(ctx, config) {
|
|
|
138
141
|
async function getTodayInHistory() {
|
|
139
142
|
try {
|
|
140
143
|
logInfo("60s API: 开始获取历史上的今天");
|
|
141
|
-
const response = await ctx.http.get("
|
|
144
|
+
const response = await ctx.http.get(buildApiUrl("/v2/today-in-history"), {
|
|
142
145
|
params: {
|
|
143
146
|
encoding: "json"
|
|
144
147
|
},
|
|
@@ -159,7 +162,7 @@ function apply(ctx, config) {
|
|
|
159
162
|
async function getZhihuTrends() {
|
|
160
163
|
try {
|
|
161
164
|
logInfo("60s API: 开始获取知乎话题榜");
|
|
162
|
-
const response = await ctx.http.get("
|
|
165
|
+
const response = await ctx.http.get(buildApiUrl("/v2/zhihu"), {
|
|
163
166
|
params: {
|
|
164
167
|
encoding: "json"
|
|
165
168
|
},
|
|
@@ -180,7 +183,7 @@ function apply(ctx, config) {
|
|
|
180
183
|
async function getAiNews(params) {
|
|
181
184
|
try {
|
|
182
185
|
logInfo("60s API: 开始获取AI快报", params);
|
|
183
|
-
const response = await ctx.http.get("
|
|
186
|
+
const response = await ctx.http.get(buildApiUrl("/v2/ai-news"), {
|
|
184
187
|
params: {
|
|
185
188
|
date: params.date,
|
|
186
189
|
all: params.all,
|
|
@@ -199,7 +202,7 @@ function apply(ctx, config) {
|
|
|
199
202
|
async function getMoyuDaily(encoding = "text") {
|
|
200
203
|
try {
|
|
201
204
|
logInfo("60s API: 开始获取摸鱼日报", { encoding });
|
|
202
|
-
const response = await ctx.http.get("
|
|
205
|
+
const response = await ctx.http.get(buildApiUrl("/v2/moyu"), {
|
|
203
206
|
params: {
|
|
204
207
|
encoding
|
|
205
208
|
},
|
|
@@ -216,7 +219,7 @@ function apply(ctx, config) {
|
|
|
216
219
|
async function getGoldPrice(encoding = "text") {
|
|
217
220
|
try {
|
|
218
221
|
logInfo("60s API: 开始获取今日金价", { encoding });
|
|
219
|
-
const response = await ctx.http.get("
|
|
222
|
+
const response = await ctx.http.get(buildApiUrl("/v2/gold-price"), {
|
|
220
223
|
params: {
|
|
221
224
|
encoding
|
|
222
225
|
},
|
|
@@ -233,7 +236,7 @@ function apply(ctx, config) {
|
|
|
233
236
|
async function getFuelPrice(params) {
|
|
234
237
|
try {
|
|
235
238
|
logInfo("60s API: 开始获取今日油价", params);
|
|
236
|
-
const response = await ctx.http.get("
|
|
239
|
+
const response = await ctx.http.get(buildApiUrl("/v2/fuel-price"), {
|
|
237
240
|
params: {
|
|
238
241
|
region: params.region,
|
|
239
242
|
encoding: params.encoding || "text"
|
|
@@ -268,42 +271,29 @@ function apply(ctx, config) {
|
|
|
268
271
|
}
|
|
269
272
|
__name(sendNewsToChannels, "sendNewsToChannels");
|
|
270
273
|
function formatAiNewsText(data) {
|
|
271
|
-
const
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
const source = newsItem.source ? `(${newsItem.source})` : "";
|
|
275
|
-
return `${index + 1}. ${newsItem.title}${source}
|
|
276
|
-
${newsItem.detail}
|
|
277
|
-
🔗 ${newsItem.link}`;
|
|
278
|
-
});
|
|
279
|
-
return [header, ...lines].join("\n\n");
|
|
280
|
-
}, "formatOne");
|
|
281
|
-
if (Array.isArray(data)) {
|
|
282
|
-
return data.map(formatOne).join("\n\n");
|
|
283
|
-
}
|
|
284
|
-
return formatOne(data);
|
|
274
|
+
const items = normalizeAiNewsData(data).flatMap((item) => item.news);
|
|
275
|
+
return items.map((newsItem, index) => `${index + 1}. ${newsItem.title}
|
|
276
|
+
${newsItem.link}`).join("\n\n");
|
|
285
277
|
}
|
|
286
278
|
__name(formatAiNewsText, "formatAiNewsText");
|
|
287
|
-
function
|
|
288
|
-
const
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
return formatOne(data);
|
|
279
|
+
function formatAiNewsMarkdown(data) {
|
|
280
|
+
const items = normalizeAiNewsData(data).flatMap((item) => item.news);
|
|
281
|
+
return items.map((newsItem) => `- [${newsItem.title}](${newsItem.link})`).join("\n");
|
|
282
|
+
}
|
|
283
|
+
__name(formatAiNewsMarkdown, "formatAiNewsMarkdown");
|
|
284
|
+
function toTitleLinkData(data) {
|
|
285
|
+
return normalizeAiNewsData(data).map((item) => ({
|
|
286
|
+
date: item.date,
|
|
287
|
+
news: item.news.map((newsItem) => ({
|
|
288
|
+
title: newsItem.title,
|
|
289
|
+
detail: "",
|
|
290
|
+
link: newsItem.link,
|
|
291
|
+
source: "",
|
|
292
|
+
date: newsItem.date
|
|
293
|
+
}))
|
|
294
|
+
}));
|
|
305
295
|
}
|
|
306
|
-
__name(
|
|
296
|
+
__name(toTitleLinkData, "toTitleLinkData");
|
|
307
297
|
function formatDate(date) {
|
|
308
298
|
const year = date.getFullYear();
|
|
309
299
|
const month = `${date.getMonth() + 1}`.padStart(2, "0");
|
|
@@ -790,7 +780,7 @@ ${shortDetail}
|
|
|
790
780
|
return "获取知乎话题榜失败,请稍后重试";
|
|
791
781
|
}
|
|
792
782
|
});
|
|
793
|
-
ctx.command("AI快报 [date]", "获取AI资讯快报").option("date", "-d <date> 指定日期 (格式: YYYY-MM-DD)").option("all", "-a 获取所有日期").option("encoding", "-e <encoding> 编码方式 text/json/markdown
|
|
783
|
+
ctx.command("AI快报 [date]", "获取AI资讯快报").option("date", "-d <date> 指定日期 (格式: YYYY-MM-DD)").option("all", "-a 获取所有日期").option("encoding", "-e <encoding> 编码方式 text/json/markdown").action(async (argv, date) => {
|
|
794
784
|
const userId = argv.session.userId;
|
|
795
785
|
if (!checkCooldown(userId)) {
|
|
796
786
|
const now = Date.now();
|
|
@@ -807,27 +797,20 @@ ${shortDetail}
|
|
|
807
797
|
const dates = getRecentAiNewsDates();
|
|
808
798
|
const data = await fetchAiNewsByDates(dates);
|
|
809
799
|
if (!data.length) return "未获取到近两天的AI快报数据";
|
|
810
|
-
if (encoding === "summary") {
|
|
811
|
-
return await argv.session.send(formatAiNewsSummary(data));
|
|
812
|
-
}
|
|
813
800
|
if (encoding === "json") {
|
|
814
|
-
|
|
801
|
+
await argv.session.send(JSON.stringify(toTitleLinkData(data), null, 2));
|
|
802
|
+
return;
|
|
815
803
|
}
|
|
816
804
|
if (encoding === "markdown") {
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
);
|
|
820
|
-
return await argv.session.send(markdownResponses.filter(Boolean).join("\n\n"));
|
|
805
|
+
await argv.session.send(formatAiNewsMarkdown(data));
|
|
806
|
+
return;
|
|
821
807
|
}
|
|
822
808
|
const message2 = formatAiNewsText(data);
|
|
823
809
|
if (config.aiUseForward && argv.session.platform === "onebot") {
|
|
824
|
-
|
|
810
|
+
await argv.session.send((0, import_koishi.h)("figure", {}, [message2]));
|
|
811
|
+
return;
|
|
825
812
|
}
|
|
826
|
-
|
|
827
|
-
}
|
|
828
|
-
if (encoding === "markdown") {
|
|
829
|
-
const response2 = await getAiNews({ date: targetDate, all, encoding: "markdown" });
|
|
830
|
-
await argv.session.send(response2);
|
|
813
|
+
await argv.session.send(message2);
|
|
831
814
|
return;
|
|
832
815
|
}
|
|
833
816
|
if (encoding === "json") {
|
|
@@ -836,22 +819,16 @@ ${shortDetail}
|
|
|
836
819
|
logError("60s API: AI快报返回错误", { code: response2.code, message: response2.message });
|
|
837
820
|
return `获取AI快报失败: ${response2.message || "未知错误"}`;
|
|
838
821
|
}
|
|
839
|
-
await argv.session.send(JSON.stringify(response2.data, null, 2));
|
|
822
|
+
await argv.session.send(JSON.stringify(toTitleLinkData(response2.data), null, 2));
|
|
840
823
|
return;
|
|
841
824
|
}
|
|
842
|
-
if (encoding === "
|
|
825
|
+
if (encoding === "markdown") {
|
|
843
826
|
const response2 = await getAiNews({ date: targetDate, all, encoding: "json" });
|
|
844
827
|
if (response2.code !== 200 || !response2.data) {
|
|
845
828
|
logError("60s API: AI快报返回错误", { code: response2.code, message: response2.message });
|
|
846
829
|
return `获取AI快报失败: ${response2.message || "未知错误"}`;
|
|
847
830
|
}
|
|
848
|
-
|
|
849
|
-
if (config.aiUseForward && argv.session.platform === "onebot") {
|
|
850
|
-
const forwardMessage = (0, import_koishi.h)("figure", {}, [message2]);
|
|
851
|
-
await argv.session.send(forwardMessage);
|
|
852
|
-
} else {
|
|
853
|
-
await argv.session.send(message2);
|
|
854
|
-
}
|
|
831
|
+
await argv.session.send(formatAiNewsMarkdown(response2.data));
|
|
855
832
|
return;
|
|
856
833
|
}
|
|
857
834
|
const response = await getAiNews({ date: targetDate, all, encoding: "json" });
|