python-xiaobiu 0.2.3__py3-none-any.whl → 0.2.4__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: python-xiaobiu
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Summary: Suning biu smart home SMS login and session client
5
5
  Project-URL: Homepage, https://github.com/FaintGhost/python-xiaobiu
6
6
  Project-URL: Bug Tracker, https://github.com/FaintGhost/python-xiaobiu/issues
@@ -112,6 +112,19 @@ client.list_device_timers(family_id=37790, device_id="...")
112
112
  client.get_device_panel_template(family_id=37790, device_id="...")
113
113
  ```
114
114
 
115
+ ### Session renewal
116
+
117
+ ```python
118
+ client.silent_login() # renew the passport ticket from the stored cookies
119
+ client.keep_alive() # renew, then confirm the API still answers
120
+ ```
121
+
122
+ Mirrors `AutoLoginPresenter.autoLogin`: the app re-posts
123
+ `https://passport.suning.com/ids/login` with **no credentials** and
124
+ `service=https://shcss.suning.com/shcss-web/`. A live session answers
125
+ `ok` and redirects to `?ticket=ST...`; an expired one answers the login
126
+ page JSON, which raises `AuthenticationError`.
127
+
115
128
  ### Timers
116
129
 
117
130
  ```python
@@ -1,18 +1,19 @@
1
- xiaobiu/__init__.py,sha256=vuubbRNBH7hN-QW8_-aYQcOjj10OQp26w_7imNU9KO8,837
1
+ xiaobiu/__init__.py,sha256=CokyQWRkaJGRUONI0GpmW9OlB-aXEfEGJaoXbBx1MoU,988
2
2
  xiaobiu/ac_control.py,sha256=JK9anHEPbvSr50nq2oqfTZbxZTlwwWRSPpcCCSm9nP0,25940
3
- xiaobiu/ac_status.py,sha256=62ecNZyeUm9SUz0NhVCVj4mcTB3KB78lLQgNNf1P4yY,14915
3
+ xiaobiu/ac_status.py,sha256=_aburbpy3HPX2VVm9BdpvzwVl5hDxS-RV0gFbObTgs4,15964
4
4
  xiaobiu/app_api.py,sha256=gJLa-iq60s_YEEM00E4lMcTsBKoV60GtaaqBsWWOWio,4729
5
5
  xiaobiu/captcha_bridge.py,sha256=l8c3HnspQt1JDM1sDKnvcM4yvf7NZosSQ7x2AlO-kEA,12920
6
6
  xiaobiu/cli.py,sha256=rKbvt7rsV_OcNr5N62JIRhveKzyDwtu2MApSdeDj_fA,18810
7
- xiaobiu/client.py,sha256=o-1ks6mlH5wUMksGYBDJpoPdSRVngggp7pNmCxJv-UY,23969
7
+ xiaobiu/client.py,sha256=J7aodIyuttdeF13z8PyNsK1OEtq57k480HvfcaCl1nE,24164
8
8
  xiaobiu/crypto.py,sha256=C0Lcp2Cfy-075hv65b1RYfn6UbiHneLhp4PhUftzzOk,2424
9
9
  xiaobiu/exceptions.py,sha256=RKaity_UctboUGeC8QE0iF0KcqfYqlVHnfOQ2yXQT7c,1036
10
10
  xiaobiu/har_templates.py,sha256=clatrCsn4_AxlSrpy99mJVThI34bFQpQQMoM-4H9e08,5562
11
11
  xiaobiu/models.py,sha256=h6lhH0GQIgH9zUwmQLBBAY-SJBZjYr5LMxXqiRVrDOI,6223
12
12
  xiaobiu/parsers.py,sha256=5Cb90sN5rw_SNv2BukOoiLmeG9Mxegq-XGkgE8YkxvU,3046
13
13
  xiaobiu/persistence.py,sha256=k9ZES5DyE-OkptlLLw0jnBWwGtdDxgrn0YiVhAQ1V8c,887
14
+ xiaobiu/session.py,sha256=c5WJlAQ59v1Wf9oleYDOHtdlQVzkJ9QduQmIZEsjICU,2126
14
15
  xiaobiu/sms_login.py,sha256=5zQS_lhFvADdp6BnoenvSLdOSK1hWaT35rQQ4K3Ig-w,16394
15
- python_xiaobiu-0.2.3.dist-info/METADATA,sha256=pMTeNbfjzG2Ccmqmeg-tAljPON24Xx87gNK9c3Z1yyo,13198
16
- python_xiaobiu-0.2.3.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
17
- python_xiaobiu-0.2.3.dist-info/entry_points.txt,sha256=w1z8h63RRusoSGAbbSMpNcq4qbN3HOedsMlxwuFH3U8,44
18
- python_xiaobiu-0.2.3.dist-info/RECORD,,
16
+ python_xiaobiu-0.2.4.dist-info/METADATA,sha256=dC35Cl0UNA8rqPfuM-_VE6xh9Ppan5c0HkFCMNHBmzY,13693
17
+ python_xiaobiu-0.2.4.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
18
+ python_xiaobiu-0.2.4.dist-info/entry_points.txt,sha256=w1z8h63RRusoSGAbbSMpNcq4qbN3HOedsMlxwuFH3U8,44
19
+ python_xiaobiu-0.2.4.dist-info/RECORD,,
xiaobiu/__init__.py CHANGED
@@ -7,6 +7,11 @@ from .client import (
7
7
  main,
8
8
  parse_jsonp_or_json,
9
9
  )
10
+ from .session import (
11
+ SILENT_LOGIN_SERVICE,
12
+ SILENT_LOGIN_URL,
13
+ silent_login,
14
+ )
10
15
  from .models import (
11
16
  AirConditionerStatus,
12
17
  AuthState,
@@ -42,7 +47,10 @@ __all__ = [
42
47
  "SignedRequestTemplate",
43
48
  "SuningError",
44
49
  "SuningSmartHomeClient",
50
+ "SILENT_LOGIN_SERVICE",
51
+ "SILENT_LOGIN_URL",
45
52
  "SwingMode",
53
+ "silent_login",
46
54
  "Timer",
47
55
  "main",
48
56
  "parse_jsonp_or_json",
xiaobiu/ac_status.py CHANGED
@@ -18,6 +18,7 @@ from .app_api import (
18
18
  decode_app_api_response,
19
19
  is_login_redirect,
20
20
  )
21
+ from . import session
21
22
  from .exceptions import AuthenticationError, SuningError
22
23
  from .models import (
23
24
  AirConditionerStatus,
@@ -412,20 +413,38 @@ def bootstrap_service(client: Any, service_name: str) -> dict[str, Any]:
412
413
 
413
414
 
414
415
  def query_member_base_info(client: Any) -> dict[str, Any]:
416
+ """Read the member profile, following the passport ticket redirect.
417
+
418
+ The endpoint always answers ``302`` to passport with a per-call
419
+ ``service`` parameter; following it makes passport mint a service
420
+ ticket from the ``TGC`` cookie and hand control back with JSON. Only a
421
+ response that *ends* on the login page means the session is gone.
422
+ """
423
+
415
424
  response = client.session.get(
416
425
  MEMBER_BASE_INFO_URL,
417
426
  timeout=client.timeout,
418
- allow_redirects=False,
427
+ allow_redirects=True,
419
428
  )
420
- if is_login_redirect(response):
421
- bootstrap_service(client, "shcss")
429
+ if "/ids/login" in str(response.url):
430
+ try:
431
+ session.silent_login(client)
432
+ except SuningError:
433
+ bootstrap_service(client, "shcss")
422
434
  response = client.session.get(
423
435
  MEMBER_BASE_INFO_URL,
424
436
  timeout=client.timeout,
425
- allow_redirects=False,
437
+ allow_redirects=True,
426
438
  )
439
+ if "/ids/login" in str(response.url):
440
+ raise AuthenticationError("member base info redirected to login")
427
441
  response.raise_for_status()
428
- data = response.json()
442
+ try:
443
+ data = response.json()
444
+ except ValueError as error:
445
+ raise AuthenticationError(
446
+ "member base info returned a non-JSON body"
447
+ ) from error
429
448
  if data.get("code") != "0":
430
449
  raise AuthenticationError(data.get("desc") or "member base info request failed")
431
450
  client._touch_state()
@@ -433,8 +452,16 @@ def query_member_base_info(client: Any) -> dict[str, Any]:
433
452
 
434
453
 
435
454
  def keep_alive(client: Any) -> dict[str, Any]:
455
+ """Renew the passport ticket first, then confirm the API accepts it.
456
+
457
+ The app re-posts the login form with no credentials on start-up
458
+ (``AutoLoginPresenter.autoLogin``) so the ticket never goes stale; doing
459
+ the same here means a quiet renewal instead of a loud reauth prompt.
460
+ """
461
+
462
+ renewal = session.silent_login(client)
436
463
  member_info = query_member_base_info(client)
437
- return {"member": member_info}
464
+ return {"renewal": renewal, "member": member_info}
438
465
 
439
466
 
440
467
  __all__ = [
xiaobiu/client.py CHANGED
@@ -26,6 +26,7 @@ from requests.cookies import create_cookie
26
26
 
27
27
  from . import (
28
28
  ac_control,
29
+ session,
29
30
  ac_status,
30
31
  app_api,
31
32
  exceptions,
@@ -296,9 +297,15 @@ class SuningSmartHomeClient:
296
297
  def query_member_base_info(self) -> dict[str, Any]:
297
298
  return ac_status.query_member_base_info(self)
298
299
 
300
+ def silent_login(self) -> dict[str, Any]:
301
+ """Renew the passport ticket using the cookies already stored."""
302
+
303
+ return session.silent_login(self)
304
+
299
305
  def keep_alive(self) -> dict[str, Any]:
300
- member_info = self.query_member_base_info()
301
- return {"member": member_info}
306
+ """Renew the ticket and confirm the API still accepts the session."""
307
+
308
+ return ac_status.keep_alive(self)
302
309
 
303
310
  # ------------------------------------------------------------------
304
311
  # app-api plumbing (kept as thin forwards so the legacy
xiaobiu/session.py ADDED
@@ -0,0 +1,67 @@
1
+ """Session renewal that mirrors the app's silent login.
2
+
3
+ ``AutoLoginPresenter.autoLogin`` posts the passport login form with no
4
+ credentials at all: the existing cookies are what identifies the user, and
5
+ the ``service`` parameter decides which site's ticket passport mints. A
6
+ successful call answers ``ok`` and redirects to ``<service>?ticket=ST...``;
7
+ an expired session answers the login-page JSON instead.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from typing import Any
13
+
14
+ from .exceptions import AuthenticationError, SuningError
15
+
16
+ SILENT_LOGIN_URL = "https://passport.suning.com/ids/login"
17
+ SILENT_LOGIN_SERVICE = "https://shcss.suning.com/shcss-web/"
18
+ SILENT_LOGIN_THEME = "zn"
19
+ SILENT_LOGIN_CHANNEL = "MOBILE"
20
+ SILENT_LOGIN_REMEMBER_ME_TYPE = "app"
21
+
22
+
23
+ def build_silent_login_payload(client: Any) -> dict[str, str]:
24
+ """Form fields ``autoLogin`` sends; credentials are deliberately absent."""
25
+
26
+ return {
27
+ "rememberMe": "true",
28
+ "rememberMeType": SILENT_LOGIN_REMEMBER_ME_TYPE,
29
+ "service": SILENT_LOGIN_SERVICE,
30
+ "dfpToken": str(getattr(client.state, "dfp_token", "") or ""),
31
+ "loginTheme": SILENT_LOGIN_THEME,
32
+ "loginChannel": SILENT_LOGIN_CHANNEL,
33
+ "terminal": SILENT_LOGIN_CHANNEL,
34
+ "jsonViewType": "true",
35
+ }
36
+
37
+
38
+ def silent_login(client: Any) -> dict[str, Any]:
39
+ """Renew the session with the cookies already in the session jar."""
40
+
41
+ response = client.session.post(
42
+ SILENT_LOGIN_URL,
43
+ data=build_silent_login_payload(client),
44
+ timeout=client.timeout,
45
+ allow_redirects=True,
46
+ )
47
+ body = (response.text or "").strip()
48
+ ticket = "ticket=ST" in str(response.url)
49
+ if ticket or body.lower() == "ok":
50
+ client._touch_state()
51
+ return {
52
+ "renewed": True,
53
+ "final_url": response.url,
54
+ "body": body[:120],
55
+ }
56
+ if "loginPage" in body or '"res_code":-1' in body:
57
+ raise AuthenticationError("静默登录失败:会话已失效,请重新登录")
58
+ raise SuningError(f"静默登录返回了无法识别的响应: {body[:120]}")
59
+
60
+
61
+ __all__ = [
62
+ "SILENT_LOGIN_CHANNEL",
63
+ "SILENT_LOGIN_SERVICE",
64
+ "SILENT_LOGIN_URL",
65
+ "build_silent_login_payload",
66
+ "silent_login",
67
+ ]