22from __future__ import annotations
33
44import html
5+ import warnings
56
67from typing import Optional
78
@@ -57,7 +58,7 @@ def __repr__(self):
5758 return f"Activity({ repr (self .raw )} )"
5859
5960 def __str__ (self ):
60- return '-A ' + ' ' .join (self .parts )
61+ return '-A ' + ' ' .join (self .parts )
6162
6263 @property
6364 def parts (self ):
@@ -77,7 +78,8 @@ def parts(self):
7778 case "followstudio" :
7879 return [f"{ self .actor_username } " , "followed" , f"-S { self .title !r} ({ self .gallery_id } )" ]
7980 case "shareproject" :
80- return [f"{ self .actor_username } " , "reshared" if self .is_reshare else "shared" , f"-P { self .title !r} ({ self .project_id } )" ]
81+ return [f"{ self .actor_username } " , "reshared" if self .is_reshare else "shared" ,
82+ f"-P { self .title !r} ({ self .project_id } )" ]
8183 case "remixproject" :
8284 return [f"{ self .actor_username } " , "remixed" ,
8385 f"-P { self .parent_title !r} ({ self .parent_id } ) as -P { self .title !r} ({ self .project_id } )" ]
@@ -129,16 +131,19 @@ def parts(self):
129131 return [f"{ self .actor_username } " , "created" , f"-S { self .gallery_title } ({ self .gallery_id } )" ]
130132
131133 case "promotetomanager" :
132- return [f"{ self .actor_username } " , "promoted" , f"-U { self .recipient_username } " , "in" , f"-S { self .gallery_title } ({ self .gallery_id } )" ]
134+ return [f"{ self .actor_username } " , "promoted" , f"-U { self .recipient_username } " , "in" ,
135+ f"-S { self .gallery_title } ({ self .gallery_id } )" ]
133136
134137 case "updateprofile" :
135138 return [f"{ self .actor_username } " , "updated their profile." , f"Changed fields: { self .changed_fields } " ]
136139
137140 case "removeprojectfromstudio" :
138- return [f"{ self .actor_username } " , "removed" , f"-P { self .project_title } ({ self .project_id } )" , "from" , f"-S { self .gallery_title } ({ self .gallery_id } )" ]
141+ return [f"{ self .actor_username } " , "removed" , f"-P { self .project_title } ({ self .project_id } )" , "from" ,
142+ f"-S { self .gallery_title } ({ self .gallery_id } )" ]
139143
140144 case "addprojecttostudio" :
141- return [f"{ self .actor_username } " , "added" , f"-P { self .project_title } ({ self .project_id } )" , "to" , f"-S { self .gallery_title } ({ self .gallery_id } )" ]
145+ return [f"{ self .actor_username } " , "added" , f"-P { self .project_title } ({ self .project_id } )" , "to" ,
146+ f"-S { self .gallery_title } ({ self .gallery_id } )" ]
142147
143148 case "performaction" :
144149 return [f"{ self .actor_username } " , "performed an action" ]
@@ -173,185 +178,96 @@ def _update_from_json(self, data: dict):
173178 else :
174179 username = None
175180
176- if data .get ("recipient" ) is not None :
177- recipient_username = data ["recipient" ]["username" ]
178-
179- elif data .get ("recipient_username" ) is not None :
180- recipient_username = data ["recipient_username" ]
181-
182- elif data .get ("project_creator" ) is not None :
183- recipient_username = data ["project_creator" ]["username" ]
181+ if recipient := data .get ("recipient" ):
182+ recipient_username = recipient ["username" ]
183+ elif recipient_username := data .get ("recipient_username" ):
184+ pass
185+ elif project_creator := data .get ("project_creator" ):
186+ recipient_username = project_creator ["username" ]
184187 else :
185188 recipient_username = None
186189
187190 default_case = False
188191 # Even if `activity_type` is an invalid value; it will default to 'user performed an action'
189-
192+ self .actor_username = username
193+ self .username = username
190194 self .raw = data
195+ self .datetime_created = _time
191196 if activity_type == 0 :
192- # follow
193- followed_username = data ["followed_username" ]
194-
195- self .datetime_created = _time
196197 self .type = "followuser"
197- self .username = username
198- self .followed_username = followed_username
198+ self .followed_username = data ["followed_username" ]
199199
200200 elif activity_type == 1 :
201- # follow studio
202- studio_id = data ["gallery" ]
203-
204- self .datetime_created = _time
205201 self .type = "followstudio"
206-
207- self .username = username
208- self .gallery_id = studio_id
202+ self .gallery_id = data ["gallery" ]
209203
210204 elif activity_type == 2 :
211- # love project
212- project_id = data ["project" ]
213-
214- self .datetime_created = _time
215205 self .type = "loveproject"
216-
217- self .username = username
218- self .project_id = project_id
206+ self .project_id = data ["project" ]
219207 self .recipient_username = recipient_username
220208
221209 elif activity_type == 3 :
222- # Favorite project
223- project_id = data ["project" ]
224-
225- self .datetime_created = _time
226210 self .type = "favoriteproject"
227-
228- self .username = username
229- self .project_id = project_id
211+ self .project_id = data ["project" ]
230212 self .recipient_username = recipient_username
231213
232214 elif activity_type == 7 :
233- # Add project to studio
234-
235- project_id = data ["project" ]
236- studio_id = data ["gallery" ]
237-
238- self .datetime_created = _time
239215 self .type = "addprojecttostudio"
240-
241- self .username = username
242- self .gallery_id = studio_id
243- self .project_id = project_id
216+ self .project_id = data ["project" ]
217+ self .gallery_id = data ["gallery" ]
244218 self .recipient_username = recipient_username
245219
246220 elif activity_type in (8 , 9 , 10 ):
247- # Share/Reshare project
248- project_id = data ["project" ]
249- is_reshare = data ["is_reshare" ]
250-
251- self .is_reshare = is_reshare
252- self .datetime_created = _time
253221 self .type = "shareproject"
254-
255- self .username = username
256- self .project_id = project_id
222+ self .is_reshare = data ["is_reshare" ]
223+ self .project_id = data ["project" ]
257224 self .recipient_username = recipient_username
258225
259226 elif activity_type == 11 :
260- # Remix
261- parent_id = data ["parent" ]
262-
263- self .datetime_created = _time
264227 self .type = "remixproject"
265-
266- self .username = username
267- self .project_id = parent_id
228+ self .parent_id = data ["parent" ]
229+ warnings .warn (f"This may be incorrectly implemented.\n "
230+ f"Raw data: { data } \n "
231+ f"Please raise an issue on gh: https://github.com/TimMcCool/scratchattach/issues" )
268232 self .recipient_username = recipient_username
269233
270234 # type 12 does not exist in the HTML. That's why it was removed, not merged with type 13.
271235
272236 elif activity_type == 13 :
273- # Create ('add') studio
274- studio_id = data ["gallery" ]
275-
276- self .datetime_created = _time
277237 self .type = "createstudio"
278-
279- self .username = username
280- self .gallery_id = studio_id
238+ self .gallery_id = data ["gallery" ]
281239
282240 elif activity_type == 15 :
283- # Update studio
284- studio_id = data ["gallery" ]
285-
286- self .datetime_created = _time
287241 self .type = "updatestudio"
288-
289- self .username = username
290- self .gallery_id = studio_id
242+ self .gallery_id = data ["gallery" ]
291243
292244 elif activity_type in (16 , 17 , 18 , 19 ):
293- # Remove project from studio
294-
295- project_id = data ["project" ]
296- studio_id = data ["gallery" ]
297-
298- self .datetime_created = _time
299245 self .type = "removeprojectfromstudio"
300- self .gallery_id = studio_id
301- self .project_id = project_id
302-
303- self .username = username
304- self .project_id = project_id
246+ self .gallery_id = data ["gallery" ]
247+ self .project_id = data ["project" ]
305248
306249 elif activity_type in (20 , 21 , 22 ):
307- # Was promoted to manager for studio
308- studio_id = data ["gallery" ]
309-
310- self .datetime_created = _time
311250 self .type = "promotetomanager"
312-
313- self .username = username
314251 self .recipient_username = recipient_username
315- self .gallery_id = studio_id
252+ self .gallery_id = data [ "gallery" ]
316253
317254 elif activity_type in (23 , 24 , 25 ):
318- # Update profile
319- self .datetime_created = _time
320255 self .type = "updateprofile"
321256 self .changed_fields = data .get ("changed_fields" , {})
322257
323- self .username = username
324-
325258 elif activity_type in (26 , 27 ):
326259 # Comment in either project, user, or studio
327- comment_type : int = data ["comment_type" ]
328- fragment = data ["comment_fragment" ]
329- comment_id = data ["comment_id" ]
330- comment_obj_id = data ["comment_obj_id" ]
331- comment_obj_title = data ["comment_obj_title" ]
332-
333- self .datetime_created = _time
334260 self .type = "addcomment"
261+ self .comment_fragment = data ["comment_fragment" ]
262+ self .comment_type = data ["comment_type" ]
263+ self .comment_obj_id = data ["comment_obj_id" ]
264+ self .comment_obj_title = data ["comment_obj_title" ]
265+ self .comment_id = data ["comment_id" ]
335266
336- self .username = username
337-
338- self .comment_fragment = fragment
339- self .comment_type = comment_type
340- self .comment_obj_id = comment_obj_id
341- self .comment_obj_title = comment_obj_title
342- self .comment_id = comment_id
343267 else :
344- default_case = True
345-
346- if default_case :
347268 # This is coded in the scratch HTML, haven't found an example of it though
348- self .datetime_created = _time
349269 self .type = "performaction"
350270
351- self .username = username
352-
353- if not self .actor_username :
354- self .actor_username = self .username
355271
356272 def _update_from_html (self , data : Tag ):
357273
0 commit comments