Skip to content

Commit

Permalink
refactor: 重构资源对象检查逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
FHU-yezi committed Jan 26, 2025
1 parent aeb400a commit 9c0c1d2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 38 deletions.
10 changes: 5 additions & 5 deletions jkit/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ def __init__(self) -> None:
async def check(self) -> None:
raise NotImplementedError

async def _auto_check(self) -> None:
if not CONFIG.resource_check.auto_check:
async def _require_check(self) -> None:
if self._checked or not CONFIG.resource_check.auto_check:
return

if not self._checked:
await self.check()
await self.check()
self._checked = True

def _as_checked(self: P1) -> P1:
if CONFIG.resource_check.force_check_safe_data:
if not CONFIG.resource_check.force_check_safe_data:
self._checked = True

return self
Expand Down
18 changes: 7 additions & 11 deletions jkit/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,13 @@ def __init__(
)

async def check(self) -> None:
if self._checked:
return

try:
await send_request(
datasource="JIANSHU",
method="GET",
path=f"/asimov/p/{self.slug}",
response_type="JSON",
)
self._checked = True
except HTTPStatusError as e:
if e.response.status_code == _RESOURCE_UNAVAILABLE_STATUS_CODE:
raise ResourceUnavailableError(
Expand All @@ -248,7 +244,7 @@ async def id(self) -> int:

@property
async def info(self) -> ArticleInfo:
await self._auto_check()
await self._require_check()

data = await send_request(
datasource="JIANSHU",
Expand Down Expand Up @@ -313,7 +309,7 @@ async def info(self) -> ArticleInfo:

@property
async def views_count(self) -> int:
await self._auto_check()
await self._require_check()

data = await send_request(
datasource="JIANSHU",
Expand All @@ -326,7 +322,7 @@ async def views_count(self) -> int:

@property
async def audio_info(self) -> ArticleAudioInfo | None:
await self._auto_check()
await self._require_check()

data = await send_request(
datasource="JIANSHU",
Expand All @@ -349,7 +345,7 @@ async def audio_info(self) -> ArticleAudioInfo | None:

@property
async def belong_to_notebook(self) -> ArticleBelongToNotebookInfo:
await self._auto_check()
await self._require_check()

data = await send_request(
datasource="JIANSHU",
Expand All @@ -366,7 +362,7 @@ async def belong_to_notebook(self) -> ArticleBelongToNotebookInfo:
async def iter_included_collections(
self, *, start_page: int = 1, page_size: int = 10
) -> AsyncGenerator[ArticleIncludedCollectionInfo, None]:
await self._auto_check()
await self._require_check()

now_page = start_page
while True:
Expand Down Expand Up @@ -399,7 +395,7 @@ async def iter_comments(
author_only: bool = False,
page_size: int = 10,
) -> AsyncGenerator[ArticleCommentInfo, None]:
await self._auto_check()
await self._require_check()

now_page = start_page
while True:
Expand Down Expand Up @@ -462,7 +458,7 @@ async def iter_featured_comments(
*,
count: int = 10,
) -> AsyncGenerator[ArticleFeaturedCommentInfo, None]:
await self._auto_check()
await self._require_check()

data = await send_request(
datasource="JIANSHU",
Expand Down
8 changes: 2 additions & 6 deletions jkit/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,13 @@ def __init__(self, *, slug: str | None = None, url: str | None = None) -> None:
)

async def check(self) -> None:
if self._checked:
return

try:
await send_request(
datasource="JIANSHU",
method="GET",
path=f"/asimov/collections/slug/{self.slug}",
response_type="JSON",
)
self._checked = True
except HTTPStatusError as e:
if e.response.status_code == _RESOURCE_UNAVAILABLE_STATUS_CODE:
raise ResourceUnavailableError(
Expand All @@ -134,7 +130,7 @@ async def check(self) -> None:

@property
async def info(self) -> CollectionInfo:
await self._auto_check()
await self._require_check()

data = await send_request(
datasource="JIANSHU",
Expand Down Expand Up @@ -167,7 +163,7 @@ async def iter_articles(
order_by: Literal["ADD_TIME", "LAST_COMMENT_TIME", "POPULARITY"] = "ADD_TIME",
page_size: int = 20,
) -> AsyncGenerator[CollectionArticleInfo, None]:
await self._auto_check()
await self._require_check()

now_page = start_page
while True:
Expand Down
9 changes: 2 additions & 7 deletions jkit/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ def to_article_obj(self) -> Article:
class Notebook(ResourceObject, CheckableMixin, IdAndUrlMixin):
def __init__(self, *, id: int) -> None:
super().__init__()
self._checked = False

if not is_notebook_id(id):
raise ValueError(f"文集 ID 无效:{id}")
Expand All @@ -116,17 +115,13 @@ def url(self) -> str:
return notebook_id_to_url(self._id)

async def check(self) -> None:
if self._checked:
return

try:
await send_request(
datasource="JIANSHU",
method="GET",
path=f"/asimov/nb/{self.id}",
response_type="JSON",
)
self._checked = True
except HTTPStatusError as e:
if e.response.status_code == _RESOURCE_UNAVAILABLE_STATUS_CODE:
raise ResourceUnavailableError(
Expand All @@ -137,7 +132,7 @@ async def check(self) -> None:

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

data = await send_request(
datasource="JIANSHU",
Expand Down Expand Up @@ -167,7 +162,7 @@ async def iter_articles(
order_by: Literal["ADD_TIME", "LAST_COMMENT_TIME"] = "ADD_TIME",
page_size: int = 20,
) -> AsyncGenerator[NotebookArticleInfo, None]:
await self._auto_check()
await self._require_check()

now_page = start_page
while True:
Expand Down
1 change: 0 additions & 1 deletion jkit/ranking/user_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from jkit._base import DataObject, ResourceObject
from jkit._network import send_request
from jkit._normalization import normalize_assets_amount
from jkit.config import CONFIG
from jkit.exceptions import ResourceUnavailableError
from jkit.identifier_convert import user_slug_to_url
from jkit.msgspec_constraints import (
Expand Down
13 changes: 5 additions & 8 deletions jkit/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,6 @@ def __init__(self, *, slug: str | None = None, url: str | None = None) -> None:
)

async def check(self) -> None:
if self._checked:
return

try:
await send_request(
datasource="JIANSHU",
Expand All @@ -188,7 +185,7 @@ async def id(self) -> int:

@property
async def info(self) -> UserInfo:
await self._auto_check()
await self._require_check()

data = await send_request(
datasource="JIANSHU",
Expand Down Expand Up @@ -280,7 +277,7 @@ async def assets_info(self) -> tuple[float, float | None, float | None]:
async def iter_owned_collections(
self, *, start_page: int = 1, page_size: int = 10
) -> AsyncGenerator[UserCollectionInfo, None]:
await self._auto_check()
await self._require_check()

now_page = start_page
while True:
Expand Down Expand Up @@ -312,7 +309,7 @@ async def iter_owned_collections(
async def iter_managed_collections(
self, *, start_page: int = 1, page_size: int = 10
) -> AsyncGenerator[UserCollectionInfo, None]:
await self._auto_check()
await self._require_check()

now_page = start_page
while True:
Expand Down Expand Up @@ -344,7 +341,7 @@ async def iter_managed_collections(
async def iter_notebooks(
self, *, start_page: int = 1, page_size: int = 10
) -> AsyncGenerator[UserNotebookInfo, None]:
await self._auto_check()
await self._require_check()

now_page = start_page
while True:
Expand Down Expand Up @@ -382,7 +379,7 @@ async def iter_articles(
] = "PUBLISHED_AT",
page_size: int = 10,
) -> AsyncGenerator[UserArticleInfo, None]:
await self._auto_check()
await self._require_check()

now_page = start_page
while True:
Expand Down

0 comments on commit 9c0c1d2

Please sign in to comment.