This repository was archived by the owner on Oct 23, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcheckin.py
More file actions
186 lines (158 loc) · 7.06 KB
/
Copy pathcheckin.py
File metadata and controls
186 lines (158 loc) · 7.06 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Trae 每日签到脚本(GitHub Actions 版)
原理:
Trae 网页端的 JWT 只有 8 小时有效期,但真正的会话凭证是 HttpOnly Cookie
`X-Cloudide-Session`(约 14 天有效)。本脚本通过该 Cookie 调用
`GetUserToken` 接口换取全新 JWT,再用新 JWT 执行每日签到。
依赖:仅标准库,无第三方依赖。
环境变量:
TRAE_SESSION 账号 1 的 X-Cloudide-Session Cookie(必填;后端兼容单账号部署)
TRAE_DEVICE_ID 账号 1 的 x-device-id,16 位数字(选填,缺省随机)
TRAE_SESSION_N 第 N(N≥2) 个账号的会话 Cookie;缺失即停止读取更多账号
TRAE_DEVICE_ID_N 第 N 个账号的 x-device-id(选填,缺省随机)
全部账号共享: FEISHU_WEBHOOK(选填,签到后推送一条汇总)
用法:
python checkin.py
"""
import datetime
import json
import os
import random
import sys
import time
import urllib.request
BASE = "https://api.trae.cn"
def _post(path, headers, body=""):
"""POST 请求。非 2xx 不抛 HTTPError,而是以 (status, body) 正常返回,便于上层判断原因。"""
import urllib.error
req = urllib.request.Request(BASE + path, data=body.encode("utf-8"), headers=headers, method="POST")
try:
with urllib.request.urlopen(req, timeout=30) as resp:
return resp.status, resp.read().decode("utf-8", errors="replace")
except urllib.error.HTTPError as e:
return e.code, e.read().decode("utf-8", errors="replace")
def get_token(session: str) -> str:
"""用 X-Cloudide-Session Cookie 换取全新 JWT。"""
headers = {
"Cookie": "X-Cloudide-Session=" + session,
"Referer": "https://www.trae.cn/",
"Origin": "https://www.trae.cn",
"User-Agent": "TraeCheckin/1.0",
"Accept": "application/json, text/plain, */*",
}
status, text = _post("/cloudide/api/v3/common/GetUserToken", headers)
data = json.loads(text)
token = (data.get("Result") or {}).get("Token")
if status == 401:
raise RuntimeError(
"账号会话已失效(HTTP 401):X-Cloudide-Session 可能已过期(~14天有效期)。"
"请在浏览器重新登录 trae.cn 并复制新的 Cookie 值,更新到 TRAE_SESSION 后重试。"
"原始返回: " + text[:200])
if status != 200 or not token:
raise RuntimeError("GetUserToken 失败: HTTP %s %s" % (status, text[:200]))
return token
def checkin(token: str, device_id: str) -> dict:
"""执行每日签到(claim)。"""
headers = {
"Authorization": "Cloud-IDE-JWT " + token,
"X-User-Region": "cn",
"x-device-id": device_id,
"Content-Type": "application/json",
"User-Agent": "TraeCheckin/1.0",
}
status, text = _post("/trae/api/v2/ug/checkin_credits/claim", headers, "{}")
try:
return {"http": status, "body": json.loads(text)}
except json.JSONDecodeError:
return {"http": status, "body": {"raw": text}}
def notify_feishu(webhook, text):
"""向飞书机器人推送一条文本消息;webhook 为空则跳过。返回 HTTP 状态码,失败返回 None。"""
if not webhook:
return None
try:
payload = json.dumps({"msg_type": "text", "content": {"text": text}}).encode("utf-8")
req = urllib.request.Request(webhook, data=payload, headers={"Content-Type": "application/json"}, method="POST")
with urllib.request.urlopen(req, timeout=15) as resp:
return resp.status
except Exception:
return None
def beijing_now_str():
"""返回北京时间字符串(GitHub Actions 运行在 UTC,需 +8 小时)。"""
return (datetime.datetime.utcnow() + datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S")
def iter_sessions():
"""按顺序产出 (账号序号, session, device_id)。账号 1 读 TRAE_SESSION;
之后依次读 TRAE_SESSION_2, TRAE_SESSION_3… 直到缺空为止。"""
s = os.environ.get("TRAE_SESSION", "").strip()
if s:
yield 1, s, os.environ.get("TRAE_DEVICE_ID", "").strip()
n = 2
while True:
s = os.environ.get("TRAE_SESSION_%d" % n, "").strip()
if not s:
break
yield n, s, os.environ.get("TRAE_DEVICE_ID_%d" % n, "").strip()
n += 1
def random_device_id():
"""随机生成 16 位数字风控设备号(仅在缺省时兜底)。"""
return str(random.randint(10**15, 10**16 - 1))
def main():
accounts = list(iter_sessions())
if not accounts:
print("错误:缺少环境变量 TRAE_SESSION")
sys.exit(1)
webhook = os.environ.get("FEISHU_WEBHOOK", "").strip()
ok_names, fail_names = [], []
all_ok = True
for index, session, device_id in accounts:
# 多账号之间错开 3~6 秒随机间隔,降低请求密度,规避 9074「参与用户太多」风控
if index > 1:
time.sleep(random.uniform(3, 6))
name = "账号 %d" % index
device_id = device_id or random_device_id()
print("[%s] device_id=%s" % (name, device_id))
try:
token = get_token(session)
print("[%s] 已换取新 JWT,长度=%d" % (name, len(token)))
result = checkin(token, device_id)
body = result["body"]
code = body.get("code", -1)
# 9074「参与用户太多」= 设备号被风控标记;换全新设备号自动重试(最多 5 次)
attempt = 1
while code == 9074 and attempt < 5:
device_id = random_device_id()
attempt += 1
print("[%s] 命中风控 9074,换新设备号重试(第 %d 次)" % (name, attempt))
time.sleep(random.uniform(0.8, 1.5))
result = checkin(token, device_id)
body = result["body"]
code = body.get("code", -1)
checked = body.get("checked_in", False)
ok = (result["http"] == 200) and (code == 0 or checked)
credits = body.get("credits", 0)
if ok:
print("[%s] 签到成功,本次获得:%s 积分" % (name, credits))
ok_names.append(name)
else:
reason = body.get("message") or ("HTTP %s" % result["http"])
print("[%s] 签到失败:%s" % (name, reason))
fail_names.append(name)
all_ok = False
except Exception as e:
print("[%s] 签到异常: %s" % (name, e))
fail_names.append(name)
all_ok = False
# 汇总一条飞书推送(无论成功/失败都汇总,webhook 为空则跳过)
summary = ["Trae 多账号签到结果", "时间:%s" % beijing_now_str()]
if ok_names:
summary.append("成功:" + "、".join(ok_names))
if fail_names:
summary.append("失败:" + "、".join(fail_names))
if webhook and (ok_names or fail_names):
notify_feishu(webhook, "\n".join(summary))
if not all_ok:
sys.exit(1)
print("全部账号签到完成")
if __name__ == "__main__":
main()