服务端 SDK(Python)—— 接收报告,接入你的服务器
你的后端做两件事:接收报告回调(验签、存报告),以及可选地用服务端密钥取回结果。
01 安装
Python SDK 暂未发布到公共软件源,现在没有可安装的包。本页的验签只用 Python 标准库;接收请求的部分用 Web 框架的写法示意。
02 不用客户端 SDK:从你的后端上传和提交
服务端已有视频、小程序、其他语言这类用不了客户端 SDK 的场景,由你的后端用服务端密钥上传和提交:① 调 POST /v1/upload/presign,请求体 {"filename": "…", "content_type": "…"}(filename 必填),返回 upload_id、put_url 等;② 把视频文件原样 PUT 到 put_url,不要改动它的查询串;③ 调 POST /v1/analyze/full/submit(需要 analyze:write),在 input 里放上一步的 upload_id,或者一个公网能访问的 video_url;可选 client_ref、callback_url、consent。要传完整的原视频,不能只传片段。返回 task_id 和 status: "pending",之后等 Webhook,或用 GET /v1/analyze/status/{task_id} 查询;两份报告都在结果里。
import json, time, urllib.request API_BASE = "https://open.hslingxin.com" # production gateway base URL API_KEY = "sk_YOUR_KEY" # your server key def call(method, path, body=None): request = urllib.request.Request( API_BASE + path, data=None if body is None else json.dumps(body).encode(), headers={"X-API-Key": API_KEY, "Content-Type": "application/json"}, method=method, ) with urllib.request.urlopen(request) as response: # raises on a non-2xx status return json.load(response) # 1. ask for an upload address (filename is required) upload = call("POST", "/v1/upload/presign", {"filename": "session.mp4", "content_type": "video/mp4"}) # 2. PUT the video file to put_url as is; don't change its query string with open("session.mp4", "rb") as f: put = urllib.request.Request(upload["put_url"], data=f.read(), method="PUT", headers={"Content-Type": "video/mp4"}) urllib.request.urlopen(put) # 3. submit the uploaded video; the complete original video, not segments task = call("POST", "/v1/analyze/full/submit", {"input": {"upload_id": upload["upload_id"]}, "client_ref": "user_20394"}) # task = {"task_id": ..., "status": "pending"}; wait for the webhook, or read the status status = call("GET", f"/v1/analyze/status/{task['task_id']}")
03 接收报告回调
平台把报告 POST 到你的 Webhook。验签后存下来。签名密钥是控制台的 whsec_…——不是你的 sk_。
# ② Backend webhook receiver (verify with whsec_ → dedupe → correlate → store · sk_ for fallback fetch) import hmac, hashlib, time def verify(sig, ts, body, secret): # body = raw request bytes if abs(time.time() - int(ts)) > 300: # reject stale deliveries (>5 min) — replay protection raise ValueError("stale timestamp") msg = f"{ts}.".encode() + body # timestamp + "." + raw body expected = "sha256=" + hmac.new(secret.encode(), msg, hashlib.sha256).hexdigest() if not hmac.compare_digest(expected, sig): raise ValueError("bad signature") @app.post("/hooks/lynxin") def on_report(request): verify(request.headers["X-Lynxin-Signature"], request.headers["X-Lynxin-Timestamp"], request.raw_body, WEBHOOK_SECRET) # whsec_ webhook signing secret delivery_id = request.headers["X-Lynxin-Delivery"] if already_handled(delivery_id): # a delivery can arrive more than once — dedupe by delivery ID return event = request.json if event["event"] == "analysis.completed": user_id = event["client_ref"] # correlate to your user reports = event["result"] # result.model1 = signal report · result.model2 = personality report save_report(user_id, reports) # store permanently mark_handled(delivery_id) # fallback: within 30 days, fetch with sk_ via GET /v1/results?client_ref=user_20394
感知版报告。只用感知版时,用服务端密钥调用 GET /v1/rdp/{session_id}/report 读取完整报告(需要 report:read 权限,服务端密钥默认带)。session_id 来自上传的返回和 ingest.completed 事件。
import json, time, urllib.request API_BASE = "https://open.hslingxin.com" # production gateway base URL API_KEY = "sk_YOUR_KEY" # your server key def call(method, path, body=None): request = urllib.request.Request( API_BASE + path, data=None if body is None else json.dumps(body).encode(), headers={"X-API-Key": API_KEY, "Content-Type": "application/json"}, method=method, ) with urllib.request.urlopen(request) as response: # raises on a non-2xx status return json.load(response) # session_id comes from the upload response and from the ingest.completed event report = call("GET", f"/v1/rdp/{session_id}/report")
04 可选:逐句转写、语音情绪与 Pro 视频曲线
只调 uploadAndSubmit() 时,感知版报告默认不含音频分析和 Pro 视频曲线;调 ingestAndAnalyze() 的会自动补齐,不用调下面的接口。需要时由你的后端用服务端密钥调用抽取:逐句转写和语音情绪用 POST /v1/rdp/{session_id}/audio,或异步的 POST /v1/jobs/audio-extract;Pro 视频曲线用 POST /v1/rdp/{session_id}/pro(engine 取 standard 或 high),或异步的 POST /v1/jobs/pro-extract。异步任务用 GET /v1/jobs/{job_id} 查结果。成功后再从 GET /v1/rdp/{session_id}/report 读一次报告,结果就在同一份报告里。抽取按音视频的实际时长折算成 token,再按积分扣费;异步的 Pro 视频抽取会先预扣积分,积分不够时返回 402。
import json, time, urllib.request API_BASE = "https://open.hslingxin.com" # production gateway base URL API_KEY = "sk_YOUR_KEY" # your server key def call(method, path, body=None): request = urllib.request.Request( API_BASE + path, data=None if body is None else json.dumps(body).encode(), headers={"X-API-Key": API_KEY, "Content-Type": "application/json"}, method=method, ) with urllib.request.urlopen(request) as response: # raises on a non-2xx status return json.load(response) # synchronous: extract the audio of this session call("POST", f"/v1/rdp/{session_id}/audio") # asynchronous: submit a job linked to the session, then check it until it ends # video_ref comes from the upload response, returned with session_id job = call("POST", "/v1/jobs/audio-extract", {"video_ref": video_ref, "session_id": session_id}) while True: state = call("GET", f"/v1/jobs/{job['job_id']}") if state["status"] in ("success", "failed"): # pending | running | success | failed break time.sleep(5) # read the report again; the transcript is in the same report report = call("GET", f"/v1/rdp/{session_id}/report")
05 投递漏了怎么办
Webhook 是主路径,多数情况下你只需要它。若某次投递重试全部失败,可在控制台(投递日志)重发。完整的重试计划与兜底选项见 Webhooks → 重试与兜底。