Browse Source

more rename handling, better webshot timeout logic

Mike L 3 years ago
parent
commit
87e9dcc99a
4 changed files with 21 additions and 19 deletions
  1. 1 1
      dist/twitter.js
  2. 9 8
      dist/webshot.js
  3. 1 1
      src/twitter.ts
  4. 10 9
      src/webshot.ts

+ 1 - 1
dist/twitter.js

@@ -532,7 +532,7 @@ class default_1 {
             const lazyMedia = this.lazyGetMediaById(urlSegmentToId(segmentId));
             return lazyMedia.item().then(mediaItem => {
                 const lock = this.lock;
-                const feed = linkBuilder({ userName: mediaItem.user.username });
+                const feed = Object.keys(lock.threads).find(key => lock.threads[key].id === mediaItem.user.pk);
                 if (lock.feed.includes(feed) && lock.threads[feed].offset < mediaItem.pk) {
                     logger.info(`post is newer than last offset of thread (${(0, instagram_id_to_url_segment_1.instagramIdToUrlSegment)(lock.threads[feed].offset)}), updating...`);
                     this.workOnFeed(feed);

+ 9 - 8
dist/webshot.js

@@ -75,26 +75,27 @@ class Webshot extends CallableInstance {
                 const zoomFactor = 2;
                 logger.info(`shooting ${width}*${height} webshot for ${url}`);
                 this.performOnNewPage(page => {
-                    const startTime = new Date().getTime();
+                    let startTime;
+                    let gotoUrl = () => {
+                        startTime = new Date().getTime();
+                        return page.goto(url, { waitUntil: 'load', timeout: getTimeout() });
+                    };
                     const getTimerTime = () => new Date().getTime() - startTime;
                     const getTimeout = () => twitter_1.WebshotHelpers.isWaitingForLogin ? 0 : Math.max(500, webshotDelay - getTimerTime());
                     page.setViewportSize({
                         width: width / zoomFactor,
                         height: height / zoomFactor,
                     }).then(() => page.context().addCookies(this.getCookies()))
-                        .then(() => page.goto(url, { waitUntil: 'load', timeout: getTimeout() }))
+                        .then(gotoUrl)
                         .then(() => twitter_1.WebshotHelpers.handleCookieConsent(page))
                         .then(() => ((next) => Promise.race([
-                        twitter_1.WebshotHelpers.handleLogin(page)
-                            .then(() => page.goto(url, { waitUntil: 'load', timeout: getTimeout() }))
-                            .then(next),
-                        next(),
-                    ]))(() => (0, util_1.promisify)(setTimeout)(2000).then(() => page.waitForSelector('article', { timeout: getTimeout() }))))
+                        twitter_1.WebshotHelpers.handleLogin(page).then(gotoUrl).then(next),
+                        (0, util_1.promisify)(setTimeout)(2000).then(next),
+                    ]))(() => page.waitForSelector('article', { timeout: getTimeout() })))
                         .catch((err) => {
                         if (err.name !== 'TimeoutError')
                             throw err;
                         logger.warn(`navigation timed out at ${getTimerTime()} ms`);
-                        return null;
                     })
                         .then(() => page.addStyleTag({ content: 'nav,footer,main>*>*+*,header div>[class$=" "]>[class]+div,main button,canvas,main section,main section+div>ul>:not(div),' +
                             'main section+div>ul>div [role="button"],section+div h2,article>*>:first-child+*>*>:first-child+*{display:none!important} ' +

+ 1 - 1
src/twitter.ts

@@ -438,7 +438,7 @@ export default class {
       const lazyMedia = this.lazyGetMediaById(urlSegmentToId(segmentId));
       return lazyMedia.item().then(mediaItem => {
         const lock = this.lock;
-        const feed = linkBuilder({userName: mediaItem.user.username});
+        const feed = Object.keys(lock.threads).find(key => lock.threads[key].id === mediaItem.user.pk);
         if (lock.feed.includes(feed) && lock.threads[feed].offset < mediaItem.pk) {
           logger.info(`post is newer than last offset of thread (${idToUrlSegment(lock.threads[feed].offset)}), updating...`);
           this.workOnFeed(feed);

+ 10 - 9
src/webshot.ts

@@ -110,27 +110,28 @@ class Webshot extends CallableInstance<[LazyMediaItem[], (...args) => void, numb
       logger.info(`shooting ${width}*${height} webshot for ${url}`);
       this.performOnNewPage(
         page => {
-          const startTime = new Date().getTime();
+          let startTime: number;
+          let gotoUrl = () => {
+            startTime = new Date().getTime();
+            return page.goto(url, {waitUntil: 'load', timeout: getTimeout()});
+          };
           const getTimerTime = () => new Date().getTime() - startTime;
           const getTimeout = () => WebshotHelpers.isWaitingForLogin ? 0 : Math.max(500, webshotDelay - getTimerTime());
           page.setViewportSize({
             width: width / zoomFactor,
             height: height / zoomFactor,
           }).then(() => page.context().addCookies(this.getCookies()))
-            .then(() => page.goto(url, {waitUntil: 'load', timeout: getTimeout()}))
+            .then(gotoUrl)
             .then(() => WebshotHelpers.handleCookieConsent(page))
             .then(() =>
               (<T>(next: () => Promise<T>) => Promise.race([
-                WebshotHelpers.handleLogin(page)
-                  .then(() => page.goto(url, {waitUntil: 'load', timeout: getTimeout()}))
-                  .then(next),
-                next(),
-              ]))(() => promisify(setTimeout)(2000).then(() => page.waitForSelector('article', {timeout: getTimeout()})))
+                WebshotHelpers.handleLogin(page).then(gotoUrl).then(next),
+                promisify(setTimeout)(2000).then(next),
+              ]))(() => page.waitForSelector('article', {timeout: getTimeout()}))
             )
-            .catch((err: Error): Promise<puppeteer.ElementHandle<Element> | null> => {
+            .catch((err: Error) => {
               if (err.name !== 'TimeoutError') throw err;
               logger.warn(`navigation timed out at ${getTimerTime()} ms`);
-              return null;
             })
             // hide header, footer, "more options" button, like and share count, avatar stories/live indicator
             // person tags, carousel navigator, 2nd avatar, and comments; adjust layout for center alignment