def get_following(client: ApiClient, user_id: str, username: str, limit: int) -> typing.List[models.User]: """Retrieve the first x amount of users that an account is following. 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 following from username: the username of the account to retrieve following from limit: the maximum amount of users 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 = GetFollowing(user_id) obj, result = client.following_get(obj) following = [] while result and len(following) < limit: following.extend(result.json()["users"]) logger.info("Retrieved {} of following, {} more to go.".format( len(following), limit - len(following))) obj, result = client.following_get(obj) return [ models.User.parse(f) for f in following[:min(len(following), limit)] ]
def get_following(client: ApiClient, user_id: str, limit: int) -> typing.List[models.User]: """Retrieve the first x amount of users that follow 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 following from limit: the maximum amount of users to retrieve Returns: A list containing Instagram user objects (examples/objects/user.json). """ obj = GetFollowing(user_id) obj, result = client.following_get(obj) following = [] while result and len(following) < limit: following.extend(result.json()["users"]) logger.info("Retrieved {} of following, {} more to go.".format( len(following), limit - len(following))) obj, result = client.following_get(obj) return [ models.User.parse(f) for f in following[:min(len(following), limit)] ]
def get_following(client: ApiClient, user_id: str, limit: int) -> typing.List[dict]: obj = GetFollowing(user_id) obj, result = client.following_get(obj) following = [] while result and len(following) < limit: following.extend(result.json()["users"]) logger.info("Retrieved {} of following, {} more to go.".format( len(following), limit - len(following))) obj, result = client.followers_get(obj) return following[:min(len(following), limit)]
def get_following(client: ApiClient, limit: int, user_id: Optional[int] = None, username: Optional[str] = None) -> List[models.User]: """Retrieve the first x amount of users that an account is following. 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 following from limit: the maximum amount of users to retrieve username: the username of the account to retrieve following 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 = GetFollowing(user_id) obj, result = client.following_get(obj) following = [] while result is True and len(following) < 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. following.extend(result.json()["users"]) logger.info("Retrieved {} of following, {} more to go.".format( len(following), limit - len(following))) obj, result = client.following_get(obj) return [ models.User.parse(f) for f in following[:min(len(following), limit)] ]
import os from time import sleep import random from instauto.api.client import ApiClient from instauto.api.actions import friendships as fs from instauto.api.actions import search as se 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') s = se.Username("instagram", 1) resp = client.search_username(s).json() user_id = resp['users'][0]['pk'] f = fs.GetFollowing(user_id) obj, result = client.following_get(f) # grabs the first page while result: # paginate until all users are extracted parsed = result.json() print(f"Extracted {len(parsed['users'])} users following") print(f"The username of the first extracted user following is {parsed['users'][0]['username']}") obj, result = client.following_get(obj) sleep(random.randint(10, 60))