jlobos / instagram-web-api

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

How to Get a huge number of followers using getFollowers function without reaching the Server request Limit #226

Open khanakhun opened 3 years ago

khanakhun commented 3 years ago

const Instagram = require("instagram-web-api"); require('dotenv').config(); const FileCookieStore = require('tough-cookie-filestore2')

const cookieStore = new FileCookieStore('./cookies.json')

const { username, password } = process.env;

const client = new Instagram({ username, password , cookieStore });

client.login();

// to getting userId by Username const getUserId = async (username) =>{ let userId; await client.getUserByUsername({username: username}).then( user => { userId = user.id; }); return userId; }

// getting User FollowersofUser const getFollowersCount = async (username) => { let count; let userName; await client.getUserByUsername({ username:username }).then(user =>{ userName = user.id; })

await client.getFollowers({ userId: userName, first: 20, after: end_cursor }).then(followers => { count = followers.count }) return count; }

// Timer const sleep = (ms) => { return new Promise( resolve => setTimeout(resolve, ms)) };

let followersofUser = [];

// Get Followers Data const getFollowersData = async (userName) => {

let UserToFind = await getUserId(userName); let count = await getFollowersCount(userName); let end_cursor;

while(followersofUser.length <= count){ await client.getFollowers({ userId : UserToFind , first: 20 , after : end_cursor }).then(followers => { followers.data.forEach(function(value){ followersOfUser.push(value); }); end_cursor = followers.page_info.end_cursor; sleep(4000) })

}

return followersofUser; };

let followersData = getFollowersData('mira.ibiza'); console.log(followersData);

module.exports = { getFollowersData }

MonsieurBibo commented 3 years ago

Hey, as said in many previous issues, Instagram does have some rate limits for their Web API. It seems to be 200 requests/hour.

1 request every 4000 ms equals 900 requests an hour, you need to slow down your requests.