Browse Source

fix client re-login

Mike L 3 years ago
parent
commit
7170139fdb
2 changed files with 15 additions and 1 deletions
  1. 8 0
      dist/twitter.js
  2. 7 1
      src/twitter.ts

+ 8 - 0
dist/twitter.js

@@ -279,6 +279,14 @@ class default_1 {
             });
         };
         this.queryUser = (username) => this.client.user.searchExact(username)
+            .catch((error) => {
+            if (error instanceof instagram_private_api_1.IgLoginRequiredError) {
+                logger.warn('login required, logging in again...');
+                return this.session.login().then(() => this.client.user.searchExact(username));
+            }
+            else
+                throw error;
+        })
             .then(user => `${user.username}:${user.pk}`);
         this.workOnMedia = (lazyMediaItems, sendMedia) => this.webshot(lazyMediaItems, sendMedia, this.webshotDelay);
         this.urlSegmentToId = urlSegmentToId;

+ 7 - 1
src/twitter.ts

@@ -11,7 +11,7 @@ import {
 } from 'instagram-id-to-url-segment';
 import {
   IgApiClient,
-  IgClientError, IgExactUserNotFoundError, IgLoginTwoFactorRequiredError, IgResponseError,
+  IgClientError, IgExactUserNotFoundError, IgLoginRequiredError, IgLoginTwoFactorRequiredError, IgResponseError,
   MediaInfoResponseItemsItem, UserFeedResponseItemsItem
 } from 'instagram-private-api';
 import { SocksProxyAgent } from 'socks-proxy-agent';
@@ -472,6 +472,12 @@ export default class {
   public queryUserMedia: (username: string, targetId?: string) => Promise<LazyMediaItem[]>;
 
   public queryUser = (username: string) => this.client.user.searchExact(username)
+    .catch((error: IgClientError) => {
+      if (error instanceof IgLoginRequiredError) {
+        logger.warn('login required, logging in again...');
+        return this.session.login().then(() => this.client.user.searchExact(username));
+      } else throw error;
+    })
     .then(user => `${user.username}:${user.pk}`);
 
   private workOnMedia = (