Skip to content

Conversation

@faretek1
Copy link
Collaborator

@faretek1 faretek1 commented May 30, 2025

There is currently no way, only using scratchattach, to get the associated classroom of student accounts. It appears that there is no JSON site-api for this (or maybe I am just ignorant), however it is possible to work this out by viewing the profile-details <p> tag in the profile page HTML.

This PR implements a site.user.User.classroom getter method using the @property decorator and a cache attribute. As users can never change what class they are in, this cache value will never need to change, solving #382

The implementation uses bs4 to get the `

` tag, and uses `soup.find_all("a")` and a RegEx for each href to detect which anchor refers to the classroom. The only possible other instance of an anchor tag being used in there is for becoming a scratcher, but I am not sure.

Note: The returned classroom object will only contain a classroom id and a classroom title (and a boolean if it is ended?)

todo:

  • There should be a check for if a class is ended.

@faretek1 faretek1 marked this pull request as ready for review May 30, 2025 08:42
@faretek1
Copy link
Collaborator Author

I also moved around the imports within user.py to be structured nicer:
image

@faretek1
Copy link
Collaborator Author

faretek1 commented May 30, 2025

This would not conflict with any existing code, and has been tested on 3 accounts:

import scratchattach as sa

u1 = sa.get_user("faretek1")
u2 = sa.get_user("l-_-o-_-i")
u3 = sa.get_user("tend-1")

print(u1.classroom)
print(u2.classroom)
print(u3.classroom)

console:

Warning: For methods that require authentication, use session.connect_user instead of get_user
Warning: For methods that require authentication, use session.connect_user instead of get_user
Warning: For methods that require authentication, use session.connect_user instead of get_user
None
classroom called 'FA'
classroom called 'tend'

Process finished with exit code 0

@faretek1
Copy link
Collaborator Author

Now checked that it works for detecting if a class is closed, and fixed a bug.

import scratchattach as sa

u1 = sa.get_user("faretek1")
u2 = sa.get_user("l-_-o-_-i")
u3 = sa.get_user("tend-1")

print(u1.classroom)
print(u2.classroom.__dict__)
print(u3.classroom.__dict__)

console:

Warning: For methods that require authentication, use session.connect_user instead of get_user
Warning: For methods that require authentication, use session.connect_user instead of get_user
Warning: For methods that require authentication, use session.connect_user instead of get_user
None
{'update_function': <function Requests.get at 0x0000028BEC823880>, 'update_API': 'https://api.scratch.mit.edu/classrooms/306224', '_session': <scratchattach.site.user.User object at 0x0000028BED3984C0>, 'id': '306224', 'classtoken': None, 'is_closed': False, 'title': 'FA', '_headers': {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36', 'x-csrftoken': 'a', 'x-requested-with': 'XMLHttpRequest', 'referer': 'https://scratch.mit.edu'}, '_cookies': {}, '_json_headers': {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36', 'x-csrftoken': 'a', 'x-requested-with': 'XMLHttpRequest', 'referer': 'https://scratch.mit.edu', 'accept': 'application/json', 'Content-Type': 'application/json'}}
{'update_function': <function Requests.get at 0x0000028BEC823880>, 'update_API': 'https://api.scratch.mit.edu/classrooms/1834118', '_session': <scratchattach.site.user.User object at 0x0000028BEBD7BF40>, 'id': '1834118', 'classtoken': None, 'is_closed': True, 'title': 'tend', '_headers': {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36', 'x-csrftoken': 'a', 'x-requested-with': 'XMLHttpRequest', 'referer': 'https://scratch.mit.edu'}, '_cookies': {}, '_json_headers': {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36', 'x-csrftoken': 'a', 'x-requested-with': 'XMLHttpRequest', 'referer': 'https://scratch.mit.edu', 'accept': 'application/json', 'Content-Type': 'application/json'}}

Process finished with exit code 0

@faretek1 faretek1 merged commit efdca88 into TimMcCool:main May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant