def get_followers(client: ApiClient, user_id: str, limit: int) -> typing.List[models.User]: """Retrieve the first x amount of followers from an account. If the `user_id` is not known, use `instauto.helpers.search.get_user_id_from_username` to convert a username to user_id. Args: client: your ApiClient user_id: the user_id of the account to retrieve followers from limit: the maximum amount of followers to retrieve Returns: A list containing Instagram user objects (examples/objects/user.json). """ obj = GetFollowers(user_id) obj, result = client.followers_get(obj) followers = [] while result and len(followers) < limit: followers.extend(result.json()["users"]) logger.info("Retrieved {} followers, {} more to go.".format( len(followers), limit - len(followers))) obj, result = client.followers_get(obj) return [ models.User.parse(f) for f in followers[:min(len(followers), limit)] ]
def get_followers(client: ApiClient, user_id: str, username: str, limit: int) -> typing.List[models.User]: """Retrieve the first x amount of followers from an account. Either `user_id` or `username` need to be provided. If both are provided, the user_id takes precedence. Args: client: your ApiClient user_id: the user_id of the account to retrieve followers from username: the username of the account to retrieve followers from limit: the maximum amount of followers to retrieve Returns: A list containing Instagram user objects (examples/objects/user.json). """ if user_id is None and username is not None: user_id = get_user_id_from_username(client, username) if user_id is None: raise ValueError("Both `user_id` and `username` are not provided.") obj = GetFollowers(user_id) obj, result = client.followers_get(obj) followers = [] while result and len(followers) < limit: followers.extend(result.json()["users"]) logger.info("Retrieved {} followers, {} more to go.".format( len(followers), limit - len(followers))) obj, result = client.followers_get(obj) return [ models.User.parse(f) for f in followers[:min(len(followers), limit)] ]
def get_followers(client: ApiClient, user_id: str, limit: int) -> typing.List[dict]: obj = GetFollowers(user_id) obj, result = client.followers_get(obj) followers = [] while result and len(followers) < limit: followers.extend(result.json()["users"]) logger.info("Retrieved {} followers, {} more to go.".format( len(followers), limit - len(followers))) obj, result = client.followers_get(obj) return followers[:min(len(followers), limit)]
def get_followers(client: ApiClient, limit: int, user_id: Optional[int] = None, username: Optional[str] = None) -> List[models.User]: """Retrieve the first x amount of followers from an account. Either `user_id` or `username` need to be provided. If both are provided, the user_id takes precedence. Args: client: your ApiClient user_id: the user_id of the account to retrieve followers from limit: the maximum amount of followers to retrieve username: the username of the account to retrieve followers from Returns: A list containing Instagram user objects (examples/objects/user.json). """ if user_id is None and username is not None: user_id = get_user_id_from_username(client, username) if user_id is None: raise ValueError("Both `user_id` and `username` are not provided.") obj = GetFollowers(user_id) obj, result = client.followers_get(obj) followers = [] while result and len(followers) < limit: # pyre-ignore[16]: the type of result is `Response` or bool, but because # of the `result is True` check, it can only be of type bool here. followers.extend(result.json()["users"]) logger.info("Retrieved {} followers, {} more to go.".format( len(followers), limit - len(followers))) obj, result = client.followers_get(obj) return [ models.User.parse(f) for f in followers[:min(len(followers), limit)] ]
import random import os from instauto.api.client import ApiClient from instauto.api.actions import friendships as fs from time import sleep if __name__ == '__main__': if os.path.isfile('./.instauto.save'): client = ApiClient.initiate_from_file('./.instauto.save') else: client = ApiClient(username=os.environ.get("INSTAUTO_USER") or "your_username", password=os.environ.get("INSTAUTO_PASS") or "your_password") client.log_in() client.save_to_disk('./.instauto.save') f = fs.GetFollowers(user_id="2283025667") obj, result = client.followers_get(f) # grabs first page while result: # paginates until all followers are extracted parsed = result.json() print(f"Extracted {len(parsed['users'])} followers") print( f"The username of the first extracted follower is {parsed['users'][0]['username']}" ) obj, result = client.followers_get(obj) sleep(random.randint(10, 60))