jlobos / instagram-web-api

🤳 Instagram Private Web API client for Node
https://npmjs.com/instagram-web-api
MIT License
1.12k stars 185 forks source link

How to get All Followers/Followings? #92

Open mkgerasimenko opened 5 years ago

mkgerasimenko commented 5 years ago

Hello. Thank you for this project!

I have a problem with getting all followers/followings. I can catch 50 users(by default) from my account, but I need all.

This is my code:

public async getFollowings() {
       const followings = await this.client.getFollowings({
            userId: await this.getUserId(),
            first: 50
        });
        return await followings.data;
    }

I don't understand what I should do with after param. When I try to resolve end_cursor - I'm getting null :( For example:

const user = await this.client.getUserByUsername({
    username: this.username
});
const nextPageToken =  await user.edge_owner_to_timeline_media.page_info.end_cursor;

nextPageToken is null.

Could you please provide me information about: how to get all followers/followings. Thank you!

EeeDotZee commented 5 years ago

The end_cusor is part of the return value you get from the getFollowings function. You could call it in a loop to get more then 50 followers. Be aware that you will get a 429 response if you call that function too many times.

  var UserToFind;
  var idsToLike = [];
  var followersOfUser = [];
  var end_cursor;

  await client.getUserByUsername({ username: 'instagram' }).then(user =>{
    UserToFind = user.id;
  })

  while(followersOfUser.length <= 500){
    await client.getFollowers({ userId: UserToFind, first: 50, after: end_cursor }).then(followers => {
      followers.data.forEach(function(value){
        followersOfUser.push(value.id);
      });
      end_cursor = followers.page_info.end_cursor;
      sleep(2000);
  })
  }

This will add 550 followers of the user instagram to an array. I'm sure there are cleaner ways to do this, but it does the job. The sleep() function helps to avoid an 429 response (you can use a timeout or whatever instead)

apuzyrevsky commented 3 years ago

@EeeDotZee thanks for the solution, just encountered the same issue