python-xiaobiu 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_xiaobiu-0.1.0.dist-info/METADATA +68 -0
- python_xiaobiu-0.1.0.dist-info/RECORD +9 -0
- python_xiaobiu-0.1.0.dist-info/WHEEL +4 -0
- python_xiaobiu-0.1.0.dist-info/entry_points.txt +2 -0
- xiaobiu/__init__.py +35 -0
- xiaobiu/captcha_bridge.py +405 -0
- xiaobiu/client.py +1558 -0
- xiaobiu/crypto.py +66 -0
- xiaobiu/models.py +133 -0
xiaobiu/crypto.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
from functools import lru_cache
|
|
5
|
+
|
|
6
|
+
from cryptography.hazmat.primitives import padding, serialization
|
|
7
|
+
from cryptography.hazmat.primitives.asymmetric import padding as asymmetric_padding
|
|
8
|
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
|
9
|
+
|
|
10
|
+
SUNING_AES_KEY = b"www.cnsuning.com"
|
|
11
|
+
SUNING_AES_IV = b"moc.gninusnc.www"
|
|
12
|
+
STANDARD_BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
|
|
13
|
+
SUNING_BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789()*"
|
|
14
|
+
STANDARD_TO_SUNING_BASE64 = str.maketrans(
|
|
15
|
+
STANDARD_BASE64_ALPHABET,
|
|
16
|
+
SUNING_BASE64_ALPHABET,
|
|
17
|
+
)
|
|
18
|
+
SUNING_TO_STANDARD_BASE64 = str.maketrans(
|
|
19
|
+
SUNING_BASE64_ALPHABET,
|
|
20
|
+
STANDARD_BASE64_ALPHABET,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def encode_suning_base64(raw: bytes) -> str:
|
|
25
|
+
return base64.b64encode(raw).decode("ascii").translate(STANDARD_TO_SUNING_BASE64)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def decode_suning_base64(encoded: str) -> bytes:
|
|
29
|
+
return base64.b64decode(encoded.translate(SUNING_TO_STANDARD_BASE64))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class SuAESCipher:
|
|
33
|
+
def __init__(self, key: bytes = SUNING_AES_KEY, iv: bytes = SUNING_AES_IV) -> None:
|
|
34
|
+
self._key = key
|
|
35
|
+
self._iv = iv
|
|
36
|
+
|
|
37
|
+
def encrypt(self, plaintext: str) -> str:
|
|
38
|
+
padder = padding.PKCS7(algorithms.AES.block_size).padder()
|
|
39
|
+
padded = padder.update(plaintext.encode("utf-8")) + padder.finalize()
|
|
40
|
+
cipher = Cipher(algorithms.AES(self._key), modes.CBC(self._iv))
|
|
41
|
+
encryptor = cipher.encryptor()
|
|
42
|
+
encrypted = encryptor.update(padded) + encryptor.finalize()
|
|
43
|
+
return encode_suning_base64(encrypted)
|
|
44
|
+
|
|
45
|
+
def decrypt(self, ciphertext: str) -> str:
|
|
46
|
+
cipher = Cipher(algorithms.AES(self._key), modes.CBC(self._iv))
|
|
47
|
+
decryptor = cipher.decryptor()
|
|
48
|
+
decrypted = decryptor.update(decode_suning_base64(ciphertext)) + decryptor.finalize()
|
|
49
|
+
unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
|
|
50
|
+
plaintext = unpadder.update(decrypted) + unpadder.finalize()
|
|
51
|
+
return plaintext.decode("utf-8")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@lru_cache(maxsize=8)
|
|
55
|
+
def _load_public_key(public_key_base64: str):
|
|
56
|
+
key_bytes = base64.b64decode(public_key_base64)
|
|
57
|
+
return serialization.load_der_public_key(key_bytes)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def rsa_encrypt_base64(plaintext: str, public_key_base64: str) -> str:
|
|
61
|
+
public_key = _load_public_key(public_key_base64)
|
|
62
|
+
encrypted = public_key.encrypt(
|
|
63
|
+
plaintext.encode("utf-8"),
|
|
64
|
+
asymmetric_padding.PKCS1v15(),
|
|
65
|
+
)
|
|
66
|
+
return base64.b64encode(encrypted).decode("ascii")
|
xiaobiu/models.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
from uuid import uuid4
|
|
5
|
+
|
|
6
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SuningBaseModel(BaseModel):
|
|
10
|
+
model_config = ConfigDict(extra="ignore", validate_assignment=True)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class LoginPageConfig(SuningBaseModel):
|
|
14
|
+
login_pbk: str
|
|
15
|
+
rdsy_key: str
|
|
16
|
+
rdsy_app_code: str
|
|
17
|
+
step_flag: str
|
|
18
|
+
step_two_flag: str
|
|
19
|
+
step_three_flag: str
|
|
20
|
+
rdsy_scene_id: str
|
|
21
|
+
rdsy_scene_id_yghk: str
|
|
22
|
+
channel: str
|
|
23
|
+
check_account_key: str
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class AuthState(SuningBaseModel):
|
|
27
|
+
phone_number: str | None = None
|
|
28
|
+
international_code: str = "0086"
|
|
29
|
+
detect: str = "passport_detect_js_is_error"
|
|
30
|
+
dfp_token: str = "passport_dfpToken_js_is_error"
|
|
31
|
+
risk_type: str | None = None
|
|
32
|
+
sms_ticket: str | None = None
|
|
33
|
+
login_ticket: str | None = None
|
|
34
|
+
login_response: dict[str, Any] | None = None
|
|
35
|
+
updated_at: float | None = None
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class CaptchaSolution(SuningBaseModel):
|
|
39
|
+
kind: str
|
|
40
|
+
value: str
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class SignedRequestTemplate(SuningBaseModel):
|
|
44
|
+
method: str
|
|
45
|
+
url: str
|
|
46
|
+
headers: dict[str, str] = Field(default_factory=dict)
|
|
47
|
+
body: str = ""
|
|
48
|
+
har_path: str | None = None
|
|
49
|
+
|
|
50
|
+
def build_headers(self) -> dict[str, str]:
|
|
51
|
+
trace_id = uuid4().hex
|
|
52
|
+
headers: dict[str, str] = {}
|
|
53
|
+
for name, value in self.headers.items():
|
|
54
|
+
lower_name = name.lower()
|
|
55
|
+
if lower_name in {"host", "cookie", "content-length"} or lower_name.startswith(":"):
|
|
56
|
+
continue
|
|
57
|
+
if lower_name in {"sntraceid", "hiro_trace_id"}:
|
|
58
|
+
headers[name] = trace_id
|
|
59
|
+
continue
|
|
60
|
+
headers[name] = value
|
|
61
|
+
return headers
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class FamilyInfo(SuningBaseModel):
|
|
65
|
+
family_id: str
|
|
66
|
+
name: str
|
|
67
|
+
raw: dict[str, Any] = Field(default_factory=dict)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class HAClimatePreview(SuningBaseModel):
|
|
71
|
+
entity_domain: str
|
|
72
|
+
translation_key: str
|
|
73
|
+
available: bool
|
|
74
|
+
hvac_mode: str | None = None
|
|
75
|
+
current_temperature: float | None = None
|
|
76
|
+
target_temperature: float | None = None
|
|
77
|
+
fan_mode: str | None = None
|
|
78
|
+
swing_mode: str | None = None
|
|
79
|
+
preset_mode: str | None = None
|
|
80
|
+
supported_features_preview: list[str] = Field(default_factory=list)
|
|
81
|
+
raw_mapping: dict[str, dict[str, Any]] = Field(default_factory=dict)
|
|
82
|
+
notes: list[str] = Field(default_factory=list)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class AirConditionerStatus(SuningBaseModel):
|
|
86
|
+
device_id: str
|
|
87
|
+
name: str
|
|
88
|
+
model: str | None = None
|
|
89
|
+
family_id: str | None = None
|
|
90
|
+
group_id: str | None = None
|
|
91
|
+
group_name: str | None = None
|
|
92
|
+
category_id: str | None = None
|
|
93
|
+
available: bool
|
|
94
|
+
online: bool
|
|
95
|
+
summary: str | None = None
|
|
96
|
+
device_record_time: str | None = None
|
|
97
|
+
refresh_time: str | None = None
|
|
98
|
+
power_on: bool | None = None
|
|
99
|
+
current_temperature: float | None = None
|
|
100
|
+
target_temperature: float | None = None
|
|
101
|
+
outdoor_temperature: float | None = None
|
|
102
|
+
mode_raw: str | None = None
|
|
103
|
+
fan_mode_raw: str | None = None
|
|
104
|
+
swing_horizontal: bool | None = None
|
|
105
|
+
swing_vertical: bool | None = None
|
|
106
|
+
eco_enabled: bool | None = None
|
|
107
|
+
purify_enabled: bool | None = None
|
|
108
|
+
fresh_air_enabled: bool | None = None
|
|
109
|
+
electric_heating_enabled: bool | None = None
|
|
110
|
+
ha_climate_preview: HAClimatePreview | None = None
|
|
111
|
+
raw_status: dict[str, Any] = Field(default_factory=dict)
|
|
112
|
+
raw_device: dict[str, Any] = Field(default_factory=dict)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class SerializedCookie(SuningBaseModel):
|
|
116
|
+
name: str
|
|
117
|
+
value: str
|
|
118
|
+
domain: str
|
|
119
|
+
path: str
|
|
120
|
+
secure: bool = False
|
|
121
|
+
expires: int | float | None = None
|
|
122
|
+
rest: dict[str, Any] = Field(default_factory=dict)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class PersistedSessionState(SuningBaseModel):
|
|
126
|
+
state: AuthState = Field(default_factory=AuthState)
|
|
127
|
+
cookies: list[SerializedCookie] = Field(default_factory=list)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
class CaptchaBridgeResult(SuningBaseModel):
|
|
131
|
+
token: str
|
|
132
|
+
detect: str | None = None
|
|
133
|
+
dfp_token: str | None = None
|