Skip to content

Commit

Permalink
refactor: 重新设计文集模块
Browse files Browse the repository at this point in the history
  • Loading branch information
FHU-yezi committed Jan 30, 2025
1 parent 008dceb commit ac3a988
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions jkit/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
T = TypeVar("T", bound="Notebook")


class AuthorInfoField(DataObject, frozen=True):
class _AuthorInfoField(DataObject, frozen=True):
slug: UserSlug
name: UserName
avatar_url: UserUploadedUrl
Expand All @@ -48,18 +48,18 @@ def to_user_obj(self) -> User:
return User.from_slug(self.slug)._as_checked()


class NotebookInfo(DataObject, frozen=True):
class InfoData(DataObject, frozen=True):
id: NotebookId
name: NonEmptyStr
description_updated_at: NormalizedDatetime
author_info: AuthorInfoField
description_update_time: NormalizedDatetime
author_info: _AuthorInfoField

articles_count: NonNegativeInt
subscribers_count: NonNegativeInt
total_wordage: NonNegativeInt


class ArticleAuthorInfoField(DataObject, frozen=True):
class _ArticleAuthorInfoField(DataObject, frozen=True):
id: PositiveInt
slug: UserSlug
name: UserName
Expand All @@ -71,7 +71,7 @@ def to_user_obj(self) -> User:
return User.from_slug(self.slug)._as_checked()


class NotebookArticleInfo(DataObject, frozen=True):
class ArticleData(DataObject, frozen=True):
id: PositiveInt
slug: ArticleSlug
title: NonEmptyStr
Expand All @@ -80,7 +80,7 @@ class NotebookArticleInfo(DataObject, frozen=True):
published_at: NormalizedDatetime
is_paid: bool
can_comment: bool
author_info: ArticleAuthorInfoField
author_info: _ArticleAuthorInfoField

views_count: NonNegativeInt
likes_count: NonNegativeInt
Expand Down Expand Up @@ -129,7 +129,7 @@ async def check(self) -> None:
self._checked = True

@property
async def info(self) -> NotebookInfo:
async def info(self) -> InfoData:
await self._require_check()

data = await send_request(
Expand All @@ -139,11 +139,11 @@ async def info(self) -> NotebookInfo:
response_type="JSON",
)

return NotebookInfo(
return InfoData(
id=data["id"],
name=data["name"],
description_updated_at=normalize_datetime(data["last_updated_at"]),
author_info=AuthorInfoField(
description_update_time=normalize_datetime(data["last_updated_at"]),
author_info=_AuthorInfoField(
slug=data["user"]["slug"],
name=data["user"]["nickname"],
avatar_url=data["user"]["avatar"],
Expand All @@ -158,8 +158,7 @@ async def iter_articles(
*,
start_page: int = 1,
order_by: Literal["ADD_TIME", "LAST_COMMENT_TIME"] = "ADD_TIME",
page_size: int = 20,
) -> AsyncGenerator[NotebookArticleInfo, None]:
) -> AsyncGenerator[ArticleData, None]:
await self._require_check()

current_page = start_page
Expand All @@ -170,7 +169,7 @@ async def iter_articles(
path=f"/asimov/notebooks/{self.id}/public_notes",
body={
"page": current_page,
"count": page_size,
"count": 20,
"order_by": {
"ADD_TIME": "added_at",
"LAST_COMMENT_TIME": "commented_at",
Expand All @@ -183,32 +182,30 @@ async def iter_articles(
return

for item in data:
yield NotebookArticleInfo(
id=item["object"]["data"]["id"],
slug=item["object"]["data"]["slug"],
title=item["object"]["data"]["title"],
description=item["object"]["data"]["public_abbr"],
image_url=item["object"]["data"]["list_image_url"]
if item["object"]["data"]["list_image_url"]
item = item["object"]["data"] # noqa: PLW2901

yield ArticleData(
id=item["id"],
slug=item["slug"],
title=item["title"],
description=item["public_abbr"],
image_url=item["list_image_url"]
if item["list_image_url"]
else None,
published_at=normalize_datetime(
item["object"]["data"]["first_shared_at"]
),
is_paid=item["object"]["data"]["paid"],
can_comment=item["object"]["data"]["commentable"],
author_info=ArticleAuthorInfoField(
id=item["object"]["data"]["user"]["id"],
slug=item["object"]["data"]["user"]["slug"],
name=item["object"]["data"]["user"]["nickname"],
avatar_url=item["object"]["data"]["user"]["avatar"],
),
views_count=item["object"]["data"]["views_count"],
likes_count=item["object"]["data"]["likes_count"],
comments_count=item["object"]["data"]["public_comments_count"],
tips_count=item["object"]["data"]["total_rewards_count"],
earned_fp_amount=normalize_assets_amount(
item["object"]["data"]["total_fp_amount"]
published_at=normalize_datetime(item["first_shared_at"]),
is_paid=item["paid"],
can_comment=item["commentable"],
author_info=_ArticleAuthorInfoField(
id=item["user"]["id"],
slug=item["user"]["slug"],
name=item["user"]["nickname"],
avatar_url=item["user"]["avatar"],
),
views_count=item["views_count"],
likes_count=item["likes_count"],
comments_count=item["public_comments_count"],
tips_count=item["total_rewards_count"],
earned_fp_amount=normalize_assets_amount(item["total_fp_amount"]),
)._validate()

current_page += 1

0 comments on commit ac3a988

Please sign in to comment.