|
@@ -11,14 +11,14 @@ import {
|
|
} from 'instagram-id-to-url-segment';
|
|
} from 'instagram-id-to-url-segment';
|
|
import {
|
|
import {
|
|
IgApiClient,
|
|
IgApiClient,
|
|
- IgClientError, IgExactUserNotFoundError, IgLoginTwoFactorRequiredError, IgResponseError,
|
|
|
|
|
|
+ IgClientError, IgExactUserNotFoundError, IgLoginRequiredError, IgLoginTwoFactorRequiredError, IgResponseError,
|
|
MediaInfoResponseItemsItem, UserFeedResponseItemsItem
|
|
MediaInfoResponseItemsItem, UserFeedResponseItemsItem
|
|
} from 'instagram-private-api';
|
|
} from 'instagram-private-api';
|
|
import { SocksProxyAgent } from 'socks-proxy-agent';
|
|
import { SocksProxyAgent } from 'socks-proxy-agent';
|
|
|
|
|
|
import { getLogger } from './loggers';
|
|
import { getLogger } from './loggers';
|
|
import QQBot from './koishi';
|
|
import QQBot from './koishi';
|
|
-import { Arr, BigNumOps, chainPromises, neverResolves } from './utils';
|
|
|
|
|
|
+import { Arr, BigNumOps, chainPromises, customError, neverResolves } from './utils';
|
|
import Webshot, { Cookies, Page } from './webshot';
|
|
import Webshot, { Cookies, Page } from './webshot';
|
|
|
|
|
|
const parseLink = (link: string): { userName?: string, postUrlSegment?: string } => {
|
|
const parseLink = (link: string): { userName?: string, postUrlSegment?: string } => {
|
|
@@ -425,12 +425,9 @@ export default class {
|
|
.then(responseHandler);
|
|
.then(responseHandler);
|
|
const responseHandler = (res: typeof response): ReturnType<typeof response.json> => {
|
|
const responseHandler = (res: typeof response): ReturnType<typeof response.json> => {
|
|
if (res.status() !== 200) {
|
|
if (res.status() !== 200) {
|
|
- const err = new Error(
|
|
|
|
|
|
+ throw customError('ResponseError')(
|
|
`error navigating to user page, error was: ${res.status()} ${res.statusText()}`
|
|
`error navigating to user page, error was: ${res.status()} ${res.statusText()}`
|
|
);
|
|
);
|
|
- throw Object.defineProperty(err, 'name', {
|
|
|
|
- value: 'ResponseError',
|
|
|
|
- });
|
|
|
|
}
|
|
}
|
|
return res.json()
|
|
return res.json()
|
|
.catch(redirectionHandler)
|
|
.catch(redirectionHandler)
|
|
@@ -488,6 +485,12 @@ export default class {
|
|
public queryUserMedia: (username: string, targetId?: string) => Promise<LazyMediaItem[]>;
|
|
public queryUserMedia: (username: string, targetId?: string) => Promise<LazyMediaItem[]>;
|
|
|
|
|
|
public queryUser = (username: string) => this.client.user.searchExact(username)
|
|
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}`);
|
|
.then(user => `${user.username}:${user.pk}`);
|
|
|
|
|
|
private workOnMedia = (
|
|
private workOnMedia = (
|
|
@@ -583,13 +586,13 @@ export default class {
|
|
|
|
|
|
const workon = (queuedFeeds.length - 1) - (i * 5 + j);
|
|
const workon = (queuedFeeds.length - 1) - (i * 5 + j);
|
|
fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
|
|
fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
|
|
- const promiseDelay = this.workInterval * (Math.random() + j) * 500 / lock.feed.length;
|
|
|
|
|
|
+ const promiseDelay = this.workInterval * (Math.random() + j) * 250 / lock.feed.length;
|
|
const startTime = new Date().getTime();
|
|
const startTime = new Date().getTime();
|
|
const getTimerTime = () => new Date().getTime() - startTime;
|
|
const getTimerTime = () => new Date().getTime() - startTime;
|
|
|
|
|
|
- const promise = promisify(setTimeout)(promiseDelay).then(() => {
|
|
|
|
|
|
+ const promise = promisify(setTimeout)(promiseDelay * 3).then(() => {
|
|
logger.info(`about to pull from feed #${workon}: ${currentFeed}`);
|
|
logger.info(`about to pull from feed #${workon}: ${currentFeed}`);
|
|
- if (j === arr.length - 1) logger.info(`timeout for this batch job: ${Math.trunc(promiseDelay * 2)} ms`);
|
|
|
|
|
|
+ if (j === arr.length - 1) logger.info(`timeout for this batch job: ${Math.trunc(promiseDelay)} ms`);
|
|
return this.workOnFeed(currentFeed);
|
|
return this.workOnFeed(currentFeed);
|
|
}).then(() => {
|
|
}).then(() => {
|
|
lock.workon = workon - 1;
|
|
lock.workon = workon - 1;
|
|
@@ -599,16 +602,8 @@ export default class {
|
|
fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
|
|
fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
|
|
});
|
|
});
|
|
|
|
|
|
- return Promise.race([promise, isWaitingForLogin ? neverResolves() : promisify(setTimeout)(promiseDelay * 3)]);
|
|
|
|
|
|
+ return Promise.race([promise, isWaitingForLogin ? neverResolves() : promisify(setTimeout)(promiseDelay * 4)]);
|
|
}))
|
|
}))
|
|
- ))
|
|
|
|
- .then(() => {
|
|
|
|
- let timeout = this.workInterval * 500;
|
|
|
|
- if (timeout < 1000) timeout = 1000;
|
|
|
|
- fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
|
|
|
|
- setTimeout(() => {
|
|
|
|
- this.work();
|
|
|
|
- }, timeout);
|
|
|
|
- });
|
|
|
|
|
|
+ )).then(this.work);
|
|
};
|
|
};
|
|
}
|
|
}
|