Skip to content

Commit 89c2f4f

Browse files
authored
feat: is_followed_by_me
* add `is_followed_by_me` to fix #571 Signed-off-by: uukelele <robustrobot11@gmail.com> * add tests for `is_followed_by_me` Signed-off-by: uukelele <robustrobot11@gmail.com> * you named it wrong Signed-off-by: uukelele <robustrobot11@gmail.com> * fix: assert that user is logged in but don't require to be profile owner Signed-off-by: uukelele <robustrobot11@gmail.com> --------- Signed-off-by: uukelele <robustrobot11@gmail.com>
1 parent 3854eeb commit 89c2f4f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

scratchattach/site/user.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,29 @@ def is_followed_by(self, user):
494494
return followed
495495
return followed
496496

497+
def is_followed_by_me(self):
498+
"""
499+
You can only use this function if this object was created using :meth:`scratchattach.session.Session.connect_user`
500+
501+
Returns:
502+
boolean: Whether the user is followed by the user currently logged in.
503+
"""
504+
self._assert_auth()
505+
with requests.no_error_handling():
506+
resp = requests.get(
507+
f"https://scratch.mit.edu/users/{self.username}/",
508+
headers=self._headers,
509+
cookies=self._cookies,
510+
)
511+
soup = BeautifulSoup(resp.text, "html.parser")
512+
follow_btn = soup.select_one("div.follow-button")
513+
if not follow_btn:
514+
print("Warning: follow button not found in page.")
515+
return False # defualt to not followed
516+
data_control = follow_btn.get("data-control")
517+
return data_control == 'unfollow' # True if unfollow, False if not
518+
519+
497520
def project_count(self):
498521
with requests.no_error_handling():
499522
text = requests.get(

tests/test_user.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
def test_user():
77
if not credentials_available():
88
warnings.warn(
9-
"Skipped test_studio because there were no credentials available."
9+
"Skipped test_user because there were no credentials available."
1010
)
1111
return
1212
sess = session()
@@ -106,6 +106,11 @@ def test_user():
106106
assert status_data["status"] == "Sample status"
107107
assert status_data["color"] == "#855cd6"
108108

109+
uukelele = sess.connect_user("uukelele") # could use anyone ScratchAttachV2 is following right now but i think its cool that its following my account - uukelele, 2026
110+
assert uukelele.is_followed_by_me()
111+
# and someone he is not following
112+
assert not griffpatch.is_followed_by_me()
113+
109114

110115
if __name__ == "__main__":
111116
test_user()

0 commit comments

Comments
 (0)