Skip to content

Commit deef5a7

Browse files
committed
ユーザーとコメントのdoc作成
1 parent 98760b8 commit deef5a7

File tree

3 files changed

+216
-6
lines changed

3 files changed

+216
-6
lines changed

scapi/sites/comment.py

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,24 @@
1212
)
1313

1414
class Comment(base._BaseSiteAPI[int]):
15+
"""
16+
コメントを表す。
1517
18+
.. note::
19+
プロジェクト欄でコメントに関するAPIを使用する場合、プロジェクトの作者のユーザー名が必要です。データが取得されていない可能性がある場合、古いAPIを使用することを検討してください。
20+
21+
.. note::
22+
プロフィール欄でコメントに関するAPIを使用する場合、必ず古いAPIが使用されます。新しいAPIでしか利用できない関数では TypeError が送出されることがあります。
23+
24+
Attributes:
25+
id (int): コメントID
26+
place (common.MAYBE_UNKNOWN[Project|Studio|User]): コメントの場所
27+
parent_id (common.MAYBE_UNKNOWN[int|None]): 親コメントID
28+
commentee_id (common.MAYBE_UNKNOWN[int|None]): メンションしたユーザーのID
29+
content (common.MAYBE_UNKNOWN[str]): コメントの内容
30+
author (common.MAYBE_UNKNOWN[User]): コメントしたユーザー
31+
reply_count (common.MAYBE_UNKNOWN[int]): コメントにある返信の数
32+
"""
1633
def __repr__(self) -> str:
1734
return f"<Comment id:{self.id} content:{self.content} place:{self.place} user:{self.author} Session:{self.session}>"
1835

@@ -108,14 +125,39 @@ def _update_from_html(self,data:bs4.element.Tag):
108125

109126
@property
110127
def created_at(self) -> datetime.datetime|common.UNKNOWN_TYPE:
128+
"""
129+
コメントが作成された時間を返す
130+
131+
Returns:
132+
datetime.datetime|UNKNOWN_TYPE: データがある場合、その時間。
133+
"""
111134
return common.dt_from_isoformat(self._created_at)
112135

113136
@property
114137
def modified_at(self) -> datetime.datetime|common.UNKNOWN_TYPE:
138+
"""
139+
コメントが最後に編集された時間を返す
140+
ユーザーがコメントを編集することはできないため、基本的に created_at と同じになります。
141+
142+
Returns:
143+
datetime.datetime|UNKNOWN_TYPE: データがある場合、その時間。
144+
"""
115145
return common.dt_from_isoformat(self._modified_at)
116146

117147

118148
async def get_replies(self,limit:int|None=None,offset:int|None=None,*,use_cache:bool=True) -> AsyncGenerator["Comment", None]:
149+
"""
150+
コメントの返信を取得する。
151+
ユーザーページや、作者の不明なプロジェクト欄では取得できません。
152+
153+
Args:
154+
limit (int|None, optional): 取得するコメントの数。初期値は40です。
155+
offset (int|None, optional): 取得するコメントの開始位置。初期値は0です。
156+
use_cache (bool, optional): 古いAPIから取得した時のキャッシュを使用するか。
157+
158+
Yields:
159+
Comment: 取得したコメント
160+
"""
119161
if use_cache and self._cached_reply is not None:
120162
if limit is None:
121163
limit = 40
@@ -129,6 +171,15 @@ async def get_replies(self,limit:int|None=None,offset:int|None=None,*,use_cache:
129171
yield Comment._create_from_data(_c["id"],_c,place=self.place)
130172

131173
async def get_parent(self,use_cache:bool=True) -> "Comment|None|common.UNKNOWN_TYPE":
174+
"""
175+
親コメントを取得する。
176+
177+
Args:
178+
use_cache (bool, optional): キャッシュを使用するか。デフォルトはTrueです。
179+
180+
Returns:
181+
Comment|None|common.UNKNOWN_TYPE: 取得できる場合、取得されたコメント
182+
"""
132183
if not isinstance(self.parent_id,int):
133184
return self.parent_id
134185
if self._cached_parent is None or use_cache:
@@ -197,12 +248,29 @@ async def post_reply(
197248
content:str,
198249
commentee:"user.User|int|None|common.UNKNOWN_TYPE"=common.UNKNOWN,
199250
is_old:bool=False
200-
):
251+
) -> "Comment":
252+
"""
253+
コメントを返信する。
254+
255+
Args:
256+
content (str): コメントの内容
257+
commentee (User|int|None, optional): メンションする場合、ユーザーかそのユーザーのID
258+
is_old (bool, optional): 古いAPIを使用して送信するか
259+
260+
Returns:
261+
comment.Comment: 投稿されたコメント
262+
"""
201263
if commentee is common.UNKNOWN:
202264
commentee = self.author and self.author.id or None
203265
return await Comment.post_comment(self.place,content,self.parent_id or self.id,commentee,is_old)
204266

205267
async def delete(self,is_old:bool=False):
268+
"""
269+
コメントを削除する。
270+
271+
Args:
272+
is_old (bool, optional): 古いAPIを使用するか
273+
"""
206274
self.require_session()
207275
if isinstance(self.place,user.User):
208276
is_old = True
@@ -219,6 +287,12 @@ async def delete(self,is_old:bool=False):
219287
await self.client.delete(url,json={"reportId":None})
220288

221289
async def report(self,is_old:bool=False):
290+
"""
291+
コメントを報告する。
292+
293+
Args:
294+
is_old (bool, optional): 古いAPIを使用するか
295+
"""
222296
self.require_session()
223297
if isinstance(self.place,user.User):
224298
is_old = True
@@ -227,9 +301,9 @@ async def report(self,is_old:bool=False):
227301
await self.client.post(url,json={"id":str(self.id)})
228302
else:
229303
if isinstance(self.place,project.Project):
230-
url = f"https://api.scratch.mit.edu/proxy/project/{self.place.id}/comment/{self.id}/report"
304+
url = f"https://api.scratch.mit.edu/proxy/project/{self.place.id}/comment/{self.id}/report"
231305
elif isinstance(self.place,studio.Studio):
232-
url = f"https://api.scratch.mit.edu/proxy/studio/{self.place.id}/comment/{self.id}/report"
306+
url = f"https://api.scratch.mit.edu/proxy/studio/{self.place.id}/comment/{self.id}/report"
233307
else:
234308
raise TypeError("User comment updates are not supported.")
235309
await self.client.post(url,json={"reportId":None})

scapi/sites/user.py

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,49 +59,117 @@ def _update_from_old_data(self, data:OldUserPayload):
5959

6060
@property
6161
def joined_at(self) -> datetime.datetime|common.UNKNOWN_TYPE:
62+
"""
63+
ユーザーが参加した時間を返す。
64+
65+
Returns:
66+
datetime.datetime|UNKNOWN_TYPE: データがある場合、その時間。
67+
"""
6268
return common.dt_from_isoformat(self._joined_at)
6369

6470

6571
async def get_featured(self) -> "project.ProjectFeatured|None":
72+
"""
73+
ユーザーの注目のプロジェクト欄を取得する。
74+
75+
Returns:
76+
project.ProjectFeatured|None: ユーザーが設定している場合、そのデータ。
77+
"""
6678
response = await self.client.get(f"https://scratch.mit.edu/site-api/users/all/{self.username}/")
6779
return project.ProjectFeatured(response.json(),self)
6880

6981
async def get_followers(self,limit:int|None=None,offset:int|None=None) -> AsyncGenerator["User", None]:
82+
"""
83+
ユーザーのフォロワーを取得する。
84+
85+
Args:
86+
limit (int|None, optional): 取得するユーザーの数。初期値は40です。
87+
offset (int|None, optional): 取得するユーザーの開始位置。初期値は0です。
88+
89+
Yields:
90+
User: 取得したユーザー
91+
"""
7092
async for _u in common.api_iterative(
7193
self.client,f"https://api.scratch.mit.edu/users/{self.username}/followers/",
7294
limit=limit,offset=offset
7395
):
7496
yield User._create_from_data(_u["username"],_u,self.client_or_session)
7597

7698
async def get_followings(self,limit:int|None=None,offset:int|None=None) -> AsyncGenerator["User", None]:
99+
"""
100+
ユーザーがフォローしているユーザーを取得する。
101+
102+
Args:
103+
limit (int|None, optional): 取得するユーザーの数。初期値は40です。
104+
offset (int|None, optional): 取得するユーザーの開始位置。初期値は0です。
105+
106+
Yields:
107+
User: 取得したユーザー
108+
"""
77109
async for _u in common.api_iterative(
78110
self.client,f"https://api.scratch.mit.edu/users/{self.username}/following/",
79111
limit=limit,offset=offset
80112
):
81113
yield User._create_from_data(_u["username"],_u,self.client_or_session)
82114

83115
async def get_projects(self,limit:int|None=None,offset:int|None=None) -> AsyncGenerator["project.Project", None]:
116+
"""
117+
ユーザーが共有しているプロジェクトを取得する。
118+
119+
Args:
120+
limit (int|None, optional): 取得するプロジェクトの数。初期値は40です。
121+
offset (int|None, optional): 取得するプロジェクトの開始位置。初期値は0です。
122+
123+
Yields:
124+
Project: 取得したプロジェクト
125+
"""
84126
async for _p in common.api_iterative(
85127
self.client,f"https://api.scratch.mit.edu/users/{self.username}/projects/",
86128
limit=limit,offset=offset
87129
):
88130
yield project.Project._create_from_data(_p["id"],_p,self.client_or_session)
89131

90132
async def get_favorites(self,limit:int|None=None,offset:int|None=None) -> AsyncGenerator["project.Project", None]:
133+
"""
134+
ユーザーのお気に入りのプロジェクトを取得する。
135+
136+
Args:
137+
limit (int|None, optional): 取得するプロジェクトの数。初期値は40です。
138+
offset (int|None, optional): 取得するプロジェクトの開始位置。初期値は0です。
139+
140+
Yields:
141+
Project: 取得したプロジェクト
142+
"""
91143
async for _p in common.api_iterative(
92144
self.client,f"https://api.scratch.mit.edu/users/{self.username}/favorites/",
93145
limit=limit,offset=offset
94146
):
95147
yield project.Project._create_from_data(_p["id"],_p,self.client_or_session)
96148

97149
async def get_studios(self,limit:int|None=None,offset:int|None=None) -> AsyncGenerator["studio.Studio", None]:
150+
"""
151+
ユーザーが参加しているスタジオを取得する。
152+
153+
Args:
154+
limit (int|None, optional): 取得するスタジオの数。初期値は40です。
155+
offset (int|None, optional): 取得するスタジオの開始位置。初期値は0です。
156+
157+
Yields:
158+
Studio: 取得したスタジオ
159+
"""
98160
async for _s in common.api_iterative(
99161
self.client,f"https://api.scratch.mit.edu/users/{self.username}/studios/curate",
100162
limit=limit,offset=offset
101163
):
102164
yield studio.Studio._create_from_data(_s["id"],_s,self.client_or_session)
103165

104166
async def get_message_count(self) -> int:
167+
"""
168+
ユーザーのメッセージの未読数を取得する。
169+
170+
Returns:
171+
int: 未読のメッセージの数
172+
"""
105173
response = await self.client.get(
106174
f"https://api.scratch.mit.edu/users/{self.username}/messages/count/",
107175
params={"cachebust":str(random.randint(0,10000))}
@@ -110,6 +178,16 @@ async def get_message_count(self) -> int:
110178
return data.get("count")
111179

112180
def get_comments(self,start_page:int|None=None,end_page:int|None=None) -> AsyncGenerator["comment.Comment", None]:
181+
"""
182+
プロフィールに投稿されたコメントを取得する。
183+
184+
Args:
185+
start_page (int|None, optional): 取得するコメントの開始ページ位置。初期値は1です。
186+
end_page (int|None, optional): 取得するコメントの終了ページ位置。初期値はstart_pageの値です。
187+
188+
Returns:
189+
Comment: 取得したコメント
190+
"""
113191
return comment.get_comment_from_old(self,start_page,end_page)
114192

115193
get_comments_from_old = get_comments
@@ -120,15 +198,35 @@ async def post_comment(
120198
parent:"comment.Comment|int|None"=None,commentee:"User|int|None"=None,
121199
is_old:bool=True
122200
) -> "comment.Comment":
201+
"""
202+
コメントを投稿します。
203+
204+
Args:
205+
content (str): コメントの内容
206+
parent (Comment|int|None, optional): 返信する場合、返信元のコメントかID
207+
commentee (User|int|None, optional): メンションする場合、ユーザーかそのユーザーのID
208+
is_old (bool, optional): 古いAPIを使用して送信するか この値は使用されず、常に古いAPIが使用されます。
209+
210+
Returns:
211+
comment.Comment: 投稿されたコメント
212+
"""
123213
return await comment.Comment.post_comment(self,content,parent,commentee,is_old)
124214

125215
async def follow(self):
216+
"""
217+
ユーザーをフォローする
218+
"""
219+
self.require_session()
126220
await self.client.put(
127221
f"https://scratch.mit.edu/site-api/users/followers/{self.username}/add/",
128222
params={"usernames":self._session.username}
129223
)
130224

131225
async def unfollow(self):
226+
"""
227+
ユーザーのフォローを解除する
228+
"""
229+
self.require_session()
132230
await self.client.put(
133231
f"https://scratch.mit.edu/site-api/users/followers/{self.username}/remove/",
134232
params={"usernames":self._session.username}
@@ -139,10 +237,25 @@ async def edit(
139237
self,*,
140238
bio:str|None=None,
141239
status:str|None=None,
142-
featured_project_id:int|None=None,
240+
featured_project_id:"int|project.Project|None"=None,
143241
featured_project_label:"ProjectFeaturedLabel|None"=None
144242
) -> "None | project.ProjectFeatured":
243+
"""
244+
プロフィール欄を編集する。
245+
246+
Args:
247+
bio (str | None, optional): 私について欄の内容
248+
status (str | None, optional): 私が取り組んでいることの内容
249+
featured_project_id (int|project.Project|None, optional): 注目のプロジェクト欄に設定したいプロジェクトかそのID
250+
featured_project_label (ProjectFeaturedLabel|None, optional): 注目のプロジェクト欄に使用したいラベル
251+
252+
Returns:
253+
None | project.ProjectFeatured: 変更された注目のプロジェクト欄
254+
"""
255+
self.require_session()
145256
_data = {}
257+
if isinstance(featured_project_id,project.Project):
258+
featured_project_id = featured_project_id.id
146259
if bio is not None: _data["bio"] = bio
147260
if status is not None: _data["status"] = status
148261
if featured_project_id is not None: _data["featured_project"] = featured_project_id
@@ -155,9 +268,20 @@ async def edit(
155268
return project.ProjectFeatured(data,self)
156269

157270
async def toggle_comment(self):
271+
"""
272+
プロフィールのコメント欄を開閉する。
273+
"""
274+
self.require_session()
158275
await self.client.post(f"https://scratch.mit.edu/site-api/comments/user/{self.username}/toggle-comments/")
159276

160277
async def set_icon(self,icon:file.File|bytes):
278+
"""
279+
アイコンを変更する。
280+
281+
Args:
282+
icon (file.File | bytes): アイコンのデータ
283+
"""
284+
self.require_session()
161285
async with file._read_file(icon) as f:
162286
self.require_session()
163287
await self.client.post(
@@ -167,6 +291,9 @@ async def set_icon(self,icon:file.File|bytes):
167291

168292

169293
class ProjectFeaturedLabel(Enum):
294+
"""
295+
注目のプロジェクト欄のラベルを表す。
296+
"""
170297
ProjectFeatured=""
171298
Tutorial="0"
172299
WorkInProgress="1"
@@ -185,4 +312,13 @@ async def get_from_id(cls,id:int|None) -> "ProjectFeaturedLabel":
185312
raise ValueError()
186313

187314
def get_user(username:str,*,_client:client.HTTPClient|None=None) -> common._AwaitableContextManager[User]:
315+
"""
316+
ユーザーを取得する。
317+
318+
Args:
319+
username (str): 取得したいユーザーのユーザー名
320+
321+
Returns:
322+
common._AwaitableContextManager[Studio]: await か async with で取得できるユーザー
323+
"""
188324
return common._AwaitableContextManager(User._create_from_api(username,_client))

scapi/utils/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from . import error,client,config
99

10-
__version__ = "3.0.0.dev1"
10+
__version__ = "3.0.0.dev2"
1111

1212
_KT = TypeVar("_KT")
1313
_VT = TypeVar("_VT")
@@ -184,7 +184,7 @@ class _AwaitableContextManager(Generic[_AT]):
184184
185185
obj = await coro または async with coro as obj: のようにして使用できます。
186186
187-
.. info::
187+
.. note::
188188
特別な理由がない限りは async with で使用すべきです。
189189
awaitのみで使用する場合は、最後に実行すべき関数 (.client_close())などを確認してください。
190190

0 commit comments

Comments
 (0)