Example #1
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)
Example #2
0
def like_post(client: ApiClient, media_id: str) -> bool:
    """Like a post.

    Args:
        client: your `ApiClient`
        media_id: the post to like

    Returns:
        `True` if success else `False`
    """
    like = ps.Like(media_id=media_id)
    resp = client.post_like(like)
    logger.info(f"liked post {media_id}")
    return is_resp_ok(resp)
Example #3
0
def upload_image_to_story(client: ApiClient, image_path: str) -> bool:
    """Upload an image to your story.

    Args:
        client: your `ApiClient`
        image_path: path to the image to upload

    Returns:
        `True` if success else `False`
    """
    post = ps.PostStory(path=image_path)
    resp = client.post_post(post)
    logger.info(f"Uploaded image to story")
    return is_resp_ok(resp)
Example #4
0
def save_post(client: ApiClient, media_id: str) -> bool:
    """Save a post.

    Args:
        client: your `ApiClient`
        media_id: the media_id of a post

    Returns:
        `True` if success else `False`
    """
    save = ps.Save(media_id=media_id)
    resp = client.post_save(save)
    logger.info(f"Saved post {media_id}")
    return is_resp_ok(resp)
Example #5
0
def unlike_post(client: ApiClient, media_id: str) -> bool:
    """Undo the liking of a post.

    Args:
        client: your `ApiClient`
        media_id: the media_id of a post

    Returns:
        `True` if success else `False`
    """
    like = ps.Unlike(media_id=media_id)
    resp = client.post_unlike(like)
    logger.info(f"Unliked post {media_id}")
    return is_resp_ok(resp)
Example #6
0
def comment_post(client: ApiClient, media_id: str, comment: str) -> bool:
    """Leave a comment on a post.

    Args:
        client: your `ApiClient`
        media_id: the post to comment on
        comment: the comment to place

    Returns:
        `True` if success else `False`
    """
    obj = ps.Comment(media_id=media_id, comment_text=comment)
    resp = client.post_comment(obj)
    logger.info(f"Commented {comment} on post {media_id}")
    return is_resp_ok(resp)
Example #7
0
def update_caption(client: ApiClient, media_id: str, new_caption: str) -> bool:
    """Update the caption of a post.

    Args:
        client: your `ApiClient`
        media_id: the media_id of a post
        new_caption: the new caption

    Returns:
        `True` if success else `False`
    """
    caption = ps.UpdateCaption(media_id=media_id, caption_text=new_caption)
    resp = client.post_update_caption(caption)
    logger.info(f"Updated caption of post {media_id} to {new_caption}")
    return is_resp_ok(resp)
Example #8
0
def upload_image_to_feed(client: ApiClient,
                         image_path: str,
                         caption: Optional[str] = None,
                         location: Optional[ps.Location] = None) -> bool:
    """Upload an image to your feed. Location and caption are optional.

    Args:
        client: your `ApiClient`
        image_path: path to the image to upload
        caption: the caption of the post
        location: the location tag of the post

    Returns:
        `True` if success else `False`
    """
    post = ps.PostFeed(
        path=image_path,
        caption=caption or '',
        location=location,
    )
    resp = client.post_post(post, 80)
    logger.info(f"Uploaded image to feed")
    return is_resp_ok(resp)
Example #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)