From a290e22e6171c0ec9195eb3b3b79bf190c98d348 Mon Sep 17 00:00:00 2001 From: FHU-yezi Date: Thu, 13 Feb 2025 09:54:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20`jkit.private.asse?= =?UTF-8?q?ts.Assets.assets=5Finfo`=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8B=A5=E6=9C=89=20Token=20=E7=9A=84=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=9A=84=E7=B2=BE=E7=A1=AE=E8=B5=84=E4=BA=A7=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jkit/constants.py | 26 +++++++++++++------------- jkit/private/assets.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 13 deletions(-) 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, *,