18
18
class Classroom (BaseSiteComponent ):
19
19
def __init__ (self , ** entries ):
20
20
# Info on how the .update method has to fetch the data:
21
+ # NOTE: THIS DOESN'T WORK WITH CLOSED CLASSES!
21
22
self .update_function = requests .get
22
23
if "id" in entries :
23
24
self .update_API = f"https://api.scratch.mit.edu/classrooms/{ entries ['id' ]} "
@@ -46,6 +47,9 @@ def __init__(self, **entries):
46
47
self ._json_headers ["accept" ] = "application/json"
47
48
self ._json_headers ["Content-Type" ] = "application/json"
48
49
50
+ def __repr__ (self ):
51
+ return f"classroom called '{ self .title } '"
52
+
49
53
def _update_from_dict (self , classrooms ):
50
54
try :
51
55
self .id = int (classrooms ["id" ])
@@ -215,6 +219,25 @@ def close(self):
215
219
warnings .warn (f"{ self ._session } may not be authenticated to edit { self } " )
216
220
raise e
217
221
222
+ def register_user (self , username : str , password : str , birth_month : int , birth_year : int ,
223
+ gender : str , country : str , is_robot : bool = False ):
224
+ return register_user (self .id , self .classtoken , username , password , birth_month , birth_year , gender , country , is_robot )
225
+
226
+ def generate_signup_link (self ):
227
+ if self .classtoken is not None :
228
+ return f"https://scratch.mit.edu/signup/{ self .classtoken } "
229
+
230
+ self ._check_session ()
231
+
232
+ response = requests .get (f"https://scratch.mit.edu/site-api/classrooms/generate_registration_link/{ self .id } /" , headers = self ._headers , cookies = self ._cookies )
233
+ # Should really check for '404' page
234
+ data = response .json ()
235
+ if "reg_link" in data :
236
+ return data ["reg_link" ]
237
+ else :
238
+ raise exceptions .Unauthorized (f"{ self ._session } is not authorised to generate a signup link of { self } " )
239
+
240
+
218
241
def public_activity (self , * , limit = 20 ):
219
242
"""
220
243
Returns:
@@ -288,3 +311,25 @@ def get_classroom_from_token(class_token) -> Classroom:
288
311
"""
289
312
warnings .warn ("For methods that require authentication, use session.connect_classroom instead of get_classroom" )
290
313
return commons ._get_object ("classtoken" , class_token , Classroom , exceptions .ClassroomNotFound )
314
+
315
+
316
+ def register_user (class_id : int , class_token : str , username : str , password : str , birth_month : int , birth_year : int , gender : str , country : str , is_robot : bool = False ):
317
+ data = {"classroom_id" : class_id ,
318
+ "classroom_token" : class_token ,
319
+
320
+ "username" : username ,
321
+ "password" : password ,
322
+ "birth_month" : birth_month ,
323
+ "birth_year" : birth_year ,
324
+ "gender" : gender ,
325
+ "country" : country ,
326
+ "is_robot" : is_robot }
327
+
328
+ response = requests .post ("https://scratch.mit.edu/classes/register_new_student/" ,
329
+ data = data , headers = headers , cookies = {"scratchcsrftoken" : 'a' })
330
+ ret = response .json ()[0 ]
331
+
332
+ if "username" in ret :
333
+ return
334
+ else :
335
+ raise exceptions .Unauthorized (f"Can't create account: { response .text } " )
0 commit comments