@acedatacloud/skills 2026.726.8 → 2026.726.9
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.
- package/package.json +1 -1
- package/skills/cto51/SKILL.md +89 -0
- package/skills/cto51/scripts/cto51.py +406 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acedatacloud/skills",
|
|
3
|
-
"version": "2026.726.
|
|
3
|
+
"version": "2026.726.9",
|
|
4
4
|
"description": "Agent Skills for AceDataCloud AI services — music, image, video generation, LLM chat, web search. Compatible with Claude Code, GitHub Copilot, Gemini CLI, OpenAI Codex, and 30+ AI coding agents.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agent-skills",
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: cto51
|
|
3
|
+
description: Read the connected 51CTO 博客 (blog.51cto.com) account and create Markdown article drafts with the user's own login cookies (BYOC). Use when the user mentions 51CTO, wants to save a 51CTO draft, or asks who their connected 51CTO account is.
|
|
4
|
+
when_to_use: |
|
|
5
|
+
Trigger for the user's 51CTO 博客 account driven by their own login cookie:
|
|
6
|
+
show the connected account, or turn Markdown into a 51CTO article draft.
|
|
7
|
+
The write API creates a draft, so this skill stops there and hands the user
|
|
8
|
+
the editor URL. Writes are gated behind explicit confirmation.
|
|
9
|
+
connections: [cto51]
|
|
10
|
+
allowed_tools: [Bash]
|
|
11
|
+
license: Apache-2.0
|
|
12
|
+
metadata:
|
|
13
|
+
author: acedatacloud
|
|
14
|
+
version: "1.0"
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
# cto51 — read & draft on 51CTO 博客 via your own cookies
|
|
18
|
+
|
|
19
|
+
Drives the user's **real** 51CTO account through the same `blog.51cto.com`
|
|
20
|
+
endpoints the site uses, authenticated by the login cookie they captured with
|
|
21
|
+
the ACE extension. No browser, no third-party deps — just `urllib`.
|
|
22
|
+
|
|
23
|
+
The connector injects the cookie jar as a JSON env var `$CTO51_COOKIES`. Never
|
|
24
|
+
print it.
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
python3 "$SKILL_DIR/scripts/cto51.py" whoami
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
If `$SKILL_DIR` points at a different skill loaded in the same turn, resolve
|
|
31
|
+
this skill's directory explicitly before running the commands below.
|
|
32
|
+
|
|
33
|
+
## Important: draft only
|
|
34
|
+
|
|
35
|
+
51CTO's write endpoint creates a **draft**. This skill returns the draft's
|
|
36
|
+
editor URL and does not publish. Tell the user plainly that they must open that
|
|
37
|
+
URL and publish themselves — do not claim the article went live.
|
|
38
|
+
|
|
39
|
+
## Verify the connection first
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
python3 "$SKILL_DIR/scripts/cto51.py" whoami
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
If this fails with a redirect or auth error, the cookie has expired. Ask the
|
|
46
|
+
user to reconnect at `https://auth.acedata.cloud/user/connections` rather than
|
|
47
|
+
retrying.
|
|
48
|
+
|
|
49
|
+
## Create a draft — GATED
|
|
50
|
+
|
|
51
|
+
Prepare the complete Markdown in a file. The first call is always a dry run and
|
|
52
|
+
does not write anything.
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# Dry run — shows exactly what would be written.
|
|
56
|
+
python3 "$SKILL_DIR/scripts/cto51.py" draft \
|
|
57
|
+
--title "标题" --content-file /tmp/article.md --tags "python,api"
|
|
58
|
+
|
|
59
|
+
# Actually create the draft after the user confirms.
|
|
60
|
+
python3 "$SKILL_DIR/scripts/cto51.py" draft \
|
|
61
|
+
--title "标题" --content-file /tmp/article.md --tags "python,api" --confirm
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Options: `--content-file <path.md>` (preferred) or `--content "<markdown>"` for
|
|
65
|
+
short inline text; `--tags "a,b"` comma-separated; `--abstract "…"` sets the
|
|
66
|
+
summary shown in listings. The dry run echoes every field that will be written,
|
|
67
|
+
including the abstract — show that output to the user before confirming.
|
|
68
|
+
|
|
69
|
+
`--confirm` is valid only as the final argument. Show the title, tags and full
|
|
70
|
+
content to the user before writing.
|
|
71
|
+
|
|
72
|
+
## Gotchas
|
|
73
|
+
|
|
74
|
+
- 51CTO sits behind a WAF that answers a bare request with HTTP 567. The CLI
|
|
75
|
+
always sends a full browser fingerprint, so do not strip its headers or
|
|
76
|
+
re-implement the calls with plain `curl`.
|
|
77
|
+
- Both the identity and the `_csrf` token come from the publish page. A
|
|
78
|
+
redirect there means the session is dead — reconnect, do not retry.
|
|
79
|
+
- Content is sent as Markdown (`is_old=0`). Do not pre-render it to HTML.
|
|
80
|
+
- Images referenced by external URL are not re-hosted. If the source host
|
|
81
|
+
blocks hotlinking they will not render; mention this when the article has
|
|
82
|
+
images.
|
|
83
|
+
- Do not retry a timed-out write automatically — the outcome may be unknown and
|
|
84
|
+
a retry can create a duplicate draft.
|
|
85
|
+
|
|
86
|
+
## Record the output
|
|
87
|
+
|
|
88
|
+
This skill only produces drafts, so do **not** call `publish_artifact`. Report
|
|
89
|
+
the returned `draft_id` and `edit_url` to the user instead.
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
cto51 — read & draft on 51CTO 博客 (blog.51cto.com) with the user's own login
|
|
4
|
+
cookies (BYOC). Standard-library only (urllib), no third-party deps, so it runs
|
|
5
|
+
in the bare sandbox without an image change.
|
|
6
|
+
|
|
7
|
+
The connector injects the user's cookie jar as a JSON env var ``CTO51_COOKIES``
|
|
8
|
+
— a list of ``{name, value, domain, ...}`` dicts captured by the ACE extension.
|
|
9
|
+
(The namespace is `cto51`, not `51cto`, because `51CTO_COOKIES` would not be a
|
|
10
|
+
valid shell identifier.)
|
|
11
|
+
|
|
12
|
+
Read commands run directly. ``draft`` is GATED: without a trailing ``--confirm``
|
|
13
|
+
it only dry-runs. ``--confirm`` is honored ONLY as the last argument.
|
|
14
|
+
|
|
15
|
+
NOTE: this creates a DRAFT and returns its editor URL; the user finishes
|
|
16
|
+
publishing in the 51CTO editor.
|
|
17
|
+
|
|
18
|
+
Examples:
|
|
19
|
+
python3 cto51.py whoami
|
|
20
|
+
python3 cto51.py draft --title T --content-file a.md --confirm
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import argparse
|
|
26
|
+
import gzip
|
|
27
|
+
import http.client
|
|
28
|
+
import json
|
|
29
|
+
import os
|
|
30
|
+
import re
|
|
31
|
+
import socket
|
|
32
|
+
import sys
|
|
33
|
+
import urllib.error
|
|
34
|
+
import urllib.parse
|
|
35
|
+
import urllib.request
|
|
36
|
+
|
|
37
|
+
UA = (
|
|
38
|
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
|
|
39
|
+
"(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
|
|
40
|
+
)
|
|
41
|
+
PLATFORM = "cto51"
|
|
42
|
+
BASE = "https://blog.51cto.com"
|
|
43
|
+
PUBLISH_PAGE = f"{BASE}/blogger/publish"
|
|
44
|
+
# Cap on the FORM-ENCODED body (urlencode expands CJK ~3x).
|
|
45
|
+
MAX_ENCODED_BYTES = 8 * 1024 * 1024
|
|
46
|
+
|
|
47
|
+
# Bounded quantifiers so a hostile page cannot cause catastrophic backtracking.
|
|
48
|
+
_CSRF_RE = re.compile(r'<meta\s{1,10}name="csrf-token"\s{1,10}content="([^"]{1,500})"')
|
|
49
|
+
_USER_RE = re.compile(
|
|
50
|
+
r'<li class="more user">\s{0,50}<a[^>]{0,300}href="([^"]{1,300})"[^>]{0,300}>'
|
|
51
|
+
r'\s{0,50}<img[^>]{0,300}src="([^"]{1,300})"'
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
_RAW = sys.argv[1:]
|
|
55
|
+
CONFIRM = bool(_RAW) and _RAW[-1] == "--confirm"
|
|
56
|
+
ARGV = _RAW[:-1] if CONFIRM else list(_RAW)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class _NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
60
|
+
"""Refuse redirects outright.
|
|
61
|
+
|
|
62
|
+
add_unredirected_header only protects the Cookie; urllib copies every other
|
|
63
|
+
header (here the `_csrf` form token's companion headers) onto the redirected
|
|
64
|
+
request, so a 30x to a foreign host could hand them over.
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
68
|
+
return None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
_OPENER = urllib.request.build_opener(_NoRedirect())
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def out(obj) -> None:
|
|
75
|
+
print(json.dumps(obj, ensure_ascii=False, indent=2, default=str))
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def die(msg: str, code: int = 1) -> None:
|
|
79
|
+
out({"error": msg})
|
|
80
|
+
sys.exit(code)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
# 51CTO error/WAF pages carry the site chrome, including the csrf-token meta —
|
|
84
|
+
# never echo a raw response body without stripping it. Covers both the HTML meta
|
|
85
|
+
# form (name="csrf-token" content="…") and any JSON/inline "csrfToken":"…".
|
|
86
|
+
_CSRF_LEAK_RES = (
|
|
87
|
+
# name="csrf-token" … content="TOKEN" (and the reversed attribute order)
|
|
88
|
+
re.compile(r'(name=["\']csrf[-_]?token["\'][^>]{0,400}?content=["\'])([^"\']{0,500})', re.I),
|
|
89
|
+
re.compile(r'(content=["\'])([^"\']{0,500})(["\'][^>]{0,400}?name=["\']csrf[-_]?token)', re.I),
|
|
90
|
+
# "_csrf": "TOKEN" / _csrf=TOKEN / csrfToken: 'TOKEN' — 51CTO runs Yii, whose
|
|
91
|
+
# CSRF parameter is literally `_csrf`, so `token` must be OPTIONAL here.
|
|
92
|
+
re.compile(r'(_?csrf[-_]?(?:token)?["\']?\s{0,5}[:=]\s{0,5}["\']?)([^"\'&,\s>;]{1,500})', re.I),
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _redact(text: str) -> str:
|
|
97
|
+
for rx in _CSRF_LEAK_RES:
|
|
98
|
+
text = rx.sub(
|
|
99
|
+
(lambda mo: mo.group(1) + "<redacted>" + (mo.group(3) if mo.lastindex and mo.lastindex >= 3 else "")),
|
|
100
|
+
text,
|
|
101
|
+
)
|
|
102
|
+
return text
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
# ── Cookie jar (shared pattern across the cookie-BYOC skills) ────────
|
|
107
|
+
|
|
108
|
+
def load_cookies() -> list:
|
|
109
|
+
env = f"{PLATFORM.upper()}_COOKIES"
|
|
110
|
+
raw = os.environ.get(env)
|
|
111
|
+
if not raw:
|
|
112
|
+
die(f"{env} is not set — connect 51CTO at "
|
|
113
|
+
f"https://auth.acedata.cloud/user/connections, then retry.")
|
|
114
|
+
try:
|
|
115
|
+
jar = json.loads(raw)
|
|
116
|
+
except json.JSONDecodeError as e:
|
|
117
|
+
die(f"{env} is not valid JSON: {e}")
|
|
118
|
+
if not isinstance(jar, list):
|
|
119
|
+
die(f"{env} must be a JSON list of cookies, got {type(jar).__name__}")
|
|
120
|
+
return jar
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _domain_matches(host: str, domain: str) -> bool:
|
|
124
|
+
d = domain.lstrip(".").lower()
|
|
125
|
+
h = host.lower()
|
|
126
|
+
return not d or h == d or h.endswith("." + d)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def cookie_header(jar: list, url: str) -> str:
|
|
130
|
+
host = urllib.parse.urlsplit(url).hostname or ""
|
|
131
|
+
host_in_scope = any(
|
|
132
|
+
c.get("domain") and _domain_matches(host, str(c["domain"])) for c in jar
|
|
133
|
+
)
|
|
134
|
+
parts = []
|
|
135
|
+
for c in jar:
|
|
136
|
+
name, value = c.get("name"), c.get("value")
|
|
137
|
+
if not name or value is None:
|
|
138
|
+
continue
|
|
139
|
+
domain = c.get("domain")
|
|
140
|
+
if domain:
|
|
141
|
+
if not _domain_matches(host, str(domain)):
|
|
142
|
+
continue
|
|
143
|
+
elif not host_in_scope:
|
|
144
|
+
continue
|
|
145
|
+
# http.client raises ValueError("Invalid header value %r" % value) at
|
|
146
|
+
# send time for CR/LF, and UnicodeEncodeError for non-latin1 — and the
|
|
147
|
+
# exception text carries the WHOLE Cookie header. Reject here, with a
|
|
148
|
+
# message that never echoes a value.
|
|
149
|
+
pair = f"{name}={value}"
|
|
150
|
+
if any(ch in pair for ch in "\r\n\x00"):
|
|
151
|
+
die("a cookie in the jar contains a line break and cannot be sent — "
|
|
152
|
+
"reconnect at https://auth.acedata.cloud/user/connections.")
|
|
153
|
+
try:
|
|
154
|
+
pair.encode("latin-1")
|
|
155
|
+
except UnicodeEncodeError:
|
|
156
|
+
die("a cookie in the jar contains characters that cannot be sent in "
|
|
157
|
+
"an HTTP header — reconnect at "
|
|
158
|
+
"https://auth.acedata.cloud/user/connections.")
|
|
159
|
+
parts.append(pair)
|
|
160
|
+
return "; ".join(parts)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def request(method: str, url: str, jar: list, *, headers=None, form=None,
|
|
164
|
+
write: bool = False):
|
|
165
|
+
# 51CTO's WAF 567s a bare request; it needs a full browser fingerprint
|
|
166
|
+
# (same treatment the csdn skill needs).
|
|
167
|
+
hdrs = {
|
|
168
|
+
"User-Agent": UA,
|
|
169
|
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
|
170
|
+
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
|
|
171
|
+
"sec-ch-ua": '"Chromium";v="131", "Not_A Brand";v="24"',
|
|
172
|
+
"sec-ch-ua-mobile": "?0",
|
|
173
|
+
"sec-ch-ua-platform": '"macOS"',
|
|
174
|
+
"Sec-Fetch-Dest": "document",
|
|
175
|
+
"Sec-Fetch-Mode": "navigate",
|
|
176
|
+
"Sec-Fetch-Site": "same-origin",
|
|
177
|
+
"Origin": BASE,
|
|
178
|
+
"Referer": PUBLISH_PAGE,
|
|
179
|
+
}
|
|
180
|
+
if headers:
|
|
181
|
+
hdrs.update(headers)
|
|
182
|
+
data = None
|
|
183
|
+
if form is not None:
|
|
184
|
+
data = urllib.parse.urlencode(form).encode("utf-8")
|
|
185
|
+
hdrs.setdefault("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
|
186
|
+
req = urllib.request.Request(url, data=data, headers=hdrs, method=method)
|
|
187
|
+
# Unredirected → the cookie is not re-sent if the API 30x-redirects to a
|
|
188
|
+
# different host (e.g. a login page), so the jar never leaks off-site.
|
|
189
|
+
req.add_unredirected_header("Cookie", cookie_header(jar, url))
|
|
190
|
+
try:
|
|
191
|
+
with _OPENER.open(req, timeout=30) as resp:
|
|
192
|
+
raw = resp.read()
|
|
193
|
+
if resp.headers.get("Content-Encoding") == "gzip":
|
|
194
|
+
raw = gzip.decompress(raw)
|
|
195
|
+
return resp.status, raw.decode("utf-8", "replace")
|
|
196
|
+
except urllib.error.HTTPError as e:
|
|
197
|
+
if e.code in (301, 302, 303, 307, 308):
|
|
198
|
+
die("51CTO redirected the request — not followed, so no credential "
|
|
199
|
+
"left 51cto.com. You are most likely logged out; reconnect at "
|
|
200
|
+
"https://auth.acedata.cloud/user/connections."
|
|
201
|
+
+ (" This was a WRITE: its outcome is UNKNOWN — check your "
|
|
202
|
+
"drafts before retrying." if write else ""))
|
|
203
|
+
# Draining the error body can itself raise (IncompleteRead / reset).
|
|
204
|
+
# Degrade to an empty body so a truncated 5xx still flows into the
|
|
205
|
+
# normal non-JSON path, which carries the write-UNKNOWN wording.
|
|
206
|
+
try:
|
|
207
|
+
raw = e.read()
|
|
208
|
+
if e.headers.get("Content-Encoding") == "gzip":
|
|
209
|
+
raw = gzip.decompress(raw)
|
|
210
|
+
except Exception:
|
|
211
|
+
raw = b""
|
|
212
|
+
return e.code, raw.decode("utf-8", "replace")
|
|
213
|
+
# URLError subclasses OSError, so it must come first. The receive phase
|
|
214
|
+
# (getresponse) raises TimeoutError/HTTPException OUTSIDE urllib's own
|
|
215
|
+
# OSError wrapper, so a write whose reply never lands would otherwise
|
|
216
|
+
# escape as a bare traceback with no JSON at all.
|
|
217
|
+
except (urllib.error.URLError, TimeoutError, socket.timeout,
|
|
218
|
+
http.client.HTTPException, gzip.BadGzipFile, OSError) as e:
|
|
219
|
+
if write:
|
|
220
|
+
die(f"51CTO write did not return a result ({type(e).__name__}: {e}); "
|
|
221
|
+
f"the outcome is UNKNOWN. Check your 51CTO drafts before "
|
|
222
|
+
f"retrying so you do not create a duplicate.")
|
|
223
|
+
die(f"network error reaching {url}: {type(e).__name__}: {e}")
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def publish_page(jar: list) -> tuple[str, dict]:
|
|
227
|
+
"""Fetch the publish page — it carries both the identity and the CSRF token."""
|
|
228
|
+
status, html = request("GET", PUBLISH_PAGE, jar)
|
|
229
|
+
if status in (401, 403):
|
|
230
|
+
die("auth failed — cookie expired or invalid. Reconnect at "
|
|
231
|
+
"https://auth.acedata.cloud/user/connections.")
|
|
232
|
+
if status != 200:
|
|
233
|
+
die(f"unexpected status {status} loading the 51CTO publish page")
|
|
234
|
+
m = _USER_RE.search(html)
|
|
235
|
+
if not m:
|
|
236
|
+
# The csrf-token meta also appears on logged-out pages, so it is not a
|
|
237
|
+
# session signal. Distinguish "definitely logged out" from "markup
|
|
238
|
+
# changed" instead of always blaming the cookie.
|
|
239
|
+
if "/user/login" in html or "home.51cto.com/login" in html:
|
|
240
|
+
die("not logged in to 51CTO — reconnect at "
|
|
241
|
+
"https://auth.acedata.cloud/user/connections.")
|
|
242
|
+
die("could not confirm the 51CTO session from the publish page — either "
|
|
243
|
+
"the cookie expired or 51CTO changed its markup. Try reconnecting "
|
|
244
|
+
"at https://auth.acedata.cloud/user/connections; if that does not "
|
|
245
|
+
"help, this skill needs updating.")
|
|
246
|
+
link, avatar = m.group(1), m.group(2)
|
|
247
|
+
csrf_m = _CSRF_RE.search(html)
|
|
248
|
+
if not csrf_m:
|
|
249
|
+
die("could not read the 51CTO CSRF token from the publish page; "
|
|
250
|
+
"reconnect and retry.")
|
|
251
|
+
uid = link.rstrip("/").split("/")[-1]
|
|
252
|
+
return csrf_m.group(1), {"user_id": uid, "url": link, "avatar": avatar}
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
# ── commands ────────────────────────────────────────────────────────
|
|
256
|
+
|
|
257
|
+
def cmd_whoami(jar, _args):
|
|
258
|
+
_csrf, who = publish_page(jar)
|
|
259
|
+
out({"platform": PLATFORM, **who})
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def read_content(args) -> str:
|
|
263
|
+
content = args.content
|
|
264
|
+
if args.content_file:
|
|
265
|
+
try:
|
|
266
|
+
with open(args.content_file, encoding="utf-8") as f:
|
|
267
|
+
content = f.read()
|
|
268
|
+
except OSError as e:
|
|
269
|
+
die(f"cannot read --content-file: {e}")
|
|
270
|
+
if content is None:
|
|
271
|
+
die("provide --content-file <path.md> or --content <markdown>")
|
|
272
|
+
# The body is form-urlencoded, which expands each CJK byte to %XX (~3x), so
|
|
273
|
+
# cap the ENCODED size — a raw-byte cap would let a 10 MiB Chinese article
|
|
274
|
+
# become a ~30 MB POST that 51CTO rejects opaquely mid-write.
|
|
275
|
+
encoded = len(urllib.parse.quote_plus(content))
|
|
276
|
+
if encoded > MAX_ENCODED_BYTES:
|
|
277
|
+
die(f"content is too large: {encoded} bytes once form-encoded "
|
|
278
|
+
f"(limit {MAX_ENCODED_BYTES}). Split the article or trim it.")
|
|
279
|
+
return content
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def cmd_draft(jar, args):
|
|
283
|
+
if not args.title:
|
|
284
|
+
die("--title is required")
|
|
285
|
+
content = read_content(args)
|
|
286
|
+
tags = ",".join(t.strip() for t in (args.tags or "").split(",") if t.strip())
|
|
287
|
+
|
|
288
|
+
if not CONFIRM:
|
|
289
|
+
out({
|
|
290
|
+
"dry_run": True, "command": "draft", "platform": PLATFORM,
|
|
291
|
+
"title": args.title, "tags": tags,
|
|
292
|
+
"abstract": args.abstract or "",
|
|
293
|
+
"content_characters": len(content),
|
|
294
|
+
"note": "51CTO content is Markdown (is_old=0). Re-run with --confirm "
|
|
295
|
+
"as the LAST argument to actually create the draft. This "
|
|
296
|
+
"creates a DRAFT — finish publishing in the 51CTO editor.",
|
|
297
|
+
})
|
|
298
|
+
return
|
|
299
|
+
|
|
300
|
+
csrf, _who = publish_page(jar)
|
|
301
|
+
# We hold the exact token, so scrub it by value too — airtight regardless of
|
|
302
|
+
# how 51CTO frames it in an error page (the patterns are defence in depth).
|
|
303
|
+
def scrub(text: str) -> str:
|
|
304
|
+
return _redact(text.replace(csrf, "<redacted>") if csrf else text)
|
|
305
|
+
form = {
|
|
306
|
+
"title": args.title,
|
|
307
|
+
"content": content,
|
|
308
|
+
"cate_id": "",
|
|
309
|
+
"custom_id": "0",
|
|
310
|
+
"tag": tags,
|
|
311
|
+
"abstract": args.abstract or "",
|
|
312
|
+
"banner_type": "0",
|
|
313
|
+
"blog_type": "1",
|
|
314
|
+
"copy_code": "1",
|
|
315
|
+
"is_hide": "0",
|
|
316
|
+
"top_time": "0",
|
|
317
|
+
"is_comment": "0",
|
|
318
|
+
"is_old": "0", # 0 = Markdown
|
|
319
|
+
"blog_id": "",
|
|
320
|
+
"pid": "",
|
|
321
|
+
"did": "",
|
|
322
|
+
"work_id": "",
|
|
323
|
+
"class_id": "",
|
|
324
|
+
"subjectId": "",
|
|
325
|
+
"import_type": "-1",
|
|
326
|
+
"invite_code": "",
|
|
327
|
+
"raffle": "",
|
|
328
|
+
"orig": "",
|
|
329
|
+
"_csrf": csrf,
|
|
330
|
+
}
|
|
331
|
+
status, text = request("POST", f"{BASE}/blogger/draft", jar, write=True, form=form,
|
|
332
|
+
headers={
|
|
333
|
+
"X-Requested-With": "XMLHttpRequest",
|
|
334
|
+
"Accept": "application/json, text/javascript, */*; q=0.01",
|
|
335
|
+
"Sec-Fetch-Dest": "empty",
|
|
336
|
+
"Sec-Fetch-Mode": "cors",
|
|
337
|
+
})
|
|
338
|
+
try:
|
|
339
|
+
res = json.loads(text)
|
|
340
|
+
except json.JSONDecodeError:
|
|
341
|
+
die(f"51CTO write returned a non-JSON response ({status}); the outcome "
|
|
342
|
+
f"is UNKNOWN. Check your drafts before retrying. "
|
|
343
|
+
f"Body: {scrub(text)[:200]}")
|
|
344
|
+
# The endpoint really does answer `"data": []` (a list) for the logged-out
|
|
345
|
+
# case, so never dereference it without checking the shape first.
|
|
346
|
+
if not isinstance(res, dict):
|
|
347
|
+
die(f"51CTO write returned an unexpected response shape ({status}); the "
|
|
348
|
+
f"outcome is UNKNOWN. Check your drafts before retrying.")
|
|
349
|
+
data = res.get("data")
|
|
350
|
+
if res.get("status") != 1 or not isinstance(data, dict):
|
|
351
|
+
# Normalize to JSON, never repr(): a dict/list `msg` rendered with
|
|
352
|
+
# repr() uses single quotes, which _redact's double-quote-anchored
|
|
353
|
+
# patterns cannot match.
|
|
354
|
+
raw_msg = res.get("msg")
|
|
355
|
+
detail = (raw_msg if isinstance(raw_msg, str)
|
|
356
|
+
else json.dumps(raw_msg if raw_msg is not None else res,
|
|
357
|
+
ensure_ascii=False))
|
|
358
|
+
die(f"draft creation failed: {scrub(detail)[:200]}")
|
|
359
|
+
# Only an ASCII-numeric id is a real draft handle. str.isdigit() alone is
|
|
360
|
+
# Unicode-wide ('١٢٣', '²' all pass) and would build a plausible-but-wrong
|
|
361
|
+
# edit_url while reporting ok=true.
|
|
362
|
+
did = str(data.get("did") or "").strip()
|
|
363
|
+
if not re.fullmatch(r"[0-9]{1,20}", did):
|
|
364
|
+
# Redact BEFORE slicing so a token can never be half-exposed by the cut.
|
|
365
|
+
die(f"draft creation returned an unusable id {scrub(did)[:80]!r}; the "
|
|
366
|
+
f"outcome is UNKNOWN — check your 51CTO drafts before retrying. "
|
|
367
|
+
f"{scrub(json.dumps(res, ensure_ascii=False))[:200]}")
|
|
368
|
+
out({
|
|
369
|
+
"ok": True,
|
|
370
|
+
"draft_only": True,
|
|
371
|
+
"draft_id": str(did),
|
|
372
|
+
"edit_url": f"{BASE}/blogger/draft/{did}",
|
|
373
|
+
"note": "Draft saved. Open edit_url to review and publish.",
|
|
374
|
+
})
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
COMMANDS = {
|
|
378
|
+
"whoami": cmd_whoami,
|
|
379
|
+
"draft": cmd_draft,
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
def main() -> None:
|
|
384
|
+
p = argparse.ArgumentParser(prog="cto51.py", description="51CTO 博客 cookie CLI")
|
|
385
|
+
sub = p.add_subparsers(dest="command", required=True)
|
|
386
|
+
sub.add_parser("whoami", help="show the logged-in account")
|
|
387
|
+
sp = sub.add_parser("draft", help="create a draft article (GATED by trailing --confirm)")
|
|
388
|
+
sp.add_argument("--title")
|
|
389
|
+
sp.add_argument("--content", help="Markdown content inline")
|
|
390
|
+
sp.add_argument("--content-file", help="path to a Markdown file")
|
|
391
|
+
sp.add_argument("--tags", help="comma-separated tag names")
|
|
392
|
+
sp.add_argument("--abstract", help="short summary shown in listings")
|
|
393
|
+
args = p.parse_args(ARGV)
|
|
394
|
+
jar = load_cookies()
|
|
395
|
+
COMMANDS[args.command](jar, args)
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
if __name__ == "__main__":
|
|
399
|
+
# The agent consuming this CLI parses stdout as JSON — never let an
|
|
400
|
+
# unexpected exception escape as a bare traceback with empty stdout.
|
|
401
|
+
try:
|
|
402
|
+
main()
|
|
403
|
+
except SystemExit:
|
|
404
|
+
raise
|
|
405
|
+
except BaseException as exc: # noqa: BLE001
|
|
406
|
+
die(f"unexpected {type(exc).__name__}: {_redact(str(exc))[:200]}")
|