koishi-plugin-weibo-post-monitor 1.0.1-beta.4 → 1.0.1-beta.7

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.js CHANGED
@@ -308,10 +308,38 @@ function checkWords(params, message) {
308
308
  return true;
309
309
  }
310
310
  __name(checkWords, "checkWords");
311
+ function stripHtmlTags(html) {
312
+ if (!html) return "";
313
+ let text = html;
314
+ text = text.replace(/<br\s*\/?>/gi, "\n");
315
+ text = text.replace(/<span\s+class=['"]surl-text['"][^>]*>([^<]*)<\/span>/gi, "$1");
316
+ text = text.replace(/<img[^>]*>/gi, "");
317
+ let lastText = "";
318
+ let iterations = 0;
319
+ while (text !== lastText && iterations < 10) {
320
+ lastText = text;
321
+ text = text.replace(/<a[^>]*>([^<]*(?:<[^>]*>[^<]*<\/[^>]*>[^<]*)*)<\/a>/gi, "$1");
322
+ iterations++;
323
+ }
324
+ text = text.replace(/<[^>]+>/g, "");
325
+ text = text.replace(/&nbsp;/g, " ");
326
+ text = text.replace(/&lt;/g, "<");
327
+ text = text.replace(/&gt;/g, ">");
328
+ text = text.replace(/&quot;/g, '"');
329
+ text = text.replace(/&#39;/g, "'");
330
+ text = text.replace(/&apos;/g, "'");
331
+ text = text.replace(/&amp;/g, "&");
332
+ text = text.replace(/[ \t]+/g, " ");
333
+ text = text.replace(/\n[ \t]+/g, "\n");
334
+ text = text.replace(/[ \t]+\n/g, "\n");
335
+ text = text.replace(/\n{3,}/g, "\n\n");
336
+ text = text.trim();
337
+ return text;
338
+ }
339
+ __name(stripHtmlTags, "stripHtmlTags");
311
340
 
312
341
  // src/services/message.ts
313
342
  var import_axios2 = __toESM(require("axios"));
314
- var import_vm = require("vm");
315
343
  async function getWeiboAndSendMessageToGroup(ctx, params) {
316
344
  const [err, res] = await to(getWeibo(params));
317
345
  if (err) {
@@ -395,16 +423,15 @@ async function getMessage(params, wbPost) {
395
423
  tempMessage += screenName + " 发布了微博:\n{temp_text}\n" + picture || "";
396
424
  }
397
425
  const mid = wbPost?.mid || "";
398
- const url = `https://m.weibo.cn/status/${mid}`;
426
+ const url = `https://m.weibo.cn/statuses/extend?id=${mid}`;
399
427
  const detailMessage = await getDetailMessage(url);
400
- logger.info("detailMessage = " + detailMessage);
401
428
  if (detailMessage) {
402
429
  message_text = detailMessage;
403
430
  } else {
404
431
  message_text = wbPost?.text_raw;
405
432
  }
406
433
  const urlMessage = `
407
- 微博链接:${url}`;
434
+ 微博链接:https://m.weibo.cn/status/${mid}`;
408
435
  if (!checkWords(params, message_text)) {
409
436
  return null;
410
437
  }
@@ -413,35 +440,6 @@ async function getMessage(params, wbPost) {
413
440
  return { post: wbpost, islast: true };
414
441
  }
415
442
  __name(getMessage, "getMessage");
416
- function stripHtmlTags(html) {
417
- if (!html) return "";
418
- let text = html;
419
- text = text.replace(/<br\s*\/?>/gi, "\n");
420
- text = text.replace(/<span\s+class=['"]surl-text['"][^>]*>([^<]*)<\/span>/gi, "$1");
421
- text = text.replace(/<img[^>]*>/gi, "");
422
- let lastText = "";
423
- let iterations = 0;
424
- while (text !== lastText && iterations < 10) {
425
- lastText = text;
426
- text = text.replace(/<a[^>]*>([^<]*(?:<[^>]*>[^<]*<\/[^>]*>[^<]*)*)<\/a>/gi, "$1");
427
- iterations++;
428
- }
429
- text = text.replace(/<[^>]+>/g, "");
430
- text = text.replace(/&nbsp;/g, " ");
431
- text = text.replace(/&lt;/g, "<");
432
- text = text.replace(/&gt;/g, ">");
433
- text = text.replace(/&quot;/g, '"');
434
- text = text.replace(/&#39;/g, "'");
435
- text = text.replace(/&apos;/g, "'");
436
- text = text.replace(/&amp;/g, "&");
437
- text = text.replace(/[ \t]+/g, " ");
438
- text = text.replace(/\n[ \t]+/g, "\n");
439
- text = text.replace(/[ \t]+\n/g, "\n");
440
- text = text.replace(/\n{3,}/g, "\n\n");
441
- text = text.trim();
442
- return text;
443
- }
444
- __name(stripHtmlTags, "stripHtmlTags");
445
443
  async function getDetailMessage(wb_url) {
446
444
  try {
447
445
  let auto_cookie2 = getCookie();
@@ -449,52 +447,59 @@ async function getDetailMessage(wb_url) {
449
447
  if (!auto_cookie2) {
450
448
  return null;
451
449
  }
450
+ let xsrfToken = "";
451
+ const cookieParts = auto_cookie2.split(";");
452
+ for (const part of cookieParts) {
453
+ const trimmed = part.trim();
454
+ if (trimmed.startsWith("XSRF-TOKEN=")) {
455
+ xsrfToken = trimmed.substring("XSRF-TOKEN=".length);
456
+ break;
457
+ }
458
+ }
459
+ const urlMatch = wb_url.match(/id=(\d+)/);
460
+ const weiboId = urlMatch ? urlMatch[1] : "";
452
461
  const headers = {
453
- "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",
462
+ "accept": "application/json, text/plain, */*",
454
463
  "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
455
464
  "cache-control": "no-cache",
465
+ "mweibo-pwa": "1",
466
+ "pragma": "no-cache",
467
+ "priority": "u=1, i",
468
+ "referer": weiboId ? `https://m.weibo.cn/status/${weiboId}` : "https://m.weibo.cn/",
469
+ "sec-ch-ua": '"Chromium";v="142", "Microsoft Edge";v="142", "Not_A Brand";v="99"',
470
+ "sec-ch-ua-mobile": "?0",
471
+ "sec-ch-ua-platform": '"Windows"',
472
+ "sec-fetch-dest": "empty",
473
+ "sec-fetch-mode": "cors",
474
+ "sec-fetch-site": "same-origin",
456
475
  "user-agent": now_user_agent2,
476
+ "x-requested-with": "XMLHttpRequest",
457
477
  "cookie": auto_cookie2
458
478
  };
479
+ if (xsrfToken) {
480
+ headers["x-xsrf-token"] = xsrfToken;
481
+ }
459
482
  const response = await import_axios2.default.get(wb_url, {
460
483
  headers,
461
- responseType: "text",
484
+ responseType: "json",
462
485
  timeout: 1e4
463
486
  });
464
- const html = response.data;
465
- const startMarker = "var $render_data = ";
466
- const startIndex = html.indexOf(startMarker);
467
- if (startIndex === -1) {
487
+ const responseData = response.data;
488
+ logger.info("responseData = " + JSON.stringify(responseData));
489
+ if (!responseData || responseData.ok !== 1 || !responseData.data) {
490
+ logger.error("获取微博response返回报错: " + JSON.stringify(responseData));
468
491
  return null;
469
492
  }
470
- const scriptEndIndex = html.indexOf("</script>", startIndex);
471
- const nextVarIndex = html.indexOf("\n var ", startIndex + startMarker.length);
472
- let codeEndIndex = html.length;
473
- if (scriptEndIndex !== -1 && scriptEndIndex < codeEndIndex) {
474
- codeEndIndex = scriptEndIndex;
475
- }
476
- if (nextVarIndex !== -1 && nextVarIndex < codeEndIndex) {
477
- codeEndIndex = nextVarIndex;
478
- }
479
- let code = html.substring(startIndex, codeEndIndex).trim();
480
- if (!code.endsWith(";")) {
481
- const lastSemicolon = code.lastIndexOf(";");
482
- if (lastSemicolon !== -1) {
483
- code = code.substring(0, lastSemicolon + 1);
484
- }
485
- }
486
- const context = (0, import_vm.createContext)({});
487
- (0, import_vm.runInContext)(code, context);
488
- const renderData = context.$render_data;
489
- logger.info("renderData = " + JSON.stringify(renderData));
490
- if (!renderData || !renderData.status || !renderData.status.text) {
493
+ const longTextContent = responseData.data.longTextContent;
494
+ if (!longTextContent) {
495
+ logger.error("获取微博返回的longTextContent为空");
491
496
  return null;
492
497
  }
493
- const htmlText = renderData.status.text;
494
- const plainText = stripHtmlTags(htmlText);
498
+ logger.info("longTextContent = " + JSON.stringify(responseData));
499
+ const plainText = stripHtmlTags(longTextContent);
495
500
  return plainText;
496
501
  } catch (error) {
497
- console.error("获取微博详情失败:", error);
502
+ logger.error("获取微博详情失败:", error);
498
503
  return null;
499
504
  }
500
505
  }
@@ -1,3 +1,4 @@
1
1
  export declare function to<T, U = Error>(promise: Promise<T>, errorExt?: object): Promise<[U, undefined] | [null, T]>;
2
2
  export declare function parseDateString(dateString: string): Date;
3
3
  export declare function checkWords(params: any, message: string): boolean;
4
+ export declare function stripHtmlTags(html: string): string;
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": "1.0.1-beta.4",
4
+ "version": "1.0.1-beta.7",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [