koishi-plugin-weibo-post-monitor 0.0.8 → 0.0.10

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,23 @@ __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
+ ];
42
52
  var Config = import_koishi.Schema.object({
43
53
  account: import_koishi.Schema.string().description("账号(qq号)"),
44
54
  plantform: import_koishi.Schema.string().default("onebot").description("账号平台"),
45
55
  waitMinutes: import_koishi.Schema.number().default(3).min(1).description("隔多久拉取一次最新微博 (分钟)"),
46
- using_cookie: import_koishi.Schema.string().description("cookie"),
56
+ is_using_cookie: import_koishi.Schema.boolean().default(false).description("是否启用输入cookie"),
57
+ manual_cookie: import_koishi.Schema.string().default("").description("输入cookie"),
47
58
  sendINFO: import_koishi.Schema.array(import_koishi.Schema.object({
48
59
  weiboUID: import_koishi.Schema.string().description("微博用户UID"),
49
60
  forward: import_koishi.Schema.boolean().default(false).description("是否监听转发"),
@@ -68,8 +79,10 @@ function apply(ctx, config) {
68
79
  account: config.account,
69
80
  plantform: config.plantform,
70
81
  waitMinutes: config.waitMinutes,
71
- using_cookie: config.using_cookie
82
+ is_using_cookie: config.is_using_cookie
72
83
  };
84
+ auto_cookie = config.manual_cookie;
85
+ now_user_agent = user_agent_list[0];
73
86
  ctx.setInterval(async () => {
74
87
  for (const singleConfig of config.sendINFO) {
75
88
  const params = { ...commonConfig, ...singleConfig };
@@ -165,18 +178,25 @@ var getMessage = /* @__PURE__ */ __name((params, wbPost) => {
165
178
  const wbpost = message ? message + url : screenName + " 发布了微博:\n" + wbPost?.text_raw + url || "";
166
179
  return { post: wbpost, islast: true };
167
180
  }, "getMessage");
168
- var getWeibo = /* @__PURE__ */ __name(async (config, callback) => {
169
- const { weiboUID, using_cookie } = config;
181
+ var getWeibo = /* @__PURE__ */ __name(async (config, callback, retry = false) => {
182
+ const { weiboUID, is_using_cookie } = config;
170
183
  if (!weiboUID) {
171
184
  return;
172
185
  }
186
+ if (!is_using_cookie && !auto_cookie) {
187
+ now_user_agent = user_agent_list[Math.floor(Math.random() * user_agent_list.length)];
188
+ auto_cookie = await getCookie();
189
+ }
190
+ if (!auto_cookie) {
191
+ throw new Error("Cookie config is null, please check the config");
192
+ }
173
193
  const headers = {
174
194
  "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
195
  "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
176
196
  "cache-control": "no-cache",
177
- "cookie": using_cookie,
178
- "referer": "https://www.weibo.com/u/7730797357",
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"
197
+ "cookie": auto_cookie,
198
+ "referer": "https://www.weibo.com/u/" + weiboUID,
199
+ "user-agent": now_user_agent
180
200
  };
181
201
  const options = {
182
202
  hostname: "www.weibo.com",
@@ -190,13 +210,24 @@ var getWeibo = /* @__PURE__ */ __name(async (config, callback) => {
190
210
  res.on("data", (chunk) => {
191
211
  body += chunk;
192
212
  });
193
- res.on("end", () => {
213
+ res.on("end", async () => {
194
214
  try {
195
215
  const returnData = JSON.parse(body);
196
216
  callback?.(returnData);
197
217
  resolve(returnData);
198
218
  } catch (error) {
199
- reject({ error, body });
219
+ if (!retry && !is_using_cookie) {
220
+ try {
221
+ now_user_agent = user_agent_list[Math.floor(Math.random() * user_agent_list.length)];
222
+ auto_cookie = await getCookie();
223
+ const retryResult = await getWeibo(config, callback, true);
224
+ resolve(retryResult);
225
+ } catch (retryError) {
226
+ reject({ error, body, retryError });
227
+ }
228
+ } else {
229
+ reject({ error, body });
230
+ }
200
231
  }
201
232
  });
202
233
  res.on("error", (error) => {
@@ -264,6 +295,52 @@ var checkWords = /* @__PURE__ */ __name((params, message) => {
264
295
  }
265
296
  return true;
266
297
  }, "checkWords");
298
+ var mergeCookies = /* @__PURE__ */ __name((cookies) => {
299
+ return cookies.map((c) => c.split(";")[0]).join("; ");
300
+ }, "mergeCookies");
301
+ var getCookie = /* @__PURE__ */ __name(async () => {
302
+ const commonHeaders = {
303
+ "user-agent": now_user_agent,
304
+ "accept": "*/*",
305
+ "origin": "https://passport.weibo.com",
306
+ "referer": "https://passport.weibo.com/visitor/visitor"
307
+ };
308
+ let allCookies = [];
309
+ 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/", {
310
+ headers: {
311
+ ...commonHeaders,
312
+ "content-type": "application/x-www-form-urlencoded"
313
+ },
314
+ responseType: "text"
315
+ });
316
+ const step1SetCookies = step1Response.headers["set-cookie"] || [];
317
+ allCookies.push(...step1SetCookies);
318
+ const step1Body = step1Response.data;
319
+ let visitorId = "";
320
+ try {
321
+ const jsonMatch = step1Body.match(/visitor_gray_callback\s*\(\s*({[\s\S]*})\s*\)/);
322
+ if (!jsonMatch || !jsonMatch[1]) {
323
+ throw new Error("无法从响应中提取 JSON 对象");
324
+ }
325
+ const step1Data = JSON.parse(jsonMatch[1]);
326
+ visitorId = step1Data?.data?.tid || "";
327
+ if (!visitorId) {
328
+ throw new Error("未获取到 visitor id,响应数据: " + JSON.stringify(step1Data));
329
+ }
330
+ } catch (error) {
331
+ throw new Error("解析 genvisitor2 响应失败: " + error.message + ", 响应体: " + step1Body);
332
+ }
333
+ const step2Response = await import_axios.default.get(`https://passport.weibo.com/visitor/visitor?a=incarnate&t=${encodeURIComponent(visitorId)}`, {
334
+ headers: {
335
+ ...commonHeaders,
336
+ "cookie": mergeCookies(allCookies)
337
+ },
338
+ responseType: "text"
339
+ });
340
+ const step2SetCookies = step2Response.headers["set-cookie"] || [];
341
+ allCookies.push(...step2SetCookies);
342
+ return mergeCookies(allCookies);
343
+ }, "getCookie");
267
344
  // Annotate the CommonJS export names for ESM import in node:
268
345
  0 && (module.exports = {
269
346
  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.8",
4
+ "version": "0.0.10",
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
  }