Skip to content

Commit 36f0286

Browse files
authored
Merge pull request #667 from TimMcCool/semver2_refactor_activity_scraper
refactor activity scraper
2 parents e1571fb + cfdeddd commit 36f0286

1 file changed

Lines changed: 54 additions & 27 deletions

File tree

scratchattach/site/activity.py

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Activity and CloudActivity class"""
2+
23
from __future__ import annotations
34

4-
from bs4 import PageElement
5+
from bs4 import PageElement, Tag
56

67
from . import user, project, studio
78
from ._base import BaseSiteComponent
@@ -113,7 +114,7 @@ def _update_from_json(self, data: dict):
113114
raw = f"{username} loved project https://scratch.mit.edu/projects/{project_id}"
114115

115116
self.raw = raw
116-
self.datetime_created = _time,
117+
self.datetime_created = _time
117118
self.type = "loveproject"
118119

119120
self.username = username
@@ -182,7 +183,7 @@ def _update_from_json(self, data: dict):
182183
self.recipient_username = recipient_username
183184

184185
# type 12 does not exist in the HTML. That's why it was removed, not merged with type 13.
185-
186+
186187
elif activity_type == 13:
187188
# Create ('add') studio
188189
studio_id = data["gallery"]
@@ -295,24 +296,23 @@ def _update_from_json(self, data: dict):
295296

296297
self.username = username
297298

298-
def _update_from_html(self, data: PageElement):
299+
def _update_from_html(self, data: Tag):
299300

300301
self.raw = data
301302

302-
_time = data.find('div').find('span').findNext().findNext().text.strip()
303+
_time = getattr(data.select_one("div span.time"), "text", "").strip()
303304

304-
if '\xa0' in _time:
305-
while '\xa0' in _time:
306-
_time = _time.replace('\xa0', ' ')
305+
while "\xa0" in _time:
306+
_time = _time.replace("\xa0", " ")
307307

308308
self.datetime_created = _time
309-
self.actor_username = data.find('div').find('span').text
309+
self.actor_username = getattr(data.select_one("div span"), "text", "")
310310

311-
self.target_name = data.find('div').find('span').findNext().text
312-
self.target_link = data.find('div').find('span').findNext()["href"]
313-
self.target_id = data.find('div').find('span').findNext()["href"].split("/")[-2]
311+
self.target_name = getattr(data.select_one("div a"), "text", "")
312+
self.target_link = str((data.select_one("div a") or {"href": ""})["href"])
313+
self.target_id = self.target_link.split("/")[-2]
314314

315-
self.type = data.find('div').find_all('span')[0].next_sibling.strip()
315+
self.type = getattr(data.select_one("div span"), "next_sibling", "").strip()
316316
if self.type == "loved":
317317
self.type = "loveproject"
318318

@@ -337,7 +337,9 @@ def actor(self):
337337
"""
338338
Returns the user that performed the activity as User object
339339
"""
340-
return self._make_linked_object("username", self.actor_username, user.User, exceptions.UserNotFound)
340+
return self._make_linked_object(
341+
"username", self.actor_username, user.User, exceptions.UserNotFound
342+
)
341343

342344
def target(self):
343345
"""
@@ -347,35 +349,60 @@ def target(self):
347349

348350
if "project" in self.type: # target is a project
349351
if "target_id" in self.__dict__:
350-
return self._make_linked_object("id", self.target_id, project.Project, exceptions.ProjectNotFound)
352+
return self._make_linked_object(
353+
"id", self.target_id, project.Project, exceptions.ProjectNotFound
354+
)
351355
if "project_id" in self.__dict__:
352-
return self._make_linked_object("id", self.project_id, project.Project, exceptions.ProjectNotFound)
356+
return self._make_linked_object(
357+
"id", self.project_id, project.Project, exceptions.ProjectNotFound
358+
)
353359

354360
if self.type == "becomecurator" or self.type == "followstudio": # target is a studio
355361
if "target_id" in self.__dict__:
356-
return self._make_linked_object("id", self.target_id, studio.Studio, exceptions.StudioNotFound)
362+
return self._make_linked_object(
363+
"id", self.target_id, studio.Studio, exceptions.StudioNotFound
364+
)
357365
if "gallery_id" in self.__dict__:
358-
return self._make_linked_object("id", self.gallery_id, studio.Studio, exceptions.StudioNotFound)
366+
return self._make_linked_object(
367+
"id", self.gallery_id, studio.Studio, exceptions.StudioNotFound
368+
)
359369
# NOTE: the "becomecurator" type is ambigous - if it is inside the studio activity tab, the target is the user who joined
360370
if "username" in self.__dict__:
361-
return self._make_linked_object("username", self.username, user.User, exceptions.UserNotFound)
371+
return self._make_linked_object(
372+
"username", self.username, user.User, exceptions.UserNotFound
373+
)
362374

363375
if self.type == "followuser" or "curator" in self.type: # target is a user
364376
if "target_name" in self.__dict__:
365-
return self._make_linked_object("username", self.target_name, user.User, exceptions.UserNotFound)
377+
return self._make_linked_object(
378+
"username", self.target_name, user.User, exceptions.UserNotFound
379+
)
366380
if "followed_username" in self.__dict__:
367-
return self._make_linked_object("username", self.followed_username, user.User, exceptions.UserNotFound)
368-
if "recipient_username" in self.__dict__: # the recipient_username field always indicates the target is a user
369-
return self._make_linked_object("username", self.recipient_username, user.User, exceptions.UserNotFound)
381+
return self._make_linked_object(
382+
"username", self.followed_username, user.User, exceptions.UserNotFound
383+
)
384+
if (
385+
"recipient_username" in self.__dict__
386+
): # the recipient_username field always indicates the target is a user
387+
return self._make_linked_object(
388+
"username", self.recipient_username, user.User, exceptions.UserNotFound
389+
)
370390

371391
if self.type == "addcomment": # target is a comment
372392
if self.comment_type == 0:
373-
_c = project.Project(id=self.comment_obj_id, author_name=self._session.username,
374-
_session=self._session).comment_by_id(self.comment_id)
393+
_c = project.Project(
394+
id=self.comment_obj_id,
395+
author_name=self._session.username,
396+
_session=self._session,
397+
).comment_by_id(self.comment_id)
375398
if self.comment_type == 1:
376-
_c = user.User(username=self.comment_obj_title, _session=self._session).comment_by_id(self.comment_id)
399+
_c = user.User(
400+
username=self.comment_obj_title, _session=self._session
401+
).comment_by_id(self.comment_id)
377402
if self.comment_type == 2:
378-
_c = user.User(id=self.comment_obj_id, _session=self._session).comment_by_id(self.comment_id)
403+
_c = user.User(id=self.comment_obj_id, _session=self._session).comment_by_id(
404+
self.comment_id
405+
)
379406
else:
380407
raise ValueError(f"{self.comment_type} is an invalid comment type")
381408

0 commit comments

Comments
 (0)