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

Why getFollowers print only the first 50 results? #256

Open alexduca opened 3 years ago

alexduca commented 3 years ago

I want get all my followers list but i get only the first 50 results... after that i get this error

(node:8088) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'username' of undefined at C:\Users\ducaa\Desktop\pupp\test.js:32:51 at emitUnhandledRejectionWarning (internal/process/promises.js:168:15) at processPromiseRejections (internal/process/promises.js:247:11) at processTicksAndRejections (internal/process/task_queues.js:94:32) (node:8088) TypeError: Cannot read property 'username' of undefined at C:\Users\ducaa\Desktop\pupp\test.js:32:51 (node:8088) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. at emitDeprecationWarning (internal/process/promises.js:180:11) at processPromiseRejections (internal/process/promises.js:249:13) at processTicksAndRejections (internal/process/task_queues.js:94:32)


THIS IS THE CODE

const userprompt = await prompts(questions);
var username = userprompt['username'];
var password = userprompt['password'];

const client = new Instagram({ username, password });
await client.login();

const delay = ms => new Promise(resolve => setTimeout(resolve, ms))

const userdata = await client.getUserByUsername({ username: client.credentials.username });
var myuserid = userdata['id'];
var myfollowerscount = userdata['edge_followed_by']['count'];

console.log('Count My Followers : '+myfollowerscount);

const myfollowers = await client.getFollowers({ userId: myuserid, first: myfollowerscount });  

var numb = 0;
while(numb < myfollowerscount){
    console.log(numb+1, myfollowers.data[numb]['username']);
    if(numb > 35){
        await delay(4000);
    }
    numb++;
}
console.log('secondpart');
XeraFiu-YTB commented 3 years ago

Hey, You have a variable has_next_page that inform you if you can change your page to end_cursor (next page of follower). The first parameters is limited to 50, it's the number of followers per pages.

Here it is a sinple code to list all your followers :

(async () => {
    //List of all Followers Name
    let myFollowers = []

    let myId = "myUserId"

    let followers = await client.getFollowers({ userId: myId })
    //get your followers on the first page
    let nextPage = followers.page_info.has_next_page
    //true if you have an other page of followers
    let cursor = followers.page_info.end_cursor
    //next page of follower
    addFollowersName(followers.data)

    //loop to navigate into all the pages of followers
    while(nextPage) {
        followers = await client.getFollowers({ userId: id, first: 50, after: cursor})
        nextPage = followers.page_info.has_next_page
        cursor = followers.page_info.end_cursor
        addFollowersName(followers.data)
    }

    console.log(`List of all my Followers : ${myFollowers.join("\n- ")}`)
})