koishi-plugin-weibo-post-monitor 0.0.9 → 1.0.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
@@ -5,7 +5,8 @@ export interface Config {
5
5
  plantform: string;
6
6
  waitMinutes: number;
7
7
  sendINFO: any;
8
- using_cookie: string;
8
+ is_using_cookie: boolean;
9
+ manual_cookie: string;
9
10
  }
10
11
  export declare const Config: Schema<Config>;
11
12
  export declare function to<T, U = Error>(promise: Promise<T>, errorExt?: object): Promise<[U, undefined] | [null, T]>;
package/lib/index.js CHANGED
@@ -38,12 +38,40 @@ __export(src_exports, {
38
38
  module.exports = __toCommonJS(src_exports);
39
39
  var import_koishi = require("koishi");
40
40
  var import_https = __toESM(require("https"));
41
+ var import_axios = __toESM(require("axios"));
41
42
  var name = "weibo-post-monitor";
43
+ var auto_cookie = null;
44
+ var now_user_agent = null;
45
+ var user_agent_list = [
46
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
47
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.5993.90 Safari/537.36",
48
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.6045.105 Safari/537.36",
49
+ "Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.71 Safari/537.36",
50
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.187 Safari/537.36",
51
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.7267.799 Safari/537.36 Edge/13.10586",
52
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.4907.884 Safari/537.36 Edge/13.10586",
53
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.6562.41 Safari/537.36 Edge/13.10586",
54
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2276.302 Safari/537.36 Edge/13.10586",
55
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.5174.848 Safari/537.36 Edge/13.10586",
56
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.4662.897 Safari/537.36 Edge/13.10586",
57
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.8759.750 Safari/537.36 Edge/13.10586",
58
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.9383.882 Safari/537.36 Edge/13.10586",
59
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.7253.966 Safari/537.36 Edge/13.10586",
60
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.3909.697 Safari/537.36 Edge/13.10586",
61
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.6167.85 Safari/537.36",
62
+ "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.130 Safari/537.36",
63
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.6045.199 Safari/537.36",
64
+ "Mozilla/5.0 (Windows NT 11.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.6167.139 Safari/537.36",
65
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.5993.118 Safari/537.36",
66
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.5938.149 Safari/537.36",
67
+ "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.140 Safari/537.36"
68
+ ];
42
69
  var Config = import_koishi.Schema.object({
43
70
  account: import_koishi.Schema.string().description("账号(qq号)"),
44
71
  plantform: import_koishi.Schema.string().default("onebot").description("账号平台"),
45
72
  waitMinutes: import_koishi.Schema.number().default(3).min(1).description("隔多久拉取一次最新微博 (分钟)"),
46
- using_cookie: import_koishi.Schema.string().description("cookie"),
73
+ is_using_cookie: import_koishi.Schema.boolean().default(false).description("是否启用输入cookie"),
74
+ manual_cookie: import_koishi.Schema.string().default("").description("输入cookie"),
47
75
  sendINFO: import_koishi.Schema.array(import_koishi.Schema.object({
48
76
  weiboUID: import_koishi.Schema.string().description("微博用户UID"),
49
77
  forward: import_koishi.Schema.boolean().default(false).description("是否监听转发"),
@@ -68,8 +96,10 @@ function apply(ctx, config) {
68
96
  account: config.account,
69
97
  plantform: config.plantform,
70
98
  waitMinutes: config.waitMinutes,
71
- using_cookie: config.using_cookie
99
+ is_using_cookie: config.is_using_cookie
72
100
  };
101
+ auto_cookie = config.manual_cookie;
102
+ now_user_agent = user_agent_list[0];
73
103
  ctx.setInterval(async () => {
74
104
  for (const singleConfig of config.sendINFO) {
75
105
  const params = { ...commonConfig, ...singleConfig };
@@ -165,18 +195,25 @@ var getMessage = /* @__PURE__ */ __name((params, wbPost) => {
165
195
  const wbpost = message ? message + url : screenName + " 发布了微博:\n" + wbPost?.text_raw + url || "";
166
196
  return { post: wbpost, islast: true };
167
197
  }, "getMessage");
168
- var getWeibo = /* @__PURE__ */ __name(async (config, callback) => {
169
- const { weiboUID, using_cookie } = config;
198
+ var getWeibo = /* @__PURE__ */ __name(async (config, callback, retry = false) => {
199
+ const { weiboUID, is_using_cookie } = config;
170
200
  if (!weiboUID) {
171
201
  return;
172
202
  }
203
+ if (!is_using_cookie && !auto_cookie) {
204
+ now_user_agent = user_agent_list[Math.floor(Math.random() * user_agent_list.length)];
205
+ auto_cookie = await getCookie();
206
+ }
207
+ if (!auto_cookie) {
208
+ throw new Error("Cookie config is null, please check the config");
209
+ }
173
210
  const headers = {
174
211
  "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
175
212
  "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
176
213
  "cache-control": "no-cache",
177
- "cookie": using_cookie,
214
+ "cookie": auto_cookie,
178
215
  "referer": "https://www.weibo.com/u/" + weiboUID,
179
- "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
216
+ "user-agent": now_user_agent
180
217
  };
181
218
  const options = {
182
219
  hostname: "www.weibo.com",
@@ -190,13 +227,24 @@ var getWeibo = /* @__PURE__ */ __name(async (config, callback) => {
190
227
  res.on("data", (chunk) => {
191
228
  body += chunk;
192
229
  });
193
- res.on("end", () => {
230
+ res.on("end", async () => {
194
231
  try {
195
232
  const returnData = JSON.parse(body);
196
233
  callback?.(returnData);
197
234
  resolve(returnData);
198
235
  } catch (error) {
199
- reject({ error, body });
236
+ if (!retry && !is_using_cookie) {
237
+ try {
238
+ now_user_agent = user_agent_list[Math.floor(Math.random() * user_agent_list.length)];
239
+ auto_cookie = await getCookie();
240
+ const retryResult = await getWeibo(config, callback, true);
241
+ resolve(retryResult);
242
+ } catch (retryError) {
243
+ reject({ error, body, retryError });
244
+ }
245
+ } else {
246
+ reject({ error, body });
247
+ }
200
248
  }
201
249
  });
202
250
  res.on("error", (error) => {
@@ -264,6 +312,52 @@ var checkWords = /* @__PURE__ */ __name((params, message) => {
264
312
  }
265
313
  return true;
266
314
  }, "checkWords");
315
+ var mergeCookies = /* @__PURE__ */ __name((cookies) => {
316
+ return cookies.map((c) => c.split(";")[0]).join("; ");
317
+ }, "mergeCookies");
318
+ var getCookie = /* @__PURE__ */ __name(async () => {
319
+ const commonHeaders = {
320
+ "user-agent": now_user_agent,
321
+ "accept": "*/*",
322
+ "origin": "https://passport.weibo.com",
323
+ "referer": "https://passport.weibo.com/visitor/visitor"
324
+ };
325
+ let allCookies = [];
326
+ const step1Response = await import_axios.default.post("https://passport.weibo.com/visitor/genvisitor2", "cb=visitor_gray_callback&ver=20250916&request_id=fb15e537349aef3542ed130a47d48eb8&tid=&from=weibo&webdriver=false&rid=01by624JxwbmsPak-53wiC9LGhR6I&return_url=https://weibo.com/", {
327
+ headers: {
328
+ ...commonHeaders,
329
+ "content-type": "application/x-www-form-urlencoded"
330
+ },
331
+ responseType: "text"
332
+ });
333
+ const step1SetCookies = step1Response.headers["set-cookie"] || [];
334
+ allCookies.push(...step1SetCookies);
335
+ const step1Body = step1Response.data;
336
+ let visitorId = "";
337
+ try {
338
+ const jsonMatch = step1Body.match(/visitor_gray_callback\s*\(\s*({[\s\S]*})\s*\)/);
339
+ if (!jsonMatch || !jsonMatch[1]) {
340
+ throw new Error("无法从响应中提取 JSON 对象");
341
+ }
342
+ const step1Data = JSON.parse(jsonMatch[1]);
343
+ visitorId = step1Data?.data?.tid || "";
344
+ if (!visitorId) {
345
+ throw new Error("未获取到 visitor id,响应数据: " + JSON.stringify(step1Data));
346
+ }
347
+ } catch (error) {
348
+ throw new Error("解析 genvisitor2 响应失败: " + error.message + ", 响应体: " + step1Body);
349
+ }
350
+ const step2Response = await import_axios.default.get(`https://passport.weibo.com/visitor/visitor?a=incarnate&t=${encodeURIComponent(visitorId)}`, {
351
+ headers: {
352
+ ...commonHeaders,
353
+ "cookie": mergeCookies(allCookies)
354
+ },
355
+ responseType: "text"
356
+ });
357
+ const step2SetCookies = step2Response.headers["set-cookie"] || [];
358
+ allCookies.push(...step2SetCookies);
359
+ return mergeCookies(allCookies);
360
+ }, "getCookie");
267
361
  // Annotate the CommonJS export names for ESM import in node:
268
362
  0 && (module.exports = {
269
363
  Config,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-weibo-post-monitor",
3
3
  "description": "微博帖子更新推送插件,用于获取指定微博用户的最新帖子推送到指定群聊,参考代码https://github.com/moehuhu/weibo-monitor",
4
- "version": "0.0.9",
4
+ "version": "1.0.0",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -20,6 +20,7 @@
20
20
  "koishi": "^4.18.7"
21
21
  },
22
22
  "dependencies": {
23
- "https": "^1.0.0"
23
+ "https": "^1.0.0",
24
+ "axios": "^1.0.0"
24
25
  }
25
26
  }