-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathquickstart.py
More file actions
86 lines (63 loc) · 2.45 KB
/
quickstart.py
File metadata and controls
86 lines (63 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Local smoke test for the tikhub SDK.
Run with::
export TIKHUB_API_KEY="YOUR_API_KEY"
python examples/quickstart.py
Or pass the key inline::
python examples/quickstart.py YOUR_API_KEY
The script exercises 4 calls:
1. health_check.check -- no params, smallest possible call
2. tikhub_user.get_user_info -- your account / plan / quota
3. tikhub_user.get_user_daily_usage -- today's request count
4. douyin_web.fetch_one_video -- a real platform call
If any call fails, the script prints the typed exception with the
status code, URL, response body, and request id.
"""
from __future__ import annotations
import json
import sys
from tikhub import (
TikHub,
TikHubError,
TikHubHTTPError,
__version__,
)
# A widely shared Douyin video — replace with any aweme_id you like.
SAMPLE_AWEME_ID = "7345492945329900800"
def show(label: str, value: object) -> None:
print(f"\n=== {label} ===")
if isinstance(value, (dict, list)):
text = json.dumps(value, indent=2, ensure_ascii=False)
# Truncate huge responses so the terminal stays usable.
if len(text) > 2000:
text = text[:2000] + f"\n... ({len(text) - 2000} more chars)"
print(text)
else:
print(value)
def main() -> int:
api_key = sys.argv[1] if len(sys.argv) > 1 else None
print(f"tikhub {__version__}")
try:
with TikHub(api_key=api_key) as client:
# 1. Health check
show("1. health_check.check()", client.health_check.check())
# 2. Account info
show("2. tikhub_user.get_user_info()", client.tikhub_user.get_user_info())
# 3. Today's usage
show("3. tikhub_user.get_user_daily_usage()", client.tikhub_user.get_user_daily_usage())
# 4. Real platform call
show(
f"4. douyin_web.fetch_one_video(aweme_id={SAMPLE_AWEME_ID!r})",
client.douyin_web.fetch_one_video(aweme_id=SAMPLE_AWEME_ID),
)
except TikHubHTTPError as exc:
print(f"\nHTTP error {exc.status_code} on {exc.method} {exc.url}", file=sys.stderr)
print(f" request_id: {exc.request_id}", file=sys.stderr)
print(f" response: {exc.response_body}", file=sys.stderr)
return 1
except TikHubError as exc:
print(f"\nSDK error: {exc}", file=sys.stderr)
return 2
print("\nDone.")
return 0
if __name__ == "__main__":
raise SystemExit(main())