Skip to content

Commit

Permalink
added comment() to Mobile.py
Browse files Browse the repository at this point in the history
  • Loading branch information
diezo committed Mar 2, 2024
1 parent a84fa7b commit 597b578
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 5 deletions.
46 changes: 43 additions & 3 deletions ensta/Mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from .parser.ProfileParser import parse_profile
from .parser.FollowersParser import parse_followers
from .parser.FollowingsParser import parse_followings
from .structures import Profile, StoryLink, Followers, Followings
from .parser.AddedCommentParser import parse_added_comment
from .structures import Profile, StoryLink, Followers, Followings, AddedComment
from .Direct import Direct
from ensta.Utils import time_id, fb_uploader

Expand Down Expand Up @@ -630,7 +631,7 @@ def userid_to_username(self, user_id: str) -> str:
def like(self, media_id: str) -> bool:
"""
Likes a post or reel
:param: media_id: ID of post or reel
:param: media_id: Media ID of post or reel
:return: True/False
"""

Expand Down Expand Up @@ -669,7 +670,7 @@ def like(self, media_id: str) -> bool:
def unlike(self, media_id: str) -> bool:
"""
Unlikes a post or reel
:param: media_id: ID of post or reel
:param: media_id: Media ID of post or reel
:return: True/False
"""

Expand Down Expand Up @@ -770,3 +771,42 @@ def followings(self, identifier: str, next_cursor: str | int | None = None) -> F
"restricted you. Try using another account, switch "
"to a different network, or use reputed proxies."
)

def comment(self, text: str, media_id: str) -> AddedComment:
"""
Comment on a post or reel.
:param: media_id: Media ID of post or reel
:param: text: Comment text
:return: True/False
"""

response: Response = self.session.post(
url=f"https://i.instagram.com/api/v1/media/{media_id}/comment/",
data={
"signed_body": "SIGNATURE." + json.dumps(
{
"_uid": self.user_id,
"_uuid": str(uuid4()),
"comment_text": text
}
)
}
)

try:
response_dict: dict = response.json()

if response_dict.get("status", "") != "ok":
raise NetworkError(
"Couldn't add comment.\n"
f"Response JSON: {response_dict}"
)

return parse_added_comment(response_dict)

except JSONDecodeError:
raise NetworkError(
"Unable to add comment. Maybe your comment contains offensive words. "
"If not, try using another account, switch "
"to a different network, or use reputed proxies."
)
30 changes: 30 additions & 0 deletions ensta/parser/AddedCommentParser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from ensta.structures.AddedComment import AddedComment
from ensta.structures.AddedCommentUser import AddedCommentUser
from .AddedCommentUserParser import parse_added_comment_user


def parse_added_comment(information: dict) -> AddedComment:

comment_dict: dict = information.get("comment")
user: AddedCommentUser = parse_added_comment_user(comment_dict.get("user"))

return AddedComment(
comment_id=comment_dict.get("pk"),
user_id=str(comment_dict.get("user_id")),
type=comment_dict.get("type"),
did_report_as_spam=comment_dict.get("did_report_as_spam"),
created_at=comment_dict.get("created_at"),
created_at_utc=comment_dict.get("created_at_utc"),
content_type=comment_dict.get("content_type"),
status=comment_dict.get("status"),
bit_flags=comment_dict.get("bit_flags"),
share_enabled=comment_dict.get("share_enabled"),
is_ranked_comment=comment_dict.get("is_ranked_comment"),
media_id=str(comment_dict.get("media_id")),
restricted_status=comment_dict.get("restricted_status"),
is_created_by_media_owner=comment_dict.get("is_created_by_media_owner"),
user=user,
text=comment_dict.get("text"),
is_covered=comment_dict.get("is_covered"),
comment_creation_key=information.get("comment_creation_key")
)
19 changes: 19 additions & 0 deletions ensta/parser/AddedCommentUserParser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from ensta.structures.AddedCommentUser import AddedCommentUser


def parse_added_comment_user(information: dict) -> AddedCommentUser:

return AddedCommentUser(
user_id=str(information.get("pk")),
username=information.get("username"),
full_name=information.get("full_name"),
is_private=information.get("is_private"),
has_onboarded_to_text_post_app=information.get("has_onboarded_to_text_post_app"),
fbid_v2=str(information.get("fbid_v2")),
is_verified=information.get("is_verified"),
profile_picture_id=information.get("profile_pic_id"),
profile_picture_url=information.get("profile_pic_url"),
is_mentionable=information.get("is_mentionable"),
latest_reel_media=information.get("latest_reel_media"),
latest_besties_reel_media=information.get("latest_besties_reel_media")
)
2 changes: 2 additions & 0 deletions ensta/parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
from .FollowingsParser import parse_followings
from .FollowersListParser import parse_followers_list
from .FollowingsListParser import parse_followings_list
from .AddedCommentParser import parse_added_comment
from .AddedCommentUserParser import parse_added_comment_user
29 changes: 29 additions & 0 deletions ensta/structures/AddedComment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from dataclasses import dataclass
from .AddedCommentUser import AddedCommentUser


@dataclass
class AddedComment:

"""
Stores information about the comment you posted e.g. - Comment ID, Text, Comment Type etc.
"""

comment_id: str
user_id: str
type: int
did_report_as_spam: bool
created_at: int
created_at_utc: int
content_type: str
status: str
bit_flags: int
share_enabled: bool
is_ranked_comment: bool
media_id: str
restricted_status: int
is_created_by_media_owner: bool
user: AddedCommentUser
text: str
is_covered: bool
comment_creation_key: str
22 changes: 22 additions & 0 deletions ensta/structures/AddedCommentUser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from dataclasses import dataclass


@dataclass
class AddedCommentUser:

"""
Stores information of user who added the comment (you) e.g. - User ID, Username, Full Name, etc.
"""

user_id: str
username: str
full_name: str
is_private: bool
has_onboarded_to_text_post_app: bool
fbid_v2: str
is_verified: bool
profile_picture_id: str
profile_picture_url: str
is_mentionable: bool
latest_reel_media: int
latest_besties_reel_media: int
2 changes: 2 additions & 0 deletions ensta/structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
from .Followings import Followings
from .Follower import Follower
from .Following import Following
from .AddedComment import AddedComment
from .AddedCommentUser import AddedCommentUser
4 changes: 2 additions & 2 deletions mobile_migration_checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
- fetch posts
- individual post data
- get post id by share url
- private info
- update display name

- fb upload video
- upload single photo post
- upload carousel
- upload single reel
- comment

- fetch likers

0 comments on commit 597b578

Please sign in to comment.