diff --git a/jkit/constants.py b/jkit/constants.py index cf59412..a4a6147 100644 --- a/jkit/constants.py +++ b/jkit/constants.py @@ -1,29 +1,29 @@ -from re import compile as regex_compile +from re import compile as re_compile -ARTICLE_SLUG_REGEX = COLLECTION_SLUG_REGEX = USER_SLUG_REGEX = regex_compile( +ARTICLE_SLUG_REGEX = COLLECTION_SLUG_REGEX = USER_SLUG_REGEX = re_compile( r"^[a-z0-9]{12}$|^[a-zA-z0-9]{6}$" ) -ISLAND_SLUG_REGEX = regex_compile(r"^[a-z0-9]{16}$") +ISLAND_SLUG_REGEX = re_compile(r"^[a-z0-9]{16}$") -ARTICLE_URL_REGEX = regex_compile( +ARTICLE_URL_REGEX = re_compile( r"^https://www\.jianshu\.com/p/([a-z0-9]{12}|[a-zA-z0-9]{6})/?$" ) -COLLECTION_URL_REGEX = regex_compile( +COLLECTION_URL_REGEX = re_compile( r"^https://www\.jianshu\.com/c/([a-z0-9]{12}|[a-zA-z0-9]{6})/?$" ) -ISLAND_URL_REGEX = regex_compile(r"^https://www\.jianshu\.com/g/[a-zA-Z0-9]{16}/?$") -NOTEBOOK_URL_REGEX = regex_compile(r"^https://www\.jianshu\.com/nb/\d{6,8}/?$") -USER_URL_REGEX = regex_compile( +ISLAND_URL_REGEX = re_compile(r"^https://www\.jianshu\.com/g/[a-zA-Z0-9]{16}/?$") +NOTEBOOK_URL_REGEX = re_compile(r"^https://www\.jianshu\.com/nb/\d{6,8}/?$") +USER_URL_REGEX = re_compile( r"^https://www\.jianshu\.com/u/([a-z0-9]{12}|[a-zA-z0-9]{6})/?$" ) -USER_NAME_REGEX = regex_compile(r"^[\w]{,15}$") +USER_NAME_REGEX = re_compile(r"^[\w]{,15}$") -JIANSHU_URL_REGEX = regex_compile(r"^https://www\.jianshu\.com/[a-zA-Z0-9/]*/?$") -USER_UPLOADED_URL_REGEX = regex_compile(r"^https?:\/\/.*/?$") +JIANSHU_URL_REGEX = re_compile(r"^https://www\.jianshu\.com/[a-zA-Z0-9/]*/?$") +USER_UPLOADED_URL_REGEX = re_compile(r"^https?:\/\/.*/?$") -_HTML_TAG_REGEX = regex_compile("<.*?>") -_BLANK_LINES_REGEX = regex_compile("\n{2,}") +_HTML_TAG_REGEX = re_compile("<.*?>") +_BLANK_LINES_REGEX = re_compile("\n{2,}") _NOTEBOOK_ID_MIN = 100000 _NOTEBOOK_ID_MAX = 99999999 diff --git a/jkit/private/assets.py b/jkit/private/assets.py index a0cc83e..eeb8e74 100644 --- a/jkit/private/assets.py +++ b/jkit/private/assets.py @@ -3,6 +3,7 @@ from collections.abc import AsyncGenerator from contextlib import suppress from decimal import Decimal +from re import compile as re_compile from typing import Literal from httpx import HTTPStatusError @@ -27,10 +28,19 @@ from jkit.credentials import JianshuCredential from jkit.exceptions import BalanceNotEnoughError, WeeklyConvertLimitExceededError +_HTML_INNER_JSON_REGEX = re_compile(r"__INITIAL_STATE__=(.*);\(function") + AssetsTransactionType = Literal["INCOME", "EXPENSE"] BenefitCardType = Literal["PENDING", "ACTIVE", "EXPIRED"] +class AssetsInfoData(DataObject, frozen=True): + fp_amount: Decimal + ftn_amount: Decimal + assets_amount: Decimal + converting_fp_amount: Decimal + + class TransactionData(DataObject, frozen=True): id: PositiveInt time: NormalizedDatetime @@ -64,6 +74,32 @@ class Assets(ResourceObject): def __init__(self, *, credential: JianshuCredential) -> None: self._credential = credential + @property + async def assets_info(self) -> AssetsInfoData: + html = await send_request( + datasource="JIANSHU", + method="GET", + path="/mobile/fp/", + cookies=self._credential.cookies, + response_type="HTML", + ) + data = JSON_DECODER.decode(_HTML_INNER_JSON_REGEX.findall(html)[0]) + + return AssetsInfoData( + fp_amount=normalize_assets_amount_precise( + data["ruby"]["wallet"]["assets"]["jsd_amount18"] + ), + ftn_amount=normalize_assets_amount_precise( + data["ruby"]["wallet"]["assets"]["jsb_amount18"] + ), + assets_amount=normalize_assets_amount_precise( + data["ruby"]["wallet"]["assets"]["total_assets18"] + ), + converting_fp_amount=normalize_assets_amount_precise( + data["ruby"]["wallet"]["assets"]["exchanging_jsb18"] + ), + )._validate() + async def iter_transactions( self, *,