Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added env example and unfollow users who aren't following you example #1452

Merged
merged 4 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
IG_USERNAME = null
IG_PASSWORD = null
# Uncomment this to set a proxy
# IG_PROXY=
50 changes: 50 additions & 0 deletions examples/unfollow-users.example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* tslint:disable:no-console */
/*
This is an example of unfollowing users who aren't following you back.
Thanks to the developers for this great package.
*/
import 'dotenv/config';
import { IgApiClient, Feed } from '../src';

const ig = new IgApiClient();

ig.state.generateDevice(process.env.IG_USERNAME);

(async () => {
await ig.account.login(process.env.IG_USERNAME, process.env.IG_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);
// Making a new map of users username that follow you.
const followersUsername = new Set(followers.map(({ username }) => username));
// Filtering through the ones who aren't following you.
const notFollowingYou = following.filter(({ username }) => !followersUsername.has(username));
// Looping through and unfollowing each user
for (const user of notFollowingYou) {
await ig.friendship.destroy(user.pk);
console.log(`unfollowed ${user.username}`);
/*
Time, is the delay which is between 1 second and 7 seconds.
Creating a promise to stop the loop to avoid api spam
*/
const time = Math.round(Math.random() * 6000) + 1000;
await new Promise(resolve => setTimeout(resolve, time));
}
})();

/**
* Source: https://github.com/dilame/instagram-private-api/issues/969#issuecomment-551436680
* @param feed
* @returns All items from the feed
*/

async function getAllItemsFromFeed<T>(feed: Feed<any, T>): Promise<T[]> {
let items = [];
do {
items = items.concat(await feed.items());
} while (feed.isMoreAvailable());
return items;
}