koishi-plugin-weibo-post-monitor 1.0.3-beta.0 → 1.0.3
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 +65 -104
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -161,7 +161,7 @@ __name(fetchCookie, "fetchCookie");
|
|
|
161
161
|
var import_koishi2 = require("koishi");
|
|
162
162
|
|
|
163
163
|
// src/services/weibo.ts
|
|
164
|
-
var
|
|
164
|
+
var import_axios2 = __toESM(require("axios"));
|
|
165
165
|
async function getWeibo(config, callback, retry = false) {
|
|
166
166
|
const { weiboUID, is_using_cookie } = config;
|
|
167
167
|
if (!weiboUID) {
|
|
@@ -181,6 +181,11 @@ async function getWeibo(config, callback, retry = false) {
|
|
|
181
181
|
logger.error(errorMsg);
|
|
182
182
|
throw new Error(errorMsg);
|
|
183
183
|
}
|
|
184
|
+
const extractXSRFToken = /* @__PURE__ */ __name((cookie) => {
|
|
185
|
+
const match = cookie.match(/XSRF-TOKEN=([^;]+)/i);
|
|
186
|
+
return match ? match[1] : void 0;
|
|
187
|
+
}, "extractXSRFToken");
|
|
188
|
+
const xsrfToken = extractXSRFToken(auto_cookie2);
|
|
184
189
|
const headers = {
|
|
185
190
|
"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",
|
|
186
191
|
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
|
|
@@ -189,109 +194,65 @@ async function getWeibo(config, callback, retry = false) {
|
|
|
189
194
|
"referer": "https://www.weibo.com/u/" + weiboUID,
|
|
190
195
|
"user-agent": now_user_agent2 || ""
|
|
191
196
|
};
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const randomUserAgent = getRandomUserAgent();
|
|
214
|
-
setUserAgent(randomUserAgent);
|
|
215
|
-
const newCookie = await fetchCookie();
|
|
216
|
-
setCookie(newCookie);
|
|
217
|
-
logger.info(`[getWeibo] Cookie更新成功,开始重试请求。weiboUID: ${weiboUID}`);
|
|
218
|
-
const retryResult = await getWeibo(config, callback, true);
|
|
219
|
-
resolve(retryResult);
|
|
220
|
-
} catch (retryError) {
|
|
221
|
-
const retryErrorMsg = `[getWeibo] 403错误重试失败。weiboUID: ${weiboUID}`;
|
|
222
|
-
logger.error(`${retryErrorMsg}, 原始错误: ${errorMsg}, 重试错误: ${retryError?.message || retryError}`);
|
|
223
|
-
reject({
|
|
224
|
-
error: new Error(`${retryErrorMsg}: ${retryError?.message || retryError}`),
|
|
225
|
-
body,
|
|
226
|
-
statusCode,
|
|
227
|
-
retryError,
|
|
228
|
-
errorType: "HTTP_403_RETRY_FAILED"
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
} else {
|
|
232
|
-
const finalErrorMsg = statusCode === 403 ? `[getWeibo] HTTP 403错误,已重试过或使用手动Cookie,无法自动重试。weiboUID: ${weiboUID}` : `[getWeibo] HTTP ${statusCode}错误。weiboUID: ${weiboUID}`;
|
|
233
|
-
logger.error(`${finalErrorMsg}, 响应预览: ${bodyPreview}`);
|
|
234
|
-
reject({
|
|
235
|
-
error: new Error(finalErrorMsg),
|
|
236
|
-
body,
|
|
237
|
-
statusCode,
|
|
238
|
-
errorType: statusCode === 403 ? "HTTP_403_NO_RETRY" : `HTTP_${statusCode}`
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
197
|
+
if (xsrfToken) {
|
|
198
|
+
headers["x-xsrf-token"] = xsrfToken;
|
|
199
|
+
}
|
|
200
|
+
const url = `https://www.weibo.com/ajax/statuses/mymblog?uid=${weiboUID}`;
|
|
201
|
+
try {
|
|
202
|
+
const response = await import_axios2.default.get(url, {
|
|
203
|
+
headers,
|
|
204
|
+
responseType: "json",
|
|
205
|
+
timeout: 1e4
|
|
206
|
+
});
|
|
207
|
+
const returnData = response.data;
|
|
208
|
+
callback?.(returnData);
|
|
209
|
+
return returnData;
|
|
210
|
+
} catch (error) {
|
|
211
|
+
if (error.response) {
|
|
212
|
+
const statusCode = error.response.status;
|
|
213
|
+
const bodyPreview = JSON.stringify(error.response.data).substring(0, 200);
|
|
214
|
+
const errorMsg = `[getWeibo] HTTP状态码错误: ${statusCode}`;
|
|
215
|
+
logger.error(`${errorMsg}, weiboUID: ${weiboUID}, 响应预览: ${bodyPreview}`);
|
|
216
|
+
if (statusCode === 403 && !retry && !is_using_cookie) {
|
|
217
|
+
logger.warn(`[getWeibo] 检测到403错误,尝试更新Cookie后重试。weiboUID: ${weiboUID}`);
|
|
243
218
|
try {
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const retryErrorMsg = `[getWeibo] JSON解析错误重试失败`;
|
|
262
|
-
logger.error(`${retryErrorMsg}, weiboUID: ${weiboUID}, 原始错误: ${error?.message || error}, 重试错误: ${retryError?.message || retryError}`);
|
|
263
|
-
reject({
|
|
264
|
-
error: new Error(`${retryErrorMsg}: ${retryError?.message || retryError}`),
|
|
265
|
-
body,
|
|
266
|
-
retryError,
|
|
267
|
-
originalError: error,
|
|
268
|
-
errorType: "JSON_PARSE_RETRY_FAILED"
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
} else {
|
|
272
|
-
const finalErrorMsg = `[getWeibo] JSON解析失败,已重试过或使用手动Cookie,无法自动重试`;
|
|
273
|
-
logger.error(`${finalErrorMsg}, weiboUID: ${weiboUID}, 错误: ${error?.message || error}`);
|
|
274
|
-
reject({
|
|
275
|
-
error: new Error(`${finalErrorMsg}: ${error?.message || error}`),
|
|
276
|
-
body,
|
|
277
|
-
originalError: error,
|
|
278
|
-
errorType: "JSON_PARSE_NO_RETRY"
|
|
279
|
-
});
|
|
280
|
-
}
|
|
219
|
+
const randomUserAgent = getRandomUserAgent();
|
|
220
|
+
setUserAgent(randomUserAgent);
|
|
221
|
+
const newCookie = await fetchCookie();
|
|
222
|
+
setCookie(newCookie);
|
|
223
|
+
logger.info(`[getWeibo] Cookie更新成功,开始重试请求。weiboUID: ${weiboUID}`);
|
|
224
|
+
const retryResult = await getWeibo(config, callback, true);
|
|
225
|
+
return retryResult;
|
|
226
|
+
} catch (retryError) {
|
|
227
|
+
const retryErrorMsg = `[getWeibo] 403错误重试失败。weiboUID: ${weiboUID}`;
|
|
228
|
+
logger.error(`${retryErrorMsg}, 原始错误: ${errorMsg}, 重试错误: ${retryError?.message || retryError}`);
|
|
229
|
+
throw {
|
|
230
|
+
error: new Error(`${retryErrorMsg}: ${retryError?.message || retryError}`),
|
|
231
|
+
body: error.response.data,
|
|
232
|
+
statusCode,
|
|
233
|
+
retryError,
|
|
234
|
+
errorType: "HTTP_403_RETRY_FAILED"
|
|
235
|
+
};
|
|
281
236
|
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
237
|
+
} else {
|
|
238
|
+
const finalErrorMsg = statusCode === 403 ? `[getWeibo] HTTP 403错误,已重试过或使用手动Cookie,无法自动重试。weiboUID: ${weiboUID}` : `[getWeibo] HTTP ${statusCode}错误。weiboUID: ${weiboUID}`;
|
|
239
|
+
logger.error(`${finalErrorMsg}, 响应预览: ${bodyPreview}`);
|
|
240
|
+
throw {
|
|
241
|
+
error: new Error(finalErrorMsg),
|
|
242
|
+
body: error.response.data,
|
|
243
|
+
statusCode,
|
|
244
|
+
errorType: statusCode === 403 ? "HTTP_403_NO_RETRY" : `HTTP_${statusCode}`
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const networkErrorMsg = `[getWeibo] 网络请求错误`;
|
|
249
|
+
logger.error(`${networkErrorMsg}, weiboUID: ${weiboUID}, 错误: ${error?.message || error}`);
|
|
250
|
+
throw {
|
|
251
|
+
error: new Error(`${networkErrorMsg}: ${error?.message || error}`),
|
|
252
|
+
originalError: error,
|
|
253
|
+
errorType: "NETWORK_ERROR"
|
|
254
|
+
};
|
|
255
|
+
}
|
|
295
256
|
}
|
|
296
257
|
__name(getWeibo, "getWeibo");
|
|
297
258
|
|
|
@@ -405,7 +366,7 @@ function stripHtmlTags(html) {
|
|
|
405
366
|
__name(stripHtmlTags, "stripHtmlTags");
|
|
406
367
|
|
|
407
368
|
// src/services/message.ts
|
|
408
|
-
var
|
|
369
|
+
var import_axios3 = __toESM(require("axios"));
|
|
409
370
|
async function getWeiboAndSendMessageToGroup(ctx, params) {
|
|
410
371
|
const [err, res] = await to(getWeibo(params));
|
|
411
372
|
if (err) {
|
|
@@ -544,7 +505,7 @@ async function getDetailMessage(wb_url) {
|
|
|
544
505
|
if (xsrfToken) {
|
|
545
506
|
headers["x-xsrf-token"] = xsrfToken;
|
|
546
507
|
}
|
|
547
|
-
const response = await
|
|
508
|
+
const response = await import_axios3.default.get(wb_url, {
|
|
548
509
|
headers,
|
|
549
510
|
responseType: "json",
|
|
550
511
|
timeout: 1e4
|
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.3
|
|
4
|
+
"version": "1.0.3",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"files": [
|