Skip to content

Commit

Permalink
feat: 添加 jkit.private.assets.Assets.assets_info,用于查询拥有 Token 的用户的精确…
Browse files Browse the repository at this point in the history
…资产数据
  • Loading branch information
FHU-yezi committed Feb 13, 2025
1 parent 3e4cbdd commit a290e22
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
26 changes: 13 additions & 13 deletions jkit/constants.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
36 changes: 36 additions & 0 deletions jkit/private/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
*,
Expand Down

0 comments on commit a290e22

Please sign in to comment.