Exemplo n.º 1
0
def retrieve_story_from_user(
        client: ApiClient,
        username: Optional[str] = None,
        user_id: Optional[int] = None) -> List[models.Story]:
    """Retrieve x amount of posts from a user.

    Either `user_id` or `username` need to be provided. If both are provided,
    the user_id takes precedence.

    Args:
        client: your `ApiClient`
        limit: maximum amount of posts to retrieve
        username: username of the account to retrieve posts from
        user_id: user_id of the account to retrieve posts from

    Returns:
        A list of Instagram post objects (objects/post.json).
    """
    if username is None and user_id is None:
        raise ValueError(
            "Either `username` or `user_id` param need to be provider")
    if username is not None and user_id is None:
        user_id = get_user_id_from_username(client, username)
    elif username is not None and user_id is not None:
        logger.warning(
            "Both `username` and `user_id` are provided. `user_id` will be used."
        )

    if user_id is None:
        raise NotFoundError(f"Couldn't find user {username}")
    obj = ps.RetrieveStory(user_id=user_id)
    resp = client.post_retrieve_story(obj).json()
    items = resp['reel'].get('items')
    return [models.Story.parse(i) for i in items]
Exemplo n.º 2
0
def follow_user(client: ApiClient,
                user_id: str = None,
                username: str = None) -> bool:
    """Send a follow request to a user.

    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 follow
        username: the username of the account to follow
    Returns:
        True if success else False
    """
    if user_id is not None and username is not None:
        raise ValueError("Both `user_id` and `username` are provided.")

    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 = Create(str(user_id))
    resp = client.user_follow(obj)
    return is_resp_ok(resp)
Exemplo n.º 3
0
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)]
    ]
Exemplo n.º 4
0
    def from_followers_of(self, account_name: str, limit: int) -> "Input":
        """Retrieves accounts that follow `account_name`.

        Args:
            account_name: the account to retrieve from
            limit: the amount of accounts to retrieve
        """
        followers = get_followers(
            self._client, get_user_id_from_username(self._client,
                                                    account_name), limit)
        self._accounts.extend(followers)
        return self
Exemplo n.º 5
0
    def from_following_of(self, account_name: str, limit: int) -> "Input":
        """Retrieves accounts that `account_name` follows.

        Args:
            account_name: the account to retrieve from
            limit: the amount of accounts to retrieve
        """
        user_id = get_user_id_from_username(self._client, account_name)
        if user_id is None:
            return self
        following = get_following(self._client, str(user_id), limit)
        self._accounts.extend(following)
        return self
Exemplo n.º 6
0
def follow_user(client: ApiClient,
                user_id: str = None,
                username: str = None) -> bool:
    if user_id is not None and username is not None:
        raise ValueError("Both `user_id` and `username` are provided.")

    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 = Create(user_id)
    resp = client.user_follow(obj)
    return is_resp_ok(resp)
Exemplo n.º 7
0
def retrieve_posts_from_user(
        client: ApiClient,
        limit: int,
        username: Optional[str] = None,
        user_id: Optional[int] = None) -> List[models.Post]:
    """Retrieve x amount of posts from a user.

    Either `user_id` or `username` need to be provided. If both are provided,
    the user_id takes precedence.

    Args:
        client: your `ApiClient`
        limit: maximum amount of posts to retrieve
        username: username of the account to retrieve posts from
        user_id: user_id of the account to retrieve posts from

    Returns:
        A list of Instagram post objects (objects/post.json).
    """
    if username is None and user_id is None:
        raise ValueError(
            "Either `username` or `user_id` param need to be provider")
    if username is not None and user_id is None:
        user_id = get_user_id_from_username(client, username)
    elif username is not None and user_id is not None:
        logger.warning(
            "Both `username` and `user_id` are provided. `user_id` will be used."
        )

    if user_id is None:
        raise NotFoundError(f"Couldn't find user {username}")
    obj = ps.RetrieveByUser(user_id=user_id)
    obj, result = client.post_retrieve_by_user(obj)
    retrieved_items = []

    while result and len(retrieved_items) < limit:
        logger.info(
            f"Retrieved {len(retrieved_items)} posts from user {username or user_id}"
        )
        # pyre-ignore[6]
        retrieved_items.extend(result)
        obj, result = client.post_retrieve_by_user(obj)
    return [models.Post.parse(p) for p in retrieved_items[:limit:]]
Exemplo n.º 8
0
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)]
    ]
Exemplo n.º 9
0
def unfollow_user(client: ApiClient,
                  user_id: Optional[int] = None,
                  username: Optional[str] = None) -> bool:
    """Unfollow a user.

    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 unfollow
        username: the username of the account to unfollow
    Returns:
        True if success else False
    """
    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 = Destroy(str(user_id))
    resp = client.user_unfollow(obj)
    return is_resp_ok(resp)
Exemplo n.º 10
0
from instauto.api.client import ApiClient
from instauto.helpers.search import get_user_id_from_username
from instauto.helpers.friendships import get_following

# Initiate the client
client = ApiClient.initiate_from_file('.instauto.save')
# Get the user id by doing one of the below
user_id = "user_id"
user_id = get_user_id_from_username(client, "instagram")
# And retrieve 100 users following the user
following = get_following(client, user_id, 100)

Exemplo n.º 11
0
 def from_followers_of(self, account_name: str, limit: int) -> "Input":
     followers = get_followers(self._client, get_user_id_from_username(self._client, account_name), limit)
     self.accounts.extend(followers)
     return self