Browse Source

reverse working order

Mike L 3 years ago
parent
commit
eb9f270c09
2 changed files with 35 additions and 33 deletions
  1. 2 2
      dist/twitter.js
  2. 33 31
      src/twitter.ts

+ 2 - 2
dist/twitter.js

@@ -314,8 +314,8 @@ class default_1 {
                     fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
                 }
             });
-            utils_1.chainPromises(utils_1.Arr.chunk(lock.feed, 5).map((arr, i) => () => Promise.all(arr.map((currentFeed, j) => {
-                lock.workon = i * 5 + j;
+            utils_1.chainPromises(utils_1.Arr.chunk(lock.feed.slice(0).reverse(), 5).map((arr, i) => () => Promise.all(arr.map((currentFeed, j) => {
+                lock.workon = (lock.feed.length - 1) - i * 5 + j;
                 fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
                 const promiseDelay = this.workInterval * (Math.random() + j) * 500 / lock.feed.length;
                 logger.debug(`timeout for this pull job: ${promiseDelay * 2}`);

+ 33 - 31
src/twitter.ts

@@ -490,44 +490,46 @@ export default class {
       }
     });
     
-    chainPromises(Arr.chunk(lock.feed, 5).map((arr, i) => () => Promise.all(arr.map((currentFeed, j) => {
-      lock.workon = i * 5 + j;
-      fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
-
-      const promiseDelay = this.workInterval * (Math.random() + j) * 500 / lock.feed.length;
-      logger.debug(`timeout for this pull job: ${promiseDelay * 2}`);
-
-      const promise = promisify(setTimeout)(promiseDelay).then(() => {
-        const match = /https:\/\/www\.instagram\.com\/([^\/]+)/.exec(currentFeed);
-        if (!match) {
-          logger.error(`current feed "${currentFeed}" is invalid, please remove this feed manually`);
-          return [] as LazyMediaItem[];
-        }
-        return this.queryUserMedia(match[1], this.lock.threads[currentFeed].offset)
-          .catch((error: Error) => {
-            logger.error(`error scraping media off profile page of ${match[1]}, error: ${error}`);
+    chainPromises(Arr.chunk(lock.feed.slice(0).reverse(), 5).map((arr, i) =>
+      () => Promise.all(arr.map((currentFeed, j) => {
+        lock.workon = (lock.feed.length - 1) - i * 5 + j;
+        fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
+
+        const promiseDelay = this.workInterval * (Math.random() + j) * 500 / lock.feed.length;
+        logger.debug(`timeout for this pull job: ${promiseDelay * 2}`);
+
+        const promise = promisify(setTimeout)(promiseDelay).then(() => {
+          const match = /https:\/\/www\.instagram\.com\/([^\/]+)/.exec(currentFeed);
+          if (!match) {
+            logger.error(`current feed "${currentFeed}" is invalid, please remove this feed manually`);
             return [] as LazyMediaItem[];
-          });
-      });
+          }
+          return this.queryUserMedia(match[1], this.lock.threads[currentFeed].offset)
+            .catch((error: Error) => {
+              logger.error(`error scraping media off profile page of ${match[1]}, error: ${error}`);
+              return [] as LazyMediaItem[];
+            });
+        });
 
-      promise.then((mediaItems: LazyMediaItem[]) => {
-        const currentThread = lock.threads[currentFeed];
+        promise.then((mediaItems: LazyMediaItem[]) => {
+          const currentThread = lock.threads[currentFeed];
 
-        const updateDate = () => currentThread.updatedAt = new Date().toString();
-        if (!mediaItems || mediaItems.length === 0) { updateDate(); return; }
+          const updateDate = () => currentThread.updatedAt = new Date().toString();
+          if (!mediaItems || mediaItems.length === 0) { updateDate(); return; }
 
-        const topOfFeed = mediaItems[0].pk;
-        const updateOffset = () => currentThread.offset = topOfFeed;
+          const topOfFeed = mediaItems[0].pk;
+          const updateOffset = () => currentThread.offset = topOfFeed;
 
-        if (currentThread.offset === '-1') { updateOffset(); return; }
-        if (currentThread.offset === '0') mediaItems.splice(1);
+          if (currentThread.offset === '-1') { updateOffset(); return; }
+          if (currentThread.offset === '0') mediaItems.splice(1);
 
-        return this.workOnMedia(mediaItems, this.sendMedia(`thread ${currentFeed}`, ...currentThread.subscribers))
-          .then(updateDate).then(updateOffset);
-      }).then(() => fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock)));
+          return this.workOnMedia(mediaItems, this.sendMedia(`thread ${currentFeed}`, ...currentThread.subscribers))
+            .then(updateDate).then(updateOffset);
+        }).then(() => fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock)));
 
-      return promisify(setTimeout)(promiseDelay * 3);
-    }))))
+        return promisify(setTimeout)(promiseDelay * 3);
+      }))
+    ))
       .then(() => {
         let timeout = this.workInterval * 500;
         if (timeout < 1000) timeout = 1000;