feat: add is_followed_by_me to fix #571#572
Conversation
Signed-off-by: uukelele <robustrobot11@gmail.com>
Signed-off-by: uukelele <robustrobot11@gmail.com>
Signed-off-by: uukelele <robustrobot11@gmail.com>
Signed-off-by: uukelele <robustrobot11@gmail.com>
faretek1
left a comment
There was a problem hiding this comment.
I tested this on my machine and it works
| soup = BeautifulSoup(resp.text, "html.parser") | ||
| follow_btn = soup.select_one("div.follow-button") | ||
| if not follow_btn: | ||
| print("Warning: follow button not found in page.") |
There was a problem hiding this comment.
this may be better if it's done with warnings.warn but this should not happen anyway
There was a problem hiding this comment.
after some usage myself i realised that if you get your own user and try .is_followed_by_me() the follow button wouldn't be found in the page
There was a problem hiding this comment.
So should this fall back to the less efficient functionality?
There was a problem hiding this comment.
So should this fall back to the less efficient functionality?
i think it's better if we just explicitly state in the docs how this function works and when to use it.
as i said in the PR body:
for users with small amount e.g. <20 followers,
is_followed_byis actually faster, in my case (19 followers) 0.19s vs 0.94s.but for those with more followers e.g. 100,
is_followed_byis slower, in my case (131 followers) 1.58s vs 0.54s
i guess if you try iterate through like griffpatch's followers then is_followed_by would be a hell of a lot slower than is_followed_by_me.
so we should state that the is_followed_by_me method is best to use on users who 1) are NOT the currently logged in user and 2) have larger amount of followers.
of course i guess we could automate this by perhaps checking in is_followed_by if the username given is the one of the current session, and if the target user has over a set threshold of followers then switch to is_followed_by_me, but i guess that makes it unnecessarily complex and probably just easier to let the dev choose which one they need
There was a problem hiding this comment.
also one thing I noticed which I don't know why it wasn't implemented is that looking at the amount of times we fetch the users HTML profile page in User methods, maybe it'd be better to cache that?
or maybe we fetch the user page once when class created and store is_followed_by_me as a property? and then have .update() refresh this
|
I forgot to mark as ready for review after I tested it |
|
we could make it use .update_by_html() or something. of course it would
mean you have to use that function every time you need to get the data
associated with that page though
…On Sat, 7 Mar 2026, 12:12 uukelele, ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In scratchattach/site/user.py
<#572 (comment)>
:
> + You can only use this function if this object was created using :meth:`scratchattach.session.Session.connect_user`
+
+ Returns:
+ boolean: Whether the user is followed by the user currently logged in.
+ """
+ self._assert_auth()
+ with requests.no_error_handling():
+ resp = requests.get(
+ f"https://scratch.mit.edu/users/{self.username}/",
+ headers=self._headers,
+ cookies=self._cookies,
+ )
+ soup = BeautifulSoup(resp.text, "html.parser")
+ follow_btn = soup.select_one("div.follow-button")
+ if not follow_btn:
+ print("Warning: follow button not found in page.")
also one thing I noticed which I don't know why it wasn't implemented is
that looking at the amount of times we fetch the users HTML profile page in
User methods, maybe it'd be better to cache that?
—
Reply to this email directly, view it on GitHub
<#572 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AZV3QSISPRT6PGWZFILBCJL4PQGZ7AVCNFSM6AAAAACWEOM5UGVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZTSMBYGY3DANRYGY>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
Solves issue #571
Changes
added
is_followed_by_mewhich uses the follow button.Tests
works and is ready to merge.
one thing I noticed though:
for users with small amount e.g. <20 followers,
is_followed_byis actually faster, in my case (19 followers) 0.19s vs 0.94s.but for those with more followers e.g. 100,
is_followed_byis slower, in my case (131 followers) 1.58s vs 0.54s