Explorar el Código

backport typing effort from branch 'mediaonly'

Mike L hace 4 años
padre
commit
d157c91667
Se han modificado 8 ficheros con 53 adiciones y 55 borrados
  1. 1 1
      dist/command.js
  2. 1 1
      dist/main.js
  3. 22 21
      dist/twitter.js
  4. 1 1
      src/command.ts
  5. 1 1
      src/main.ts
  6. 1 1
      src/model.d.ts
  7. 25 28
      src/twitter.ts
  8. 1 1
      src/webshot.ts

+ 1 - 1
dist/command.js

@@ -61,7 +61,7 @@ https://twitter.com/rikakomoe/lists/lovelive`;
         lock.feed.push(link);
     if (!lock.threads[link]) {
         lock.threads[link] = {
-            offset: 0,
+            offset: '0',
             subscribers: [],
             updatedAt: '',
         };

+ 1 - 1
dist/main.js

@@ -115,7 +115,7 @@ else {
     }
 }
 Object.keys(lock.threads).forEach(key => {
-    lock.threads[key].offset = -1;
+    lock.threads[key].offset = '-1';
 });
 const qq = new mirai_1.default({
     access_token: config.mirai_access_token,

+ 22 - 21
dist/twitter.js

@@ -34,9 +34,10 @@ class default_1 {
                 this.work();
                 return;
             }
-            logger.debug(`pulling feed ${lock.feed[lock.workon]}`);
+            const currentFeed = lock.feed[lock.workon];
+            logger.debug(`pulling feed ${currentFeed}`);
             const promise = new Promise(resolve => {
-                let match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
+                let match = currentFeed.match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
                 let config;
                 let endpoint;
                 if (match) {
@@ -48,7 +49,7 @@ class default_1 {
                     endpoint = 'lists/statuses';
                 }
                 else {
-                    match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)/);
+                    match = currentFeed.match(/https:\/\/twitter.com\/([^\/]+)/);
                     if (match) {
                         config = {
                             screen_name: match[1],
@@ -59,20 +60,20 @@ class default_1 {
                     }
                 }
                 if (endpoint) {
-                    const offset = lock.threads[lock.feed[lock.workon]].offset;
+                    const offset = lock.threads[currentFeed].offset;
                     if (offset > 0)
                         config.since_id = offset;
                     this.client.get(endpoint, config, (error, tweets, response) => {
                         if (error) {
                             if (error instanceof Array && error.length > 0 && error[0].code === 34) {
-                                logger.warn(`error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
-                                lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
-                                    logger.info(`sending notfound message of ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
-                                    this.bot.sendTo(subscriber, `链接 ${lock.feed[lock.workon]} 指向的用户或列表不存在,请退订。`).catch();
+                                logger.warn(`error on fetching tweets for ${currentFeed}: ${JSON.stringify(error)}`);
+                                lock.threads[currentFeed].subscribers.forEach(subscriber => {
+                                    logger.info(`sending notfound message of ${currentFeed} to ${JSON.stringify(subscriber)}`);
+                                    this.bot.sendTo(subscriber, `链接 ${currentFeed} 指向的用户或列表不存在,请退订。`).catch();
                                 });
                             }
                             else {
-                                logger.error(`unhandled error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
+                                logger.error(`unhandled error on fetching tweets for ${currentFeed}: ${JSON.stringify(error)}`);
                             }
                             resolve();
                         }
@@ -82,16 +83,20 @@ class default_1 {
                 }
             });
             promise.then((tweets) => {
-                logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${lock.feed[lock.workon]}`);
+                logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${currentFeed}`);
+                const currentThread = lock.threads[currentFeed];
+                const updateDate = () => currentThread.updatedAt = new Date().toString();
                 if (!tweets || tweets.length === 0) {
-                    lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
+                    updateDate();
                     return;
                 }
-                if (lock.threads[lock.feed[lock.workon]].offset === -1) {
-                    lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
+                const topOfFeed = tweets[0].id_str;
+                const updateOffset = () => currentThread.offset = topOfFeed;
+                if (currentThread.offset === '-1') {
+                    updateOffset();
                     return;
                 }
-                if (lock.threads[lock.feed[lock.workon]].offset === 0)
+                if (currentThread.offset === '0')
                     tweets.splice(1);
                 const maxCount = 3;
                 let sendTimeout = 10000;
@@ -109,8 +114,8 @@ class default_1 {
                     }
                 };
                 const sendTweets = (msg, text, author) => {
-                    lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
-                        logger.info(`pushing data of thread ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
+                    currentThread.subscribers.forEach(subscriber => {
+                        logger.info(`pushing data of thread ${currentFeed} to ${JSON.stringify(subscriber)}`);
                         const retry = (reason, count) => {
                             if (count <= maxCount)
                                 sendTimeout *= (count + 2) / (count + 1);
@@ -133,11 +138,7 @@ class default_1 {
                         this.bot.sendTo(subscriber, msg, sendTimeout).catch(error => retry(error, 1));
                     });
                 };
-                return this.webshot(tweets, sendTweets, this.webshotDelay)
-                    .then(() => {
-                    lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
-                    lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
-                });
+                return this.webshot(tweets, sendTweets, this.webshotDelay).then(updateDate).then(updateOffset);
             })
                 .then(() => {
                 lock.workon++;

+ 1 - 1
src/command.ts

@@ -61,7 +61,7 @@ https://twitter.com/rikakomoe/lists/lovelive`;
   if (!flag) lock.feed.push(link);
   if (!lock.threads[link]) {
     lock.threads[link] = {
-      offset: 0,
+      offset: '0',
       subscribers: [],
       updatedAt: '',
     };

+ 1 - 1
src/main.ts

@@ -122,7 +122,7 @@ if (fs.existsSync(path.resolve(config.lockfile))) {
 }
 
 Object.keys(lock.threads).forEach(key => {
-  lock.threads[key].offset = -1;
+  lock.threads[key].offset = '-1';
 });
 
 const qq = new QQBot({

+ 1 - 1
src/model.d.ts

@@ -16,7 +16,7 @@ interface ILock {
   threads: {
     [key: string]:
       {
-        offset: number,
+        offset: string,
         updatedAt: string,
         subscribers: IChat[],
       }

+ 25 - 28
src/twitter.ts

@@ -33,7 +33,6 @@ interface ITweet {
   extended_entities: ExtendedEntities;
   full_text: string;
   display_text_range: [number, number];
-  id: number;
   id_str: string;
   retweeted_status?: Tweet;
 }
@@ -98,10 +97,11 @@ export default class {
       return;
     }
 
-    logger.debug(`pulling feed ${lock.feed[lock.workon]}`);
+    const currentFeed = lock.feed[lock.workon];
+    logger.debug(`pulling feed ${currentFeed}`);
 
     const promise = new Promise(resolve => {
-      let match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
+      let match = currentFeed.match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
       let config: any;
       let endpoint: string;
       if (match) {
@@ -112,7 +112,7 @@ export default class {
         };
         endpoint = 'lists/statuses';
       } else {
-        match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)/);
+        match = currentFeed.match(/https:\/\/twitter.com\/([^\/]+)/);
         if (match) {
           config = {
             screen_name: match[1],
@@ -124,18 +124,18 @@ export default class {
       }
 
       if (endpoint) {
-        const offset = lock.threads[lock.feed[lock.workon]].offset;
+        const offset = lock.threads[currentFeed].offset as unknown as number;
         if (offset > 0) config.since_id = offset;
         this.client.get(endpoint, config, (error, tweets, response) => {
           if (error) {
             if (error instanceof Array && error.length > 0 && error[0].code === 34) {
-              logger.warn(`error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
-              lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
-                logger.info(`sending notfound message of ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
-                this.bot.sendTo(subscriber, `链接 ${lock.feed[lock.workon]} 指向的用户或列表不存在,请退订。`).catch();
+              logger.warn(`error on fetching tweets for ${currentFeed}: ${JSON.stringify(error)}`);
+              lock.threads[currentFeed].subscribers.forEach(subscriber => {
+                logger.info(`sending notfound message of ${currentFeed} to ${JSON.stringify(subscriber)}`);
+                this.bot.sendTo(subscriber, `链接 ${currentFeed} 指向的用户或列表不存在,请退订。`).catch();
               });
             } else {
-              logger.error(`unhandled error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
+              logger.error(`unhandled error on fetching tweets for ${currentFeed}: ${JSON.stringify(error)}`);
             }
             resolve();
           } else resolve(tweets);
@@ -143,17 +143,18 @@ export default class {
       }
     });
 
-    promise.then((tweets: any) => {
-      logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${lock.feed[lock.workon]}`);
-      if (!tweets || tweets.length === 0) {
-        lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
-        return;
-      }
-      if (lock.threads[lock.feed[lock.workon]].offset === -1) {
-        lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
-        return;
-      }
-      if (lock.threads[lock.feed[lock.workon]].offset === 0) tweets.splice(1);
+    promise.then((tweets: Tweets) => {
+      logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${currentFeed}`);
+      const currentThread = lock.threads[currentFeed];
+
+      const updateDate = () => currentThread.updatedAt = new Date().toString();
+      if (!tweets || tweets.length === 0) { updateDate(); return; }
+
+      const topOfFeed = tweets[0].id_str;
+      const updateOffset = () => currentThread.offset = topOfFeed;
+
+      if (currentThread.offset === '-1') { updateOffset(); return; }
+      if (currentThread.offset === '0') tweets.splice(1);
 
       const maxCount = 3;
       let sendTimeout = 10000;
@@ -171,8 +172,8 @@ export default class {
         }
       };
       const sendTweets = (msg: MessageChain, text: string, author: string) => {
-        lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
-          logger.info(`pushing data of thread ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
+        currentThread.subscribers.forEach(subscriber => {
+          logger.info(`pushing data of thread ${currentFeed} to ${JSON.stringify(subscriber)}`);
           const retry = (reason, count: number) => { // workaround for https://github.com/mamoe/mirai/issues/194
             if (count <= maxCount) sendTimeout *= (count + 2) / (count + 1);
             setTimeout(() => {
@@ -193,11 +194,7 @@ export default class {
           this.bot.sendTo(subscriber, msg, sendTimeout).catch(error => retry(error, 1));
         });
       };
-      return (this.webshot as any)(tweets, sendTweets, this.webshotDelay)
-      .then(() => {
-        lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
-        lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
-      });
+      return this.webshot(tweets, sendTweets, this.webshotDelay).then(updateDate).then(updateOffset);
     })
       .then(() => {
         lock.workon++;

+ 1 - 1
src/webshot.ts

@@ -30,7 +30,7 @@ const logger = getLogger('webshot');
 const mkdirP = dir => { if (!existsSync(dir)) mkdirSync(dir, {recursive: true}); };
 const baseName = path => path.split(/[/\\]/).slice(-1)[0];
 
-class Webshot extends CallableInstance<[number], Promise<void>> {
+class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Promise<void>> {
 
   private browser: Browser;
   private outDir: string;