Posts a comment on the user's profile. You can only use this function if this object was created using :meth:`scratchattach.session.Session.connect_user` Args: :param content: Content of the comment that should be posted Keyword Arguments: :param co
(self, content, *, parent_id="", commentee_id="")
| 841 | ) |
| 842 | |
| 843 | def post_comment(self, content, *, parent_id="", commentee_id=""): |
| 844 | """ |
| 845 | Posts a comment on the user's profile. You can only use this function if this object was created using :meth:`scratchattach.session.Session.connect_user` |
| 846 | |
| 847 | Args: |
| 848 | :param content: Content of the comment that should be posted |
| 849 | |
| 850 | Keyword Arguments: |
| 851 | :param commentee_id: ID of the comment you want to reply to. If you don't want to mention a user, don't put the argument. |
| 852 | :param parent_id: ID of the user that will be mentioned in your comment and will receive a message about your comment. If you don't want to mention a user, don't put the argument. |
| 853 | |
| 854 | Returns: |
| 855 | scratchattach.comment.Comment: An object representing the created comment. |
| 856 | """ |
| 857 | self._assert_auth() |
| 858 | data = { |
| 859 | "commentee_id": commentee_id, |
| 860 | "content": str(content), |
| 861 | "parent_id": parent_id, |
| 862 | } |
| 863 | r = requests.post( |
| 864 | f"https://scratch.mit.edu/site-api/comments/user/{self.username}/add/", |
| 865 | headers=headers, |
| 866 | cookies=self._cookies, |
| 867 | data=json.dumps(data), |
| 868 | ) |
| 869 | if r.status_code != 200: |
| 870 | if "Looks like we are having issues with our servers!" in r.text: |
| 871 | raise exceptions.BadRequest("Invalid arguments passed") |
| 872 | else: |
| 873 | raise exceptions.CommentPostFailure(r.text) |
| 874 | |
| 875 | text = r.text |
| 876 | try: |
| 877 | data = { |
| 878 | "id": text.split('<div id="comments-')[1].split('" class="comment')[0], |
| 879 | "author": {"username": text.split('" data-comment-user="')[1].split('"><img class')[0]}, |
| 880 | "content": text.split('<div class="content">')[1].split("</div>")[0].strip(), |
| 881 | "reply_count": 0, |
| 882 | "cached_replies": [], |
| 883 | } |
| 884 | _comment = comment.Comment( |
| 885 | source=comment.CommentSource.USER_PROFILE, |
| 886 | parent_id=None if parent_id == "" else parent_id, |
| 887 | commentee_id=commentee_id, |
| 888 | source_id=self.username, |
| 889 | id=data["id"], |
| 890 | _session=self._session, |
| 891 | datetime=datetime.now(), |
| 892 | ) |
| 893 | _comment._update_from_dict(data) |
| 894 | return _comment |
| 895 | except Exception as e: |
| 896 | if '{"error": "isFlood"}' in text: |
| 897 | raise ( |
| 898 | exceptions.CommentPostFailure( |
| 899 | "You are being rate-limited for running this operation too often. Implement a cooldown of about 10 seconds." |
| 900 | ) |
no test coverage detected