@honor-claw/yoyo 1.2.0 → 1.2.1-beta.0

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.
@@ -0,0 +1,99 @@
1
+ import time
2
+ import hmac
3
+ import hashlib
4
+ import base64
5
+ import json
6
+ import requests
7
+ from datetime import datetime
8
+ import argparse
9
+
10
+
11
+ def gen_headers():
12
+ # ak / sk 建议线上不要写死在代码里,放到环境变量或配置文件中
13
+ ak = "DB1178355DDE4CD2"
14
+ sk = "4764F7B34264E8F4343AC7363AA6D4"
15
+
16
+ # 对应:var ts = Math.floor(Date.now()).toString();
17
+ # Date.now() 是毫秒,这里同样用毫秒时间戳
18
+ ts = str(int(time.time() * 1000))
19
+
20
+ # 对应:CryptoJS.HmacSHA256(ts + ak, sk).toString().toUpperCase()
21
+ msg = (ts + ak).encode("utf-8")
22
+ key = sk.encode("utf-8")
23
+ digest = hmac.new(key, msg, hashlib.sha256).hexdigest().upper()
24
+
25
+ # 对应:new Buffer(...).toString('base64');
26
+ # 即对十六进制字符串做 base64 编码
27
+ sign = base64.b64encode(digest.encode("utf-8")).decode("utf-8")
28
+
29
+ headers = {
30
+ "accessKey": ak,
31
+ "ts": ts,
32
+ "sign": sign,
33
+ "Content-Type": "application/json",
34
+ }
35
+ return headers
36
+
37
+
38
+ def get_current_local_time_str():
39
+ """
40
+ 生成形如:2025-12-23 12:48:00.000 的时间字符串
41
+ 使用当前系统时间
42
+ """
43
+ now = datetime.now()
44
+ # %f 是微秒,取前 3 位当毫秒
45
+ return now.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
46
+
47
+
48
+ def parse_args():
49
+ parser = argparse.ArgumentParser(
50
+ description="调用 time-agent 接口的命令行工具"
51
+ )
52
+ parser.add_argument(
53
+ "--query",
54
+ # required=True,
55
+ default="大小周下周六休息7点",
56
+ help="语义查询内容(必填)",
57
+ )
58
+ parser.add_argument(
59
+ "--type",
60
+ dest="req_type",
61
+ default="time",
62
+ help="payload.data.type,默认值为 'time'",
63
+ )
64
+ parser.add_argument(
65
+ "--timeZone",
66
+ dest="time_zone",
67
+ default="+0800",
68
+ help="payload.data.timeZone,默认值为 '+0800'",
69
+ )
70
+ return parser.parse_args()
71
+
72
+
73
+ def main():
74
+ url = "https://ai-model-access-drcn.rnd.honor.com/time-agent/v1/models/model/predict"
75
+ args = parse_args()
76
+
77
+ payload = {
78
+ "data": {
79
+ "type": args.req_type,
80
+ "timeZone": args.time_zone,
81
+ "query": args.query,
82
+ "localTime": get_current_local_time_str()
83
+ }
84
+ }
85
+
86
+ headers = gen_headers()
87
+
88
+ resp = requests.post(url, headers=headers, data=json.dumps(payload))
89
+ print("Status code:", resp.status_code)
90
+ try:
91
+ datas = resp.json()
92
+ res = json.dumps(datas.get("data", {}), separators=(",", ":"), ensure_ascii=False)
93
+ print({"data_time": res})
94
+ except Exception:
95
+ print("Response text:", resp.text)
96
+
97
+
98
+ if __name__ == "__main__":
99
+ main()