Skip to content

Commit

Permalink
Added env example and unfollow users who aren't following you example (
Browse files Browse the repository at this point in the history
…#1452)

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

* Update .env.example

Co-authored-by: nerix <nero.9@hotmail.de>

* Update examples/unfollow-users.example.ts

Co-authored-by: nerix <nero.9@hotmail.de>

* Update examples/unfollow-users.example.ts

Co-authored-by: nerix <nero.9@hotmail.de>

Co-authored-by: nerix <nero.9@hotmail.de>
  • Loading branch information
sell and Nerixyz authored Jun 15, 2021
1 parent 5ef32a7 commit d914b59
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
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;
}

0 comments on commit d914b59

Please sign in to comment.