Returns the comments posted on the user's profile (with replies). Keyword Arguments: page: The page of the comments that should be returned. Returns: list : A list containing the requested comments as Comment objects.
(self, *, page=1)
| 1009 | ) |
| 1010 | |
| 1011 | def comments(self, *, page=1) -> list[comment.Comment] | None: |
| 1012 | """ |
| 1013 | Returns the comments posted on the user's profile (with replies). |
| 1014 | |
| 1015 | Keyword Arguments: |
| 1016 | page: The page of the comments that should be returned. |
| 1017 | |
| 1018 | Returns: |
| 1019 | list<scratchattach.comment.Comment>: A list containing the requested comments as Comment objects. |
| 1020 | """ |
| 1021 | data = [] |
| 1022 | |
| 1023 | with requests.no_error_handling(): |
| 1024 | resp = requests.get(f"https://scratch.mit.edu/site-api/comments/user/{self.username}/?page={page}") |
| 1025 | |
| 1026 | if resp.status_code == 404: |
| 1027 | # Profile comments seem to end at page 67, and afterwards give 404. |
| 1028 | # Usually when page > 67. It is possible to have empty pages before |
| 1029 | # page 67, but still have pages with content afterwards. Hence we want |
| 1030 | # to differentiate between empty pages with a page that actually marks |
| 1031 | # a definite end |
| 1032 | # |
| 1033 | # A way to reasonably reliably detect the end of the comments is by keeping track |
| 1034 | # of how many blank pages you have seen, and breaking if you e.g. have 3 in a row: |
| 1035 | # https://github.com/TimMcCool/scratchattach/issues/582#issuecomment-4318675630 |
| 1036 | return None |
| 1037 | |
| 1038 | soup = BeautifulSoup(resp.content, "html.parser") |
| 1039 | |
| 1040 | _comments = soup.find_all("li", {"class": "top-level-reply"}) |
| 1041 | for entity in _comments: |
| 1042 | comment_id = entity.find("div", {"class": "comment"})["data-comment-id"] |
| 1043 | user = entity.find("a", {"id": "comment-user"})["data-comment-user"] |
| 1044 | content = str(entity.find("div", {"class": "content"}).text).strip() |
| 1045 | time = entity.find("span", {"class": "time"})["title"] |
| 1046 | |
| 1047 | main_comment = { |
| 1048 | "id": comment_id, |
| 1049 | "author": {"username": user}, |
| 1050 | "content": content, |
| 1051 | "datetime_created": time, |
| 1052 | } |
| 1053 | _comment = comment.Comment( |
| 1054 | source=comment.CommentSource.USER_PROFILE, |
| 1055 | source_id=self.username, |
| 1056 | _session=self._session, |
| 1057 | ) |
| 1058 | _comment._update_from_dict(main_comment) |
| 1059 | |
| 1060 | reply_objs = [] |
| 1061 | replies = entity.find_all("li", {"class": "reply"}) |
| 1062 | for reply in replies: |
| 1063 | r_comment_id = reply.find("div", {"class": "comment"})["data-comment-id"] |
| 1064 | r_user = reply.find("a", {"id": "comment-user"})["data-comment-user"] |
| 1065 | r_content = ( |
| 1066 | str(reply.find("div", {"class": "content"}).text) |
| 1067 | .strip() |
| 1068 | .replace("\n", "") |