koishi-plugin-nitter 0.0.8 → 0.0.9

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
@@ -16,6 +16,7 @@ export interface Config {
16
16
  app: string;
17
17
  enableReTweet: boolean;
18
18
  sendPic: boolean;
19
+ cronString: string;
19
20
  maxSize: number;
20
21
  }
21
22
  declare module 'koishi' {
package/lib/index.js CHANGED
@@ -41,18 +41,21 @@ var import_koishi = require("koishi");
41
41
  // src/translate.ts
42
42
  var import_axios = __toESM(require("axios"));
43
43
  var import_https_proxy_agent = require("https-proxy-agent");
44
+
45
+ // src/retry.ts
44
46
  async function retry(retries, fn, delay = 500) {
45
47
  try {
46
- await fn();
47
- return Promise.resolve();
48
+ return await fn();
48
49
  } catch (err) {
49
50
  console.log(`剩余${retries}次尝试`, err);
50
- await new Promise((resolve) => setTimeout(resolve, delay));
51
- return retries > 1 ? retry(retries - 1, fn, delay * 2) : Promise.reject(err);
51
+ if (retries <= 1) throw err;
52
+ await new Promise((r) => setTimeout(r, delay));
53
+ return retry(retries - 1, fn, delay * 2);
52
54
  }
53
- ;
54
55
  }
55
56
  __name(retry, "retry");
57
+
58
+ // src/translate.ts
56
59
  var translate;
57
60
  function setGoogleTranslate(key, proxy) {
58
61
  translate = /* @__PURE__ */ __name((texts) => {
@@ -142,8 +145,8 @@ async function addTranslate(page, className) {
142
145
  tempDiv.innerHTML = translatedHTMLs2[index];
143
146
  const newElement = tempDiv.firstElementChild;
144
147
  if (newElement) {
145
- element.insertAdjacentElement("afterend", newElement);
146
- element.insertAdjacentHTML("afterend", '<div style="height: 1px;background-color: #ccc; margin-top: 10px; margin-bottom: 10px;"></div>');
148
+ element.insertAdjacentElement("beforebegin", newElement);
149
+ element.insertAdjacentHTML("beforebegin", '<div style="height: 1px;background-color: #ccc; margin-top: 10px; margin-bottom: 10px;"></div>');
147
150
  }
148
151
  }
149
152
  });
@@ -153,7 +156,6 @@ __name(addTranslate, "addTranslate");
153
156
 
154
157
  // src/index.tsx
155
158
  var import_rettiwt_api = require("rettiwt-api");
156
- var import_node_cron = require("node-cron");
157
159
 
158
160
  // src/download.ts
159
161
  var import_fluent_ffmpeg = __toESM(require("fluent-ffmpeg"));
@@ -269,7 +271,8 @@ var Config = import_koishi.Schema.intersect([
269
271
  }).description("订阅配置"),
270
272
  import_koishi.Schema.object({
271
273
  enableReTweet: import_koishi.Schema.boolean().default(false).description("是否发送转推"),
272
- sendPic: import_koishi.Schema.boolean().default(false).description("是否单独发送推文中的图片")
274
+ sendPic: import_koishi.Schema.boolean().default(false).description("是否单独发送推文中的图片"),
275
+ cronString: import_koishi.Schema.string().default("15 */5 * * * *").description("使用cron表达式描述检查更新的时间,默认为每隔5分钟检查一次")
273
276
  }).description("推送设置"),
274
277
  import_koishi.Schema.union([
275
278
  import_koishi.Schema.object({
@@ -289,7 +292,8 @@ function apply(ctx, config) {
289
292
  config.nitterUrl = config.nitterUrl.replace(/\/+$/, "");
290
293
  const twitterClient = new import_rettiwt_api.Rettiwt({
291
294
  apiKey: config.apiKey,
292
- proxyUrl: config.proxy ? new URL(config.proxy) : void 0
295
+ proxyUrl: config.proxy ? new URL(config.proxy) : void 0,
296
+ logging: true
293
297
  });
294
298
  let cronJob;
295
299
  const queue = new taskQueue();
@@ -307,11 +311,6 @@ function apply(ctx, config) {
307
311
  };
308
312
  }));
309
313
  queue.onProcess(broadcast);
310
- cronJob = (0, import_node_cron.schedule)("15 */5 * * * *", checkForUpdates);
311
- ctx.on("dispose", () => {
312
- cronJob.stop();
313
- });
314
- ctx.logger("nitter").info("开始监听推特动态");
315
314
  })();
316
315
  ctx.command("nitter.follow", "按照subscription中的订阅配置,使用登录的推特账号关注所有需要订阅的账号", { authority: 3 }).action(async () => {
317
316
  const whiteList = ctx.subscription.getAvailableAccounts(config.app);
@@ -319,9 +318,14 @@ function apply(ctx, config) {
319
318
  const followingIdList = followingList.map((user) => user.userName);
320
319
  for (const id of whiteList) {
321
320
  if (!followingIdList.includes(id)) {
322
- const user = await twitterClient.user.details(id);
321
+ let user;
322
+ await retry(3, async () => {
323
+ user = await twitterClient.user.details(id);
324
+ });
323
325
  await new Promise((resolve) => setTimeout(resolve, 3 * 1e3));
324
- await twitterClient.user.follow(user.id);
326
+ await retry(3, async () => {
327
+ await twitterClient.user.follow(user.id);
328
+ }, 30 * 1e3);
325
329
  console.log(`关注${id}成功`);
326
330
  await new Promise((resolve) => setTimeout(resolve, 30 * 1e3));
327
331
  }
@@ -360,8 +364,8 @@ function apply(ctx, config) {
360
364
  try {
361
365
  const [screenshot, imageUrls, hlsUrls] = await renderTweetScreenshot(tweetId);
362
366
  const screenshotMsg = /* @__PURE__ */ (0, import_jsx_runtime.jsx)("img", { src: "data:image/png;base64," + screenshot.toString("base64") });
363
- ctx.subscription.broadcast(config.app, account, screenshotMsg);
364
- let forwardMsg = [];
367
+ await ctx.subscription.broadcast(config.app, account, screenshotMsg);
368
+ let forwardMsg = [`${ctx.subscription.getName(config.app, account)} ${ctx.subscription.getAccount(config.app, account)}`];
365
369
  const videoUrls = await downloadVideosToBase64(hlsUrls, config.maxSize);
366
370
  if (imageUrls.length > 0) {
367
371
  forwardMsg.push(...imageUrls.map((url) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("message", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("img", { src: url }) })));
@@ -390,7 +394,7 @@ function apply(ctx, config) {
390
394
  for (const data of tweetList) {
391
395
  if (!config.enableReTweet && data.retweetedTweet)
392
396
  continue;
393
- if ((/* @__PURE__ */ new Date()).getTime() - new Date(data.createdAt).getTime() > 60 * 60 * 1e3)
397
+ if ((/* @__PURE__ */ new Date()).getTime() - new Date(data.createdAt).getTime() > 2 * 60 * 60 * 1e3)
394
398
  continue;
395
399
  if ((await ctx.database.get("nitter_records", { id: data.id })).length > 0)
396
400
  continue;
@@ -412,6 +416,7 @@ function apply(ctx, config) {
412
416
  }
413
417
  const page = await puppeteer.page();
414
418
  try {
419
+ await page.setCacheEnabled(false);
415
420
  const tweetUrl = `${config.nitterUrl}/i/status/${tweetId}`;
416
421
  await retry(3, async () => {
417
422
  await page.goto(tweetUrl);
package/lib/retry.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function retry<T>(retries: number, fn: () => Promise<T> | T, delay?: number): Promise<T>;
@@ -1,5 +1,4 @@
1
1
  import { Page } from 'puppeteer-core';
2
- export declare function retry(retries: number, fn: () => any, delay?: number): any;
3
2
  export declare function setGoogleTranslate(key: string, proxy: string): void;
4
3
  export declare function setOpenAiTranslate(url: string, key: string, model: string, prompt: string, temperature: number, timeout?: number): void;
5
4
  export declare function addTranslate(page: Page, className: string): Promise<void>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-nitter",
3
3
  "description": "使用Rettiwt-API订阅推文,并使用nitter渲染",
4
- "version": "0.0.8",
4
+ "version": "0.0.9",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -19,11 +19,11 @@
19
19
  "axios": "^1.12.2",
20
20
  "https-proxy-agent": "^7.0.6",
21
21
  "koishi": "^4.18.9",
22
- "koishi-plugin-subscription": "^0.0.5"
22
+ "koishi-plugin-subscription": "^0.0.6"
23
23
  },
24
24
  "dependencies": {
25
25
  "fluent-ffmpeg": "^2.1.3",
26
26
  "node-cron": "^4.2.1",
27
- "rettiwt-api": "^6.1.3"
27
+ "rettiwt-api": "^6.1.7"
28
28
  }
29
29
  }