Esempio n. 1
0
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)]
    ]
Esempio n. 2
0
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)]
    ]
Esempio n. 3
0
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)]
Esempio n. 4
0
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)]
    ]