Skip to content

Commit

Permalink
feat: 调整配置项结构
Browse files Browse the repository at this point in the history
  • Loading branch information
FHU-yezi committed Feb 18, 2024
1 parent 4039342 commit 6b0b399
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 88 deletions.
20 changes: 6 additions & 14 deletions jkit/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

class DataObject(Struct):
def _validate(self) -> Self:
from jkit.config import DATA_OBJECT_CONFIG
from jkit.config import CONFIG

if not DATA_OBJECT_CONFIG.enable_validation:
if not CONFIG.data_validation.enabled:
return self

try:
Expand Down Expand Up @@ -57,26 +57,18 @@ async def check(self) -> None:
raise NotImplementedError

async def _auto_check(self) -> None:
from jkit.config import RESOURCE_OBJECT_CONFIG
from jkit.config import CONFIG

if not RESOURCE_OBJECT_CONFIG.auto_checking:
if not CONFIG.resource_check.auto_check:
return

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

def _as_checked(self) -> Self:
"""将资源对象设置为已检查状态
from jkit.config import CONFIG

默认情况下,从 DataObject 获取的资源标识符创建的资源对象不进行检查。
此操作有利于提升性能。
可通过修改 `RESOURCE_OBJECT_CONFIG.force_check_object_from_data_object` 的值
改变此行为。
"""

from jkit.config import RESOURCE_OBJECT_CONFIG

if RESOURCE_OBJECT_CONFIG.force_check_object_from_data_object:
if CONFIG.resource_check.force_check_safe_data:
self._checked = True

return self
Expand Down
4 changes: 2 additions & 2 deletions jkit/_network_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from lxml.html import fromstring as parse_html
from msgspec.json import Decoder

from jkit.config import NETWORK_CONFIG
from jkit.config import CONFIG

HTTP_CLIENT = NETWORK_CONFIG._get_http_client()
HTTP_CLIENT = CONFIG.network._get_http_client()
JSON_DECODER = Decoder()


Expand Down
18 changes: 9 additions & 9 deletions jkit/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
normalize_percentage,
)
from jkit._utils import only_one
from jkit.config import ENDPOINT_CONFIG
from jkit.config import CONFIG
from jkit.exceptions import ResourceUnavailableError
from jkit.identifier_check import is_article_url
from jkit.identifier_convert import article_slug_to_url, article_url_to_slug
Expand Down Expand Up @@ -247,7 +247,7 @@ async def check(self) -> None:

try:
await get_json(
endpoint=ENDPOINT_CONFIG.jianshu,
endpoint=CONFIG.endpoints.jianshu,
path=f"/asimov/p/{self.slug}",
)
self._checked = True
Expand All @@ -265,7 +265,7 @@ async def info(self) -> ArticleInfo:
await self._auto_check()

data = await get_json(
endpoint=ENDPOINT_CONFIG.jianshu,
endpoint=CONFIG.endpoints.jianshu,
path=f"/asimov/p/{self.slug}",
)

Expand Down Expand Up @@ -328,7 +328,7 @@ async def views_count(self) -> int:
await self._auto_check()

data = await get_json(
endpoint=ENDPOINT_CONFIG.jianshu,
endpoint=CONFIG.endpoints.jianshu,
path=f"/shakespeare/v2/notes/{self.slug}/views_count",
)

Expand All @@ -339,7 +339,7 @@ async def audio_info(self) -> Optional[ArticleAudioInfo]:
await self._auto_check()

data = await get_json(
endpoint=ENDPOINT_CONFIG.jianshu,
endpoint=CONFIG.endpoints.jianshu,
path=f"/shakespeare/v2/notes/{self.slug}/audio",
)

Expand All @@ -360,7 +360,7 @@ async def belong_to_notebook(self) -> ArticleBelongToNotebookInfo:
await self._auto_check()

data = await get_json(
endpoint=ENDPOINT_CONFIG.jianshu,
endpoint=CONFIG.endpoints.jianshu,
path=f"/shakespeare/v2/notes/{self.slug}/book",
)

Expand All @@ -377,7 +377,7 @@ async def iter_included_collections(
now_page = start_page
while True:
data = await get_json(
endpoint=ENDPOINT_CONFIG.jianshu,
endpoint=CONFIG.endpoints.jianshu,
path=f"/shakespeare/notes/{await self.id}/included_collections",
params={"page": now_page, "count": page_size},
)
Expand Down Expand Up @@ -408,7 +408,7 @@ async def iter_comments(
now_page = start_page
while True:
data = await get_json(
endpoint=ENDPOINT_CONFIG.jianshu,
endpoint=CONFIG.endpoints.jianshu,
path=f"/shakespeare/notes/{await self.id}/comments",
params={
"page": now_page,
Expand Down Expand Up @@ -467,7 +467,7 @@ async def iter_featured_comments(
await self._auto_check()

data: List[Dict[str, Any]] = await get_json(
endpoint=ENDPOINT_CONFIG.jianshu,
endpoint=CONFIG.endpoints.jianshu,
path=f"/shakespeare/notes/{self.slug}/featured_comments",
params={
"count": count,
Expand Down
8 changes: 4 additions & 4 deletions jkit/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from jkit._network_request import get_json
from jkit._normalization import normalize_assets_amount, normalize_datetime
from jkit._utils import only_one
from jkit.config import ENDPOINT_CONFIG
from jkit.config import CONFIG
from jkit.exceptions import ResourceUnavailableError
from jkit.identifier_check import is_collection_url
from jkit.identifier_convert import collection_slug_to_url, collection_url_to_slug
Expand Down Expand Up @@ -143,7 +143,7 @@ async def check(self) -> None:

try:
await get_json(
endpoint=ENDPOINT_CONFIG.jianshu,
endpoint=CONFIG.endpoints.jianshu,
path=f"/asimov/collections/slug/{self.slug}",
)
self._checked = True
Expand All @@ -157,7 +157,7 @@ async def info(self) -> CollectionInfo:
await self._auto_check()

data = await get_json(
endpoint=ENDPOINT_CONFIG.jianshu,
endpoint=CONFIG.endpoints.jianshu,
path=f"/asimov/collections/slug/{self.slug}",
)

Expand Down Expand Up @@ -190,7 +190,7 @@ async def iter_articles(
now_page = start_page
while True:
data: List[Dict[str, Any]] = await get_json(
endpoint=ENDPOINT_CONFIG.jianshu,
endpoint=CONFIG.endpoints.jianshu,
path=f"/asimov/collections/slug/{self.slug}/public_notes",
params={
"page": now_page,
Expand Down
53 changes: 40 additions & 13 deletions jkit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@


class _NetworkConfig(ConfigObject, **CONFIG_CONFIG):
http_protool: Literal["HTTP/1", "HTTP/2"] = "HTTP/2"
"""网络配置"""

# 使用的传输协议,HTTP/2 有助于提升性能
protool: Literal["HTTP/1", "HTTP/2"] = "HTTP/2"

# 代理配置,与 HTTPX proxies 选项支持类型相同
proxies: Optional[ProxiesTypes] = None

# 请求超时,与 HTTPX timeout 选项支持类型相同
timeout: TimeoutTypes = 5

def _get_http_client(self) -> AsyncClient:
return AsyncClient(
http1=self.http_protool == "HTTP/1",
http2=self.http_protool == "HTTP/2",
http1=self.protool == "HTTP/1",
http2=self.protool == "HTTP/2",
proxies=self.proxies,
timeout=self.timeout,
)
Expand All @@ -28,20 +35,40 @@ def __setattr__(self, __name: str, __value: object) -> None:
jkit._network_request.HTTP_CLIENT = self._get_http_client()


class _EndpointConfig(ConfigObject, **CONFIG_CONFIG):
class _EndpointsConfig(ConfigObject, **CONFIG_CONFIG):
"""API 端点配置"""

jianshu: NonEmptyStr = "https://www.jianshu.com"


class _ResourceObjectConfig(ConfigObject, **CONFIG_CONFIG):
auto_checking: bool = True
force_check_object_from_data_object: bool = True
class _ResourceCheckConfig(ConfigObject, **CONFIG_CONFIG):
"""资源检查配置"""

# 从资源对象获取数据时自动进行资源检查
# 检查结果将在同对象中缓存,以避免不必要的开销
# 关闭后需要手动调用资源对象的 check 方法进行检查
# 否则可能抛出 jkit.exceptions 范围以外的异常
auto_check: bool = True

# 强制对从安全数据来源构建的资源对象进行资源检查
# 启用后可避免边界条件下的报错(如长时间保存资源对象)
# 这将对性能造成影响
force_check_safe_data: bool = False


class _DataValidationConfig(ConfigObject, **CONFIG_CONFIG):
"""数据校验配置"""

# 是否启用数据校验
# 遇特殊情况时可关闭以避免造成 ValidationError,此时不保证采集到的数据正确
enabled: bool = True


class _DataObjectConfig(ConfigObject, **CONFIG_CONFIG):
enable_validation: bool = True
class _Config(ConfigObject, **CONFIG_CONFIG):
network: _NetworkConfig = _NetworkConfig()
endpoints: _EndpointsConfig = _EndpointsConfig()
resource_check: _ResourceCheckConfig = _ResourceCheckConfig()
data_validation: _DataValidationConfig = _DataValidationConfig()


NETWORK_CONFIG = _NetworkConfig()
ENDPOINT_CONFIG = _EndpointConfig()
RESOURCE_OBJECT_CONFIG = _ResourceObjectConfig()
DATA_OBJECT_CONFIG = _DataObjectConfig()
CONFIG = _Config()
4 changes: 3 additions & 1 deletion jkit/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
USER_URL_REGEX = regex_compile(r"^https://www\.jianshu\.com/u/[a-zA-Z0-9]{6,12}/?$")
ARTICLE_URL_REGEX = regex_compile(r"^https://www\.jianshu\.com/p/[a-zA-Z0-9]{12}/?$")
NOTEBOOK_URL_REGEX = regex_compile(r"^https://www\.jianshu\.com/nb/\d{7,8}/?$")
COLLECTION_URL_REGEX = regex_compile(r"^https://www\.jianshu\.com/c/[a-zA-Z0-9]{6,12}/?$")
COLLECTION_URL_REGEX = regex_compile(
r"^https://www\.jianshu\.com/c/[a-zA-Z0-9]{6,12}/?$"
)
ISLAND_URL_REGEX = regex_compile(r"^https://www\.jianshu\.com/g/[a-zA-Z0-9]{16}/?$")

USER_SLUG_REGEX = regex_compile(r"^[a-zA-Z0-9]{6,12}$")
Expand Down
4 changes: 2 additions & 2 deletions jkit/jianshu_lottery.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)
from jkit._network_request import get_json
from jkit._normalization import normalize_datetime
from jkit.config import ENDPOINT_CONFIG
from jkit.config import CONFIG

if TYPE_CHECKING:
from jkit.user import User
Expand Down Expand Up @@ -42,7 +42,7 @@ async def iter_win_records(
self, *, count: int = 100
) -> AsyncGenerator[JianshuLotteryWinRecord, None]:
data: List[Dict[str, Any]] = await get_json(
endpoint=ENDPOINT_CONFIG.jianshu,
endpoint=CONFIG.endpoints.jianshu,
path="/asimov/ad_rewards/winner_list",
params={"count": count},
) # type: ignore
Expand Down
8 changes: 4 additions & 4 deletions jkit/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)
from jkit._network_request import get_json
from jkit._normalization import normalize_assets_amount, normalize_datetime
from jkit.config import ENDPOINT_CONFIG
from jkit.config import CONFIG
from jkit.exceptions import ResourceUnavailableError
from jkit.identifier_check import is_notebook_id
from jkit.identifier_convert import notebook_id_to_url
Expand Down Expand Up @@ -118,7 +118,7 @@ async def check(self) -> None:

try:
await get_json(
endpoint=ENDPOINT_CONFIG.jianshu, path=f"/asimov/nb/{self.id}"
endpoint=CONFIG.endpoints.jianshu, path=f"/asimov/nb/{self.id}"
)
self._checked = True
except HTTPStatusError:
Expand All @@ -129,7 +129,7 @@ async def info(self) -> NotebookInfo:
await self._auto_check()

data = await get_json(
endpoint=ENDPOINT_CONFIG.jianshu, path=f"/asimov/nb/{self.id}"
endpoint=CONFIG.endpoints.jianshu, path=f"/asimov/nb/{self.id}"
)

return NotebookInfo(
Expand Down Expand Up @@ -158,7 +158,7 @@ async def iter_articles(
now_page = start_page
while True:
data: List[Dict[str, Any]] = await get_json(
endpoint=ENDPOINT_CONFIG.jianshu,
endpoint=CONFIG.endpoints.jianshu,
path=f"/asimov/notebooks/{self.id}/public_notes",
params={
"page": now_page,
Expand Down
Loading

0 comments on commit 6b0b399

Please sign in to comment.