ping / instagram_private_api

A Python library to access Instagram's private API.
MIT License
3k stars 619 forks source link

user_followers pagination #342

Open master0v opened 3 years ago

master0v commented 3 years ago

Before submitting an issue, make sure you have:

Which client are you using?


Describe your Question/Issue:

Please make sure the description is worded well enough to be understood with as much context and examples as possible.

I am trying to use pagination on user_followers

rank_token = api.generate_uuid()
results = api.user_followers(user_id, rank_token)
next_max_id = results.get('next_max_id')
while next_max_id:
        results = self.api.user_followers(user_id, rank_token, max_id=next_max_id)

there is no 'next_max_id' in the results after the first call, even though I only got a subset of all followers:

users : 195 items (<class 'list'>) page_size = 200

Paste the output of python -V here: Python 3.9.1

master0v commented 3 years ago

ping to top

jcsumlin commented 3 years ago

I am able to call results =api.user_following(api.authenticated_user_id,api.generate_uuid()) And I receive a full list of my followers (over 500). If you are calling this on another user that is not yourself please make that distinction.

master0v commented 3 years ago

I my code above I am requesting my own followERs. In your code sample you call user_followING. I believe that makes a difference.

arfathyahiya commented 3 years ago
    def get_all_followers(self, username):
        rank_token = self.uuid
        print(rank_token)
        user_id = self.get_user_id(username)
        followers = []
        results = self.user_followers(user_id, rank_token)
        followers.extend(results.get("users", []))
        max_id = results.get("next_max_id")
        while max_id:
            try:
                results = self.user_followers(user_id, rank_token)
            except socket.timeout:
                time.sleep(1)
                continue
            followers.extend(results.get("users", []))
            max_id = results.get("next_max_id")
            time.sleep(0.5)
            print(len(followers))
            print(max_id)
            # results.pop("users")
            print(results)
        return followers

It always return next_max_id and exceeds the number of followers by returning same followers everytime once all followers are fetched

philipperemy commented 2 years ago
def get_all_followers(client, user_id):
    rank_token = client.uuid
    followers = []
    max_id = None
    while True:
        try:
            if max_id is not None:
                results = client.user_followers(user_id, rank_token, max_id=max_id)
            else:
                results = client.user_followers(user_id, rank_token)
        except socket.timeout:
            time.sleep(1)
            continue
        max_id = results.get("next_max_id")
        followers.extend(results['users'])
        if max_id is None:
            break
        time.sleep(0.5)
    return followers

where client = Client(user_name, password, settings=cache_settings)

achampagne1 commented 2 years ago

@philipperemy what is cache_settings?

philipperemy commented 2 years ago

@achampagne1 it's explained here: https://github.com/ping/instagram_private_api#avoiding-re-login

Here is an example on how to implement it: https://github.com/ping/instagram_private_api/issues/309

syedfaiqyazdani commented 2 years ago

how to fetch all followers of a user using instagram_private_api ? api.user_followers('322083867',api.uuid)

i get only 50 results of that user how to do pagination to get all followers of a specific user ?

808arc commented 1 year ago

Hi, did you find a solution yet?

philipperemy commented 1 year ago

@808arc read my comment it works.

808arc commented 1 year ago

@808arc read my comment it works.

Thank you! I tried. I copy and paste youre code but It didnt work one me. Look like youre code work only on self and not targeting another accounts

Can I ask you to look on this code?

from instagram_private_api import Client, ClientCompatPatch

user_name = '***'
password = '***'

api = Client(user_name, password)

from random import randint
from time import sleep

user_id = api.username_info('target')['user']['pk']

# Create a list of followers' usernames
usernames = []

next_max_id = followers.get('next_max_id')

while next_max_id:
    delay = randint(20,40)
    print("Sleep " + str(delay) + "s")
    sleep(delay)
    # Get a list of the user's followers
    followers = api.user_followers(user_id, rank_token=api.generate_uuid(),)
    next_max_id = followers.get('next_max_id')

    for follower in followers['users']:
        usernames.append(follower['username'])

# Print the list of followers' usernames
print(len(usernames))
print(usernames)
gonultasbu commented 1 year ago

The code above works, but it returns same users multiple times for other accounts for some reason. I think this has something to do with pagination.