koishi-plugin-weibo-post-monitor 1.0.1-beta.3 → 1.0.1-beta.5

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) {
@@ -356,11 +384,14 @@ async function getMessage(params, wbPost) {
356
384
  }
357
385
  const screenName = user?.screen_name || "";
358
386
  let weiboType = -1;
387
+ logger.info("wbPost = " + JSON.stringify(wbPost));
359
388
  if (wbPost?.page_info) {
360
389
  weiboType = 0;
361
- } else if (wbPost?.pic_infos && wbPost?.pic_ids && wbPost.pic_ids.length > 0) {
390
+ }
391
+ if (wbPost?.pic_infos && wbPost?.pic_ids && wbPost.pic_ids.length > 0) {
362
392
  weiboType = 2;
363
- } else if (wbPost?.retweeted_status) {
393
+ }
394
+ if (wbPost?.retweeted_status) {
364
395
  weiboType = 1;
365
396
  }
366
397
  let message_text = "";
@@ -369,7 +400,6 @@ async function getMessage(params, wbPost) {
369
400
  return null;
370
401
  }
371
402
  if (weiboType == 0) {
372
- logger.info("获取到推文类型: 0-视频");
373
403
  const pageInfo = wbPost?.page_info;
374
404
  if (!pageInfo) {
375
405
  return null;
@@ -381,22 +411,19 @@ async function getMessage(params, wbPost) {
381
411
  }
382
412
  }
383
413
  if (weiboType == 1) {
384
- logger.info("获取到推文类型: 1-转发微博");
385
414
  if (params.forward) {
386
415
  tempMessage += screenName + " 转发了微博:\n{temp_text}" || "";
387
416
  }
388
417
  }
389
418
  if (weiboType == 2) {
390
- logger.info("获取到推文类型: 2-图文");
391
419
  const picIds = wbPost?.pic_ids || [];
392
420
  const picInfos = wbPost?.pic_infos || {};
393
421
  const firstPicUrl = picInfos?.[picIds[0]]?.large?.url || "";
394
422
  const picture = import_koishi2.h.image(firstPicUrl);
395
423
  tempMessage += screenName + " 发布了微博:\n{temp_text}\n" + picture || "";
396
424
  }
397
- logger.info("[weiboType = " + weiboType + "]");
398
425
  const mid = wbPost?.mid || "";
399
- const url = `https://m.weibo.cn/status/${mid}`;
426
+ const url = `https://m.weibo.cn/statuses/extend?id=${mid}`;
400
427
  const detailMessage = await getDetailMessage(url);
401
428
  if (detailMessage) {
402
429
  message_text = detailMessage;
@@ -404,7 +431,7 @@ async function getMessage(params, wbPost) {
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,86 +440,62 @@ 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
- const cookie = getCookie();
448
- const userAgent = getUserAgent();
449
- if (!cookie) {
445
+ let auto_cookie2 = getCookie();
446
+ let now_user_agent2 = getUserAgent();
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",
454
- "accept-language": "zh-CN,zh;q=0.9",
455
- "user-agent": userAgent || "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
456
- "cookie": cookie
462
+ "accept": "application/json, text/plain, */*",
463
+ "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
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",
475
+ "user-agent": now_user_agent2,
476
+ "x-requested-with": "XMLHttpRequest",
477
+ "cookie": auto_cookie2
457
478
  };
479
+ if (xsrfToken) {
480
+ headers["x-xsrf-token"] = xsrfToken;
481
+ }
458
482
  const response = await import_axios2.default.get(wb_url, {
459
483
  headers,
460
- responseType: "text",
484
+ responseType: "json",
461
485
  timeout: 1e4
462
486
  });
463
- const html = response.data;
464
- const startMarker = "var $render_data = ";
465
- const startIndex = html.indexOf(startMarker);
466
- if (startIndex === -1) {
487
+ const responseData = response.data;
488
+ if (!responseData || responseData.ok !== 1 || !responseData.data) {
467
489
  return null;
468
490
  }
469
- const scriptEndIndex = html.indexOf("</script>", startIndex);
470
- const nextVarIndex = html.indexOf("\n var ", startIndex + startMarker.length);
471
- let codeEndIndex = html.length;
472
- if (scriptEndIndex !== -1 && scriptEndIndex < codeEndIndex) {
473
- codeEndIndex = scriptEndIndex;
474
- }
475
- if (nextVarIndex !== -1 && nextVarIndex < codeEndIndex) {
476
- codeEndIndex = nextVarIndex;
477
- }
478
- let code = html.substring(startIndex, codeEndIndex).trim();
479
- if (!code.endsWith(";")) {
480
- const lastSemicolon = code.lastIndexOf(";");
481
- if (lastSemicolon !== -1) {
482
- code = code.substring(0, lastSemicolon + 1);
483
- }
484
- }
485
- const context = (0, import_vm.createContext)({});
486
- (0, import_vm.runInContext)(code, context);
487
- const renderData = context.$render_data;
488
- if (!renderData || !renderData.status || !renderData.status.text) {
491
+ const longTextContent = responseData.data.longTextContent;
492
+ if (!longTextContent) {
489
493
  return null;
490
494
  }
491
- const htmlText = renderData.status.text;
492
- const plainText = stripHtmlTags(htmlText);
495
+ const plainText = stripHtmlTags(longTextContent);
493
496
  return plainText;
494
497
  } catch (error) {
495
- console.error("获取微博详情失败:", error);
498
+ logger.error("获取微博详情失败:", error);
496
499
  return null;
497
500
  }
498
501
  }
@@ -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.3",
4
+ "version": "1.0.1-beta.5",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [