From 597b578761c614f7d6af19f4183309593364d310 Mon Sep 17 00:00:00 2001 From: Deepak Soni Date: Sat, 2 Mar 2024 15:29:13 +0530 Subject: [PATCH] added comment() to Mobile.py --- ensta/Mobile.py | 46 ++++++++++++++++++++++++-- ensta/parser/AddedCommentParser.py | 30 +++++++++++++++++ ensta/parser/AddedCommentUserParser.py | 19 +++++++++++ ensta/parser/__init__.py | 2 ++ ensta/structures/AddedComment.py | 29 ++++++++++++++++ ensta/structures/AddedCommentUser.py | 22 ++++++++++++ ensta/structures/__init__.py | 2 ++ mobile_migration_checklist.md | 4 +-- 8 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 ensta/parser/AddedCommentParser.py create mode 100644 ensta/parser/AddedCommentUserParser.py create mode 100644 ensta/structures/AddedComment.py create mode 100644 ensta/structures/AddedCommentUser.py diff --git a/ensta/Mobile.py b/ensta/Mobile.py index 670fb62..9e71c85 100644 --- a/ensta/Mobile.py +++ b/ensta/Mobile.py @@ -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 @@ -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 """ @@ -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 """ @@ -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." + ) diff --git a/ensta/parser/AddedCommentParser.py b/ensta/parser/AddedCommentParser.py new file mode 100644 index 0000000..b719945 --- /dev/null +++ b/ensta/parser/AddedCommentParser.py @@ -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") + ) diff --git a/ensta/parser/AddedCommentUserParser.py b/ensta/parser/AddedCommentUserParser.py new file mode 100644 index 0000000..b51ac04 --- /dev/null +++ b/ensta/parser/AddedCommentUserParser.py @@ -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") + ) diff --git a/ensta/parser/__init__.py b/ensta/parser/__init__.py index 6a83a42..1b8b180 100644 --- a/ensta/parser/__init__.py +++ b/ensta/parser/__init__.py @@ -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 diff --git a/ensta/structures/AddedComment.py b/ensta/structures/AddedComment.py new file mode 100644 index 0000000..0420b4b --- /dev/null +++ b/ensta/structures/AddedComment.py @@ -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 diff --git a/ensta/structures/AddedCommentUser.py b/ensta/structures/AddedCommentUser.py new file mode 100644 index 0000000..82ffd3a --- /dev/null +++ b/ensta/structures/AddedCommentUser.py @@ -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 diff --git a/ensta/structures/__init__.py b/ensta/structures/__init__.py index 17f3d11..dfba58c 100644 --- a/ensta/structures/__init__.py +++ b/ensta/structures/__init__.py @@ -5,3 +5,5 @@ from .Followings import Followings from .Follower import Follower from .Following import Following +from .AddedComment import AddedComment +from .AddedCommentUser import AddedCommentUser diff --git a/mobile_migration_checklist.md b/mobile_migration_checklist.md index 3aa5311..07c294d 100644 --- a/mobile_migration_checklist.md +++ b/mobile_migration_checklist.md @@ -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 \ No newline at end of file