Browse Source

fix threshold

Mike L 3 years ago
parent
commit
0501482443
5 changed files with 18 additions and 23 deletions
  1. 1 1
      dist/command.js
  2. 6 6
      dist/twitter.js
  3. 4 9
      package.json
  4. 1 1
      src/command.ts
  5. 6 6
      src/twitter.ts

+ 1 - 1
dist/command.js

@@ -27,7 +27,7 @@ function view(_chat, args, reply) {
     let query = Number(args[0]);
     if (args[0] && !Number.isInteger(query))
         return reply('查询格式有误。\n格式:/nanatsuu_view [〈整数〉]');
-    twitter_1.queryByRegExp('t7s_staff', /今週の『それゆけ!ナナスタ☆通信』第(\d+)話はこちら!/, 360)
+    twitter_1.queryByRegExp('t7s_staff', /今週の『それゆけ!ナナスタ☆通信』第(\d+)話はこちら!/, 3600 * 24 * 7)
         .then(match => {
         if (!match)
             throw Error();

+ 6 - 6
dist/twitter.js

@@ -9,7 +9,7 @@ const snowflake = (epoch) => Number.isNaN(epoch) ? undefined :
     utils_1.BigNumOps.lShift(String(epoch - 1 - TWITTER_EPOCH), 22);
 exports.snowflake = snowflake;
 const logger = loggers_1.getLogger('twitter');
-let queryByRegExp = (username, regexp, cacheSeconds, until) => Promise.resolve(null);
+let queryByRegExp = (username, regexp, queryThreshold, until) => Promise.resolve(null);
 exports.queryByRegExp = queryByRegExp;
 class default_1 {
     constructor(opt) {
@@ -69,14 +69,14 @@ class default_1 {
             access_token_key: opt.accessTokenKey,
             access_token_secret: opt.accessTokenSecret,
         });
-        exports.queryByRegExp = (username, regexp, cacheSeconds, until) => {
+        exports.queryByRegExp = (username, regexp, queryThreshold, until) => {
             logger.info(`searching timeline of @${username} for matches of ${regexp}...`);
             const normalizedUsername = username.toLowerCase().replace(/^@/, '');
             const queryKey = `${normalizedUsername}:${regexp.toString()}`;
-            const isOld = (then) => {
+            const isOld = (then, threshold = 360) => {
                 if (!then)
                     return true;
-                return utils_1.BigNumOps.compare(exports.snowflake(Date.now() - cacheSeconds * 1000), then) >= 0;
+                return utils_1.BigNumOps.compare(exports.snowflake(Date.now() - threshold * 1000), then) >= 0;
             };
             if (queryKey in this.lastQueries && !isOld(this.lastQueries[queryKey].id)) {
                 const { match, id } = this.lastQueries[queryKey];
@@ -93,9 +93,9 @@ class default_1 {
                     return match;
                 }
                 const last = tweets.slice(-1)[0].id_str;
-                if (isOld(last))
+                if (isOld(last, queryThreshold))
                     return null;
-                exports.queryByRegExp(username, regexp, cacheSeconds, last);
+                return exports.queryByRegExp(username, regexp, queryThreshold, last);
             });
         };
     }

+ 4 - 9
package.json

@@ -33,8 +33,6 @@
     "callable-instance": "^2.0.0",
     "command-line-usage": "^5.0.5",
     "html-entities": "^1.3.1",
-    "instagram-id-to-url-segment": "github:CL-Jeremy/instagram-id-to-url-segment#built",
-    "instagram-private-api": "^1.44.1",
     "koishi": "^3.10.0",
     "koishi-adapter-onebot": "^3.0.8",
     "log4js": "^6.3.0",
@@ -42,9 +40,7 @@
     "pngjs": "^5.0.0",
     "read-all-stream": "^3.1.0",
     "sha1": "^1.1.1",
-    "sharp": "^0.25.4",
-    "socks-proxy-agent": "^5.0.0",
-    "temp": "^0.9.1",
+    "twitter": "^1.7.1",
     "typescript": "^4.2.3"
   },
   "devDependencies": {
@@ -52,9 +48,7 @@
     "@types/node": "^14.14.22",
     "@types/pngjs": "^3.4.2",
     "@types/puppeteer": "^1.5.0",
-    "@types/redis": "^2.8.6",
-    "@types/sharp": "^0.25.0",
-    "@types/temp": "^0.8.34",
+    "@types/twitter": "^1.7.0",
     "@typescript-eslint/eslint-plugin": "^4.22.0",
     "@typescript-eslint/parser": "^4.22.0",
     "eslint": "^7.25.0",
@@ -62,6 +56,7 @@
     "eslint-plugin-jsdoc": "^32.3.1",
     "eslint-plugin-prefer-arrow": "^1.2.3",
     "eslint-plugin-react": "^7.23.2",
-    "tslint-config-prettier": "^1.13.0"
+    "tslint-config-prettier": "^1.13.0",
+    "twitter-d": "^0.4.0"
   }
 }

+ 1 - 1
src/command.ts

@@ -31,7 +31,7 @@ function parseCmd(message: string): {
 function view(_chat: IChat, args: string[], reply: (msg: string) => any): void {
   let query = Number(args[0]);
   if (args[0] && !Number.isInteger(query)) return reply('查询格式有误。\n格式:/nanatsuu_view [〈整数〉]');
-  queryByRegExp('t7s_staff', /今週の『それゆけ!ナナスタ☆通信』第(\d+)話はこちら!/, 360)
+  queryByRegExp('t7s_staff', /今週の『それゆけ!ナナスタ☆通信』第(\d+)話はこちら!/, 3600 * 24 * 7)
     .then(match => {
       if (!match) throw Error();
       if (!args[0]) query = Number(match[1]);

+ 6 - 6
src/twitter.ts

@@ -39,7 +39,7 @@ interface ITweet extends TwitterTypes.Status {
 export type Tweet = ITweet;
 export type Tweets = ITweet[];
 
-export let queryByRegExp = (username: string, regexp: RegExp, cacheSeconds?: number, until?: string) =>
+export let queryByRegExp = (username: string, regexp: RegExp, queryThreshold?: number, until?: string) =>
   Promise.resolve<RegExpExecArray>(null);
 
 export default class {
@@ -54,13 +54,13 @@ export default class {
       access_token_key: opt.accessTokenKey,
       access_token_secret: opt.accessTokenSecret,
     });
-    queryByRegExp = (username, regexp, cacheSeconds?: number, until?: string) => {
+    queryByRegExp = (username, regexp, queryThreshold?, until?) => {
       logger.info(`searching timeline of @${username} for matches of ${regexp}...`);
       const normalizedUsername = username.toLowerCase().replace(/^@/, '');
       const queryKey = `${normalizedUsername}:${regexp.toString()}`;
-      const isOld = (then: string) => {
+      const isOld = (then: string, threshold = 360) => {
         if (!then) return true;
-        return BigNumOps.compare(snowflake(Date.now() - cacheSeconds * 1000), then) >= 0;
+        return BigNumOps.compare(snowflake(Date.now() - threshold * 1000), then) >= 0;
       };
       if (queryKey in this.lastQueries && !isOld(this.lastQueries[queryKey].id)) {
         const {match, id} = this.lastQueries[queryKey];
@@ -77,8 +77,8 @@ export default class {
             return match;
           }
           const last = tweets.slice(-1)[0].id_str;
-          if (isOld(last)) return null;
-          queryByRegExp(username, regexp, cacheSeconds, last);
+          if (isOld(last, queryThreshold)) return null;
+          return queryByRegExp(username, regexp, queryThreshold, last);
         });
     };
   }