Browse Source

make csrf token refresh on-demand

Mike L 2 years ago
parent
commit
a4077fd090
4 changed files with 12 additions and 4 deletions
  1. 1 1
      dist/twitter.js
  2. 5 1
      dist/wiki.js
  3. 1 1
      src/twitter.ts
  4. 5 1
      src/wiki.ts

+ 1 - 1
dist/twitter.js

@@ -37,7 +37,7 @@ const processTweetBody = (tweet) => {
         .replace(/((?<=\s)#.*?\s+)+$/g, '')
         .trim()
         .match(/(.*?)\n\n(.*)/s).slice(1);
-    const dateMatch = /(\d+\/\d+)\(.\).*(?:から|~)\n/.exec(body);
+    const dateMatch = /(\d+\/\d+)\(.\).*(?:から|~)\s*\n/.exec(body);
     const date = (dateMatch ?
         new Date(`${new Date(tweet.created_at).getFullYear()}/${dateMatch[1]}`) :
         new Date(tweet.created_at))

+ 5 - 1
dist/wiki.js

@@ -34,7 +34,6 @@ class default_1 {
                 .then(uid => {
                 logger.info(`finished logging in via browser, wiki username: ${uid}`);
                 this.bot.fetch = fetchCookie(node_fetch_1.default, jar);
-                setInterval(this.bot.updateCsrfToken, 43200000);
                 return browser.close();
             })
                 .catch((err) => browser.close().then(() => {
@@ -206,6 +205,11 @@ ${fileNames.map(fileName => `[[文件:${fileName}|无框|左]]`).join('\n')}
         };
         this.bot = new mediawiki2_1.MWBot(`${baseUrl}/api.php`);
         this.lock = lock;
+        const updateCsrfToken = this.bot.updateCsrfToken.bind(this.bot);
+        this.bot.updateCsrfToken = (function () {
+            setTimeout(() => { this.lastCsrfToken = undefined; }, 7200000);
+            return updateCsrfToken();
+        }).bind(this.bot);
     }
 }
 exports.default = default_1;

+ 1 - 1
src/twitter.ts

@@ -62,7 +62,7 @@ export const processTweetBody = (tweet: Tweet) => {
     .replace(/((?<=\s)#.*?\s+)+$/g, '')
     .trim()
     .match(/(.*?)\n\n(.*)/s).slice(1);
-    const dateMatch = /(\d+\/\d+)\(.\).*(?:から|~)\n/.exec(body);
+    const dateMatch = /(\d+\/\d+)\(.\).*(?:から|~)\s*\n/.exec(body);
     const date = (dateMatch ?
       new Date(`${new Date(tweet.created_at).getFullYear()}/${dateMatch[1]}`) :
       new Date(tweet.created_at)

+ 5 - 1
src/wiki.ts

@@ -21,6 +21,11 @@ export default class {
     constructor(lock: ILock) {
       this.bot = new MWBot(`${baseUrl}/api.php`);
       this.lock = lock;
+      const updateCsrfToken = this.bot.updateCsrfToken.bind(this.bot);
+      this.bot.updateCsrfToken = (function () {
+        setTimeout(() => { (this as any).lastCsrfToken = undefined; }, 7200000);
+        return updateCsrfToken();
+      }).bind(this.bot);
     }
     
     public login = (sessdata: string) =>
@@ -47,7 +52,6 @@ export default class {
             .then(uid => {
               logger.info(`finished logging in via browser, wiki username: ${uid}`);
               this.bot.fetch = (fetchCookie as any)(nodeFetch, jar);
-              setInterval(this.bot.updateCsrfToken, 43200000);
               return browser.close();
             })
             .catch((err: Error) => browser.close().then(() => {