Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions scratchattach/site/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
from dataclasses import dataclass
from typing import Optional, Any
from enum import Enum
from datetime import datetime

from bs4 import Tag

from . import user, project, studio, session, forum
from .typed_dicts import ActivityDict
from ._base import BaseSiteComponent
from scratchattach.utils import exceptions

Expand Down Expand Up @@ -72,7 +74,8 @@ class Activity(BaseSiteComponent):
changed_fields: Optional[dict[str, str]] = None
is_reshare: Optional[bool] = None

datetime_created: Optional[str] = None
datetime_created: Optional[datetime] = None
datetime_created_raw: Optional[str] = None
time: Any = None
type: Optional[ActivityTypes] = None

Expand Down Expand Up @@ -180,7 +183,7 @@ def update(self):
print("Warning: Activity objects can't be updated")
return False # Objects of this type cannot be updated

def _update_from_dict(self, data):
def _update_from_dict(self, data: ActivityDict):
self.raw = data

self._session = data.get("_session", self._session)
Expand Down Expand Up @@ -214,7 +217,8 @@ def _update_from_dict(self, data):
self.changed_fields = data.get("changed_fields", self.changed_fields)
self.is_reshare = data.get("is_reshare", self.is_reshare)

self.datetime_created = data.get("datetime_created", self.datetime_created)
self.datetime_created_raw = data.get("datetime_created", self.datetime_created_raw)
self.datetime_created = datetime.fromisoformat(self.datetime_created_raw)
self.time = data.get("time", self.time)

_type = data.get("type", self.type)
Expand Down Expand Up @@ -252,7 +256,8 @@ def _update_from_json(self, data: dict):
self.actor_username = username
self.username = username
self.raw = data
self.datetime_created = _time
self.datetime_created_raw = _time
self.datetime_created = datetime.fromisoformat(self.datetime_created_raw)
if activity_type == 0:
self.type = ActivityTypes.followuser
self.followed_username = data["followed_username"]
Expand Down Expand Up @@ -339,7 +344,8 @@ def _update_from_html(self, data: Tag):
while '\xa0' in _time:
_time = _time.replace('\xa0', ' ')

self.datetime_created = _time
self.datetime_created_raw = _time
self.datetime_created = datetime.fromisoformat(self.datetime_created_raw)
self.actor_username = data.find('div').find('span').text

self.target_name = data.find('div').find('span').find_next().text
Expand Down
6 changes: 3 additions & 3 deletions scratchattach/site/classroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def __post_init__(self):
self._headers = commons.headers
self._cookies = {}
else:
self._headers = self._session._headers
self._cookies = self._session._cookies
self._headers = self._session.get_headers()
self._cookies = self._session.get_cookies()

# Headers for operations that require accept and Content-Type fields:
self._json_headers = {**self._headers,
Expand Down Expand Up @@ -98,7 +98,7 @@ def update(self):
"educator": {},
"status": status,
"is_closed": True
}
}

if educator_username:
ret["educator"]["username"] = educator_username
Expand Down
33 changes: 33 additions & 0 deletions scratchattach/site/typed_dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,36 @@ class PlaceholderProjectDataDict(TypedDict):
metadata: PlaceholderProjectDataMetadataDict
md5extsToSha256: OrderedDict[str, str]
adminOwnershipToken: Optional[str]

class ActivityDict(TypedDict):
id: int
datetime_created: str
actor_username: str
actor_id: int
type: str

# addcomment
comment_type: NotRequired[int]
comment_obj_id: NotRequired[int]
comment_id: NotRequired[int]
comment_fragment: NotRequired[str]
comment_obj_title: NotRequired[str]
commentee_username: NotRequired[Optional[str]]

# forumpost
topic_id: NotRequired[int]
topic_title: NotRequired[str]

# followuser
followed_user_id: NotRequired[int]
followed_username: NotRequired[str]

# loveproject
project_id: NotRequired[int]
title: NotRequired[str]

# favoriteproject
project_title: NotRequired[str]

# curatorinvite & studioactivity
gallery_id: NotRequired[int]
Loading