Skip to content

Commit ced63d7

Browse files
committed
partial activity dict and datetime attribute for activity
1 parent d52f5ec commit ced63d7

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

scratchattach/site/activity.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
from dataclasses import dataclass
88
from typing import Optional, Any
99
from enum import Enum
10+
from datetime import datetime
1011

1112
from bs4 import Tag
1213

1314
from . import user, project, studio, session, forum
15+
from .typed_dicts import ActivityDict
1416
from ._base import BaseSiteComponent
1517
from scratchattach.utils import exceptions
1618

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

75-
datetime_created: Optional[str] = None
77+
datetime_created: Optional[datetime] = None
78+
datetime_created_raw: Optional[str] = None
7679
time: Any = None
7780
type: Optional[ActivityTypes] = None
7881

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

183-
def _update_from_dict(self, data):
186+
def _update_from_dict(self, data: ActivityDict):
184187
self.raw = data
185188

186189
self._session = data.get("_session", self._session)
@@ -214,7 +217,8 @@ def _update_from_dict(self, data):
214217
self.changed_fields = data.get("changed_fields", self.changed_fields)
215218
self.is_reshare = data.get("is_reshare", self.is_reshare)
216219

217-
self.datetime_created = data.get("datetime_created", self.datetime_created)
220+
self.datetime_created_raw = data.get("datetime_created", self.datetime_created_raw)
221+
self.datetime_created = datetime.fromisoformat(self.datetime_created_raw)
218222
self.time = data.get("time", self.time)
219223

220224
_type = data.get("type", self.type)
@@ -252,7 +256,8 @@ def _update_from_json(self, data: dict):
252256
self.actor_username = username
253257
self.username = username
254258
self.raw = data
255-
self.datetime_created = _time
259+
self.datetime_created_raw = _time
260+
self.datetime_created = datetime.fromisoformat(self.datetime_created_raw)
256261
if activity_type == 0:
257262
self.type = ActivityTypes.followuser
258263
self.followed_username = data["followed_username"]
@@ -339,7 +344,8 @@ def _update_from_html(self, data: Tag):
339344
while '\xa0' in _time:
340345
_time = _time.replace('\xa0', ' ')
341346

342-
self.datetime_created = _time
347+
self.datetime_created_raw = _time
348+
self.datetime_created = datetime.fromisoformat(self.datetime_created_raw)
343349
self.actor_username = data.find('div').find('span').text
344350

345351
self.target_name = data.find('div').find('span').find_next().text

scratchattach/site/classroom.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def __post_init__(self):
5151
self._headers = commons.headers
5252
self._cookies = {}
5353
else:
54-
self._headers = self._session._headers
55-
self._cookies = self._session._cookies
54+
self._headers = self._session.get_headers()
55+
self._cookies = self._session.get_cookies()
5656

5757
# Headers for operations that require accept and Content-Type fields:
5858
self._json_headers = {**self._headers,
@@ -98,7 +98,7 @@ def update(self):
9898
"educator": {},
9999
"status": status,
100100
"is_closed": True
101-
}
101+
}
102102

103103
if educator_username:
104104
ret["educator"]["username"] = educator_username

scratchattach/site/typed_dicts.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,36 @@ class PlaceholderProjectDataDict(TypedDict):
147147
metadata: PlaceholderProjectDataMetadataDict
148148
md5extsToSha256: OrderedDict[str, str]
149149
adminOwnershipToken: Optional[str]
150+
151+
class ActivityDict(TypedDict):
152+
id: int
153+
datetime_created: str
154+
actor_username: str
155+
actor_id: int
156+
type: str
157+
158+
# addcomment
159+
comment_type: NotRequired[int]
160+
comment_obj_id: NotRequired[int]
161+
comment_id: NotRequired[int]
162+
comment_fragment: NotRequired[str]
163+
comment_obj_title: NotRequired[str]
164+
commentee_username: NotRequired[Optional[str]]
165+
166+
# forumpost
167+
topic_id: NotRequired[int]
168+
topic_title: NotRequired[str]
169+
170+
# followuser
171+
followed_user_id: NotRequired[int]
172+
followed_username: NotRequired[str]
173+
174+
# loveproject
175+
project_id: NotRequired[int]
176+
title: NotRequired[str]
177+
178+
# favoriteproject
179+
project_title: NotRequired[str]
180+
181+
# curatorinvite & studioactivity
182+
gallery_id: NotRequired[int]

0 commit comments

Comments
 (0)