Skip to content

Commit

Permalink
feat: add retry
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgfr committed Jun 9, 2023
1 parent ee04fd2 commit 98428a9
Show file tree
Hide file tree
Showing 3 changed files with 862 additions and 1,205 deletions.
33 changes: 27 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ const questions: Array<prompts.PromptObject> = [
];

async function promptUser(): Promise<Partial<PromptResult>> {
const { username, password } = await prompts(
questions,
);
const { username, password } = await prompts(questions);
return { username, password };
}

Expand All @@ -33,8 +31,31 @@ async function processUserInformations() {
console.log('Missing informations 😭');
return;
}
const listOfUnfollowers = await getUnfollowers(username, password);
console.log(listOfUnfollowers);

let listOfUnfollowers: Array<string> | null = null;
let retryCount = 0;
const maxRetries = 3;

while (retryCount < maxRetries) {
try {
listOfUnfollowers = await getUnfollowers(
username,
password,
retryCount === 0,
);
break;
} catch (error) {
retryCount++;
console.log(`Retrying... (${retryCount}/${maxRetries})`);
}
}

if (listOfUnfollowers) {
console.log('List of unfollowers:');
console.log(listOfUnfollowers);
} else {
console.log('Failed to get unfollowers after maximum retries. 😢');
}
}

export async function runCommand() {
Expand All @@ -46,4 +67,4 @@ export async function runCommand() {
.action(() => processUserInformations());

program.parse();
}
}
15 changes: 10 additions & 5 deletions src/insta.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { IgApiClient, Feed } from 'instagram-private-api';

export async function getUnfollowers(username: string, password: string): Promise<string[]> {
export async function getUnfollowers(
username: string,
password: string,
withPreLoginFlow: boolean,
): Promise<string[]> {
const ig = new IgApiClient();
ig.state.generateDevice(username);
await ig.simulate.preLoginFlow();
if (withPreLoginFlow) await ig.simulate.preLoginFlow();
await ig.account.login(username, password);
const followersFeed = ig.feed.accountFollowers(ig.state.cookieUserId);
const followingFeed = ig.feed.accountFollowing(ig.state.cookieUserId);
const followers = await getAllItemsFromFeed(followersFeed);
const following = await getAllItemsFromFeed(followingFeed);
const followersUsername = new Set(followers.map(({ username }) => username));
const notFollowingYou = following.filter(({ username }) => !followersUsername.has(username));
const notFollowingYou = following.filter(
({ username }) => !followersUsername.has(username),
);
return notFollowingYou.map(({ username }) => username);
}

Expand All @@ -20,5 +26,4 @@ async function getAllItemsFromFeed<T>(feed: Feed<any, T>): Promise<T[]> {
items = items.concat(await feed.items());
} while (feed.isMoreAvailable());
return items;

}
}
Loading

0 comments on commit 98428a9

Please sign in to comment.