dilame / instagram-private-api

NodeJS Instagram private API SDK. Written in TypeScript.
MIT License
5.95k stars 1.14k forks source link

How to get all followers of a user with a lot of followers? #1317

Open GuilhermeCunha opened 3 years ago

GuilhermeCunha commented 3 years ago

Form

Put an [x] if you meet the condition, else leave [ ].

Question

I would like to get all the followers of a user, but as there are many, I think the only alternative would be to split requests between multiple accounts.

For example, one account would have followers from 0 to 10,000, while another would have followers between 10,000 and 20,000. But I couldn't find any way to do this in the documentation, is it possible?

Code

async function getFollowers(
  username: string,
  ig: IgApiClient,
  { delay, limit, savePath }: GetFollowersOptions
): Promise<AccountFollowersFeedResponseUsersItem[]> {

  const id = await ig.user.getIdByUsername(username);
  const followersFeed = await ig.feed.accountFollowers(id);

  let followers: AccountFollowersFeedResponseUsersItem[] = [];
  let reqId = 0;
  do {
    reqId += 1;
    const items = await followersFeed.items();
    followers = [...followers, ...items];

    if (limit && limit !== -1) {
      if (followers.length >= limit) {
        break;
      }
    }

    await waitInSeconds(delay);
  } while (followersFeed.isMoreAvailable());
return followers
}
xeinebiu commented 3 years ago

const followersResponse = ig.feed.accountFollowers('128129999060');

// @ts-ignore followersResponse.nextMaxId = 'QVFBU1V3UlJuMUFIczVnVElZWDRnZ=='; // you can always save the latest "nextMaxId" hash and when you get 429, use the saved hash next time as checkpoint.

cagataykula commented 3 years ago

const followersResponse = ig.feed.accountFollowers('128129999060');

// @ts-ignore followersResponse.nextMaxId = 'QVFBU1V3UlJuMUFIczVnVElZWDRnZ=='; // you can always save the latest "nextMaxId" hash and when you get 429, use the saved hash next time as checkpoint.

Can you write an example about it? I don't get it.

tupizz commented 3 years ago

Using an observable and subscribing to it do you have the same problem?

tupizz commented 3 years ago
ig.feed.user(pageId).items$.subscribe(
    async (medias) => {
      console.log(`[ waiting ] - Inserindo ${medias.length}`);

      await Promise.all(
        medias.map(factoryCompetitorsMedia).map((media) => repository.save(media).catch(console.error))
      );
    },
    (error) => console.log(chalk.red(`[❌] -  ${error}`)),
    () => console.log(chalk.green(`[✅] -  Rotina finalizada`))
  );
khanakhun commented 3 years ago

Form

Put an [x] if you meet the condition, else leave [ ].

Question

I would like to get all the followers of a user, but as there are many, I think the only alternative would be to split requests between multiple accounts.

For example, one account would have followers from 0 to 10,000, while another would have followers between 10,000 and 20,000. But I couldn't find any way to do this in the documentation, is it possible?

Code

async function getFollowers(
  username: string,
  ig: IgApiClient,
  { delay, limit, savePath }: GetFollowersOptions
): Promise<AccountFollowersFeedResponseUsersItem[]> {

  const id = await ig.user.getIdByUsername(username);
  const followersFeed = await ig.feed.accountFollowers(id);

  let followers: AccountFollowersFeedResponseUsersItem[] = [];
  let reqId = 0;
  do {
    reqId += 1;
    const items = await followersFeed.items();
    followers = [...followers, ...items];

    if (limit && limit !== -1) {
      if (followers.length >= limit) {
        break;
      }
    }

    await waitInSeconds(delay);
  } while (followersFeed.isMoreAvailable());
return followers
}

Hi , Can you please me Guide me if you were able to achieve this , because I'm also trying to Scrape followers data

iitsuraj commented 3 years ago

You can use recursion Like this @khanakhun

  const id = await ig.user.getIdByUsername(username);
  const followersFeed = await ig.feed.accountFollowers(id);
  const followers = [];
  const e = async () => {
    let items = await followersFeed.items();
    // Save to database or append in json file
    followers.push(items);
    const isMore = await followersFeed.isMoreAvailable();
      if (isMore) {
        await delay(20 * 1000)  // delay 20 sec anti ban mode
        e();
      } else {
        console.log("=====> All Followers Extracted");
      }
  };
  await e()
};
iitsuraj commented 3 years ago

Duplicate #988