@acedatacloud/skills 2026.628.2 → 2026.628.4
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/zhihu/SKILL.md +9 -4
- package/skills/zhihu/scripts/blog.py +233 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acedatacloud/skills",
|
|
3
|
-
"version": "2026.628.
|
|
3
|
+
"version": "2026.628.4",
|
|
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",
|
package/skills/zhihu/SKILL.md
CHANGED
|
@@ -12,7 +12,7 @@ allowed_tools: [Bash]
|
|
|
12
12
|
license: Apache-2.0
|
|
13
13
|
metadata:
|
|
14
14
|
author: acedatacloud
|
|
15
|
-
version: "1.
|
|
15
|
+
version: "1.1"
|
|
16
16
|
---
|
|
17
17
|
|
|
18
18
|
# zhihu — read & publish on Zhihu via your own cookies
|
|
@@ -71,7 +71,7 @@ never silently go live. Always show the dry-run to the user, get an explicit
|
|
|
71
71
|
"yes", then re-run with `--confirm` last.
|
|
72
72
|
|
|
73
73
|
```sh
|
|
74
|
-
# Content is HTML. For Markdown, convert to HTML first.
|
|
74
|
+
# Content is HTML. For Markdown, convert to HTML first (e.g. `pandoc -f gfm -t html`).
|
|
75
75
|
python3 $BLOG publish --title "标题" --content-file article.html # dry-run
|
|
76
76
|
python3 $BLOG publish --title "标题" --content-file article.html --draft-only --confirm # save a private draft
|
|
77
77
|
python3 $BLOG publish --title "标题" --content-file article.html --confirm # PUBLIC, goes live
|
|
@@ -80,8 +80,13 @@ python3 $BLOG publish --title "标题" --content-file article.html --confirm
|
|
|
80
80
|
- `--draft-only` stops after saving a private draft (safe — nothing public).
|
|
81
81
|
- Without `--draft-only`, the article is **published publicly** under the user's
|
|
82
82
|
name. Default to `--draft-only` unless the user clearly asked to go live.
|
|
83
|
-
- Images
|
|
84
|
-
|
|
83
|
+
- **Images are auto-hosted.** Zhihu strips any `<img>` whose `src` is not on its
|
|
84
|
+
own CDN, so on `--confirm` the CLI re-uploads every external image (HTML
|
|
85
|
+
`<img src>` **and** Markdown ``, plus `data:` URIs) to Zhihu's image
|
|
86
|
+
service and rewrites the URLs first — images already on `*.zhimg.com` are left
|
|
87
|
+
untouched. The result reports `images: {found, rehosted, failed}`; the dry-run
|
|
88
|
+
reports `images_found`. Pass `--no-images` to skip this. So you can hand the
|
|
89
|
+
CLI HTML/Markdown with normal public image URLs and the pictures survive.
|
|
85
90
|
|
|
86
91
|
## Gotchas — surface before the user is surprised
|
|
87
92
|
|
|
@@ -24,13 +24,19 @@ Quick examples:
|
|
|
24
24
|
from __future__ import annotations
|
|
25
25
|
|
|
26
26
|
import argparse
|
|
27
|
+
import base64
|
|
27
28
|
import gzip
|
|
29
|
+
import hashlib
|
|
30
|
+
import hmac
|
|
28
31
|
import json
|
|
29
32
|
import os
|
|
33
|
+
import re
|
|
30
34
|
import sys
|
|
35
|
+
import time
|
|
31
36
|
import urllib.error
|
|
32
37
|
import urllib.parse
|
|
33
38
|
import urllib.request
|
|
39
|
+
from email.utils import formatdate
|
|
34
40
|
|
|
35
41
|
UA = (
|
|
36
42
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
|
|
@@ -239,6 +245,221 @@ def cmd_article(jar, args):
|
|
|
239
245
|
out(res)
|
|
240
246
|
|
|
241
247
|
|
|
248
|
+
# ── Image re-hosting ────────────────────────────────────────
|
|
249
|
+
#
|
|
250
|
+
# Zhihu silently drops any <img> whose src is not on its own CDN, so external
|
|
251
|
+
# images must be re-hosted first. Primary path: hand Zhihu the remote URL and
|
|
252
|
+
# let it fetch (uploaded_images). Fallback: download the bytes and PUT them to
|
|
253
|
+
# Zhihu's Aliyun-OSS bucket with an HMAC-SHA1-signed request. Ported from
|
|
254
|
+
# PlatformPublisher app/publisher/zhihu.py (proven in production).
|
|
255
|
+
|
|
256
|
+
ZH_UPLOADED_IMAGES = "https://zhuanlan.zhihu.com/api/uploaded_images"
|
|
257
|
+
ZH_IMAGES = "https://api.zhihu.com/images"
|
|
258
|
+
OSS_ENDPOINT = "https://zhihu-pics-upload.zhimg.com"
|
|
259
|
+
OSS_BUCKET = "zhihu-pics"
|
|
260
|
+
# A src already on Zhihu's own CDN needs no re-upload. uploaded_images hands back
|
|
261
|
+
# a pic-private.zhihu.com URL for draft-scoped images, so skip those too.
|
|
262
|
+
_ZHIMG_HOSTS = ("zhimg.com", "pic-private.zhihu.com")
|
|
263
|
+
_IMG_SRC_RE = re.compile(r'(<img[^>]*?\ssrc=["\'])([^"\']+)(["\'])', re.IGNORECASE)
|
|
264
|
+
_MD_IMG_RE = re.compile(r'(!\[[^\]]*\]\()(\s*<?)([^)\s>]+)(>?\s*\))')
|
|
265
|
+
# uploaded_images returns a DRAFT-SCOPED signed pic-private URL whose auth_key
|
|
266
|
+
# expires; reduce any Zhihu image URL to the durable public form Zhihu serves
|
|
267
|
+
# after publish (picx.zhimg.com/v2-<hash>.<ext>).
|
|
268
|
+
_ZH_IMG_HASH = re.compile(r'(v2-[0-9a-f]+)(?:~[^."?\']*)?\.(png|jpe?g|gif|webp)', re.IGNORECASE)
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def _on_zhihu_cdn(src: str) -> bool:
|
|
272
|
+
# Match on the parsed host (not a substring) so an external host that merely
|
|
273
|
+
# contains "zhimg.com" (e.g. my-zhimg.com) isn't wrongly treated as hosted.
|
|
274
|
+
host = (urllib.parse.urlsplit(src).hostname or "") if src else ""
|
|
275
|
+
return any(host == h or host.endswith("." + h) for h in _ZHIMG_HOSTS)
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def _canonical_zhimg(url):
|
|
279
|
+
if not url:
|
|
280
|
+
return url
|
|
281
|
+
m = _ZH_IMG_HASH.search(url)
|
|
282
|
+
return f"https://picx.zhimg.com/{m.group(1)}.{m.group(2).lower()}" if m else url
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def _raw(method, url, jar, *, headers=None, data=None, timeout=60):
|
|
286
|
+
"""Low-level request returning (status, raw_bytes); handles gzip + binary."""
|
|
287
|
+
hdrs = {"User-Agent": UA, "Cookie": cookie_header(jar, url)}
|
|
288
|
+
if headers:
|
|
289
|
+
hdrs.update(headers)
|
|
290
|
+
req = urllib.request.Request(url, data=data, headers=hdrs, method=method)
|
|
291
|
+
try:
|
|
292
|
+
with urllib.request.urlopen(req, timeout=timeout) as resp:
|
|
293
|
+
raw = resp.read()
|
|
294
|
+
if resp.headers.get("Content-Encoding") == "gzip":
|
|
295
|
+
raw = gzip.decompress(raw)
|
|
296
|
+
return resp.status, raw
|
|
297
|
+
except urllib.error.HTTPError as e:
|
|
298
|
+
raw = e.read()
|
|
299
|
+
try:
|
|
300
|
+
if e.headers.get("Content-Encoding") == "gzip":
|
|
301
|
+
raw = gzip.decompress(raw)
|
|
302
|
+
except Exception:
|
|
303
|
+
pass
|
|
304
|
+
return e.code, raw
|
|
305
|
+
except urllib.error.URLError:
|
|
306
|
+
return 0, b""
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def _img_content_type(data: bytes) -> str:
|
|
310
|
+
if data[:8].startswith(b"\x89PNG"):
|
|
311
|
+
return "image/png"
|
|
312
|
+
if data[:3] == b"\xff\xd8\xff":
|
|
313
|
+
return "image/jpeg"
|
|
314
|
+
if data[:6] in (b"GIF87a", b"GIF89a"):
|
|
315
|
+
return "image/gif"
|
|
316
|
+
if data[:4] == b"RIFF" and data[8:12] == b"WEBP":
|
|
317
|
+
return "image/webp"
|
|
318
|
+
return "application/octet-stream"
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def _upload_image_url(jar, src: str):
|
|
322
|
+
"""Primary path: Zhihu fetches the remote URL server-side and hosts it."""
|
|
323
|
+
body = urllib.parse.urlencode({"url": src, "source": "article"}).encode("utf-8")
|
|
324
|
+
status, raw = _raw(
|
|
325
|
+
"POST", ZH_UPLOADED_IMAGES, jar,
|
|
326
|
+
headers={"x-requested-with": "fetch", "Content-Type": "application/x-www-form-urlencoded"},
|
|
327
|
+
data=body,
|
|
328
|
+
)
|
|
329
|
+
if status and status < 400:
|
|
330
|
+
try:
|
|
331
|
+
d = json.loads(raw.decode("utf-8", "replace"))
|
|
332
|
+
except json.JSONDecodeError:
|
|
333
|
+
return None
|
|
334
|
+
if isinstance(d, dict) and d.get("src"):
|
|
335
|
+
return _canonical_zhimg(d["src"])
|
|
336
|
+
return None
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def _oss_put(object_key: str, data: bytes, token: dict) -> bool:
|
|
340
|
+
access_id = token.get("access_id", "")
|
|
341
|
+
access_key = token.get("access_key", "")
|
|
342
|
+
access_token = token.get("access_token", "")
|
|
343
|
+
if not (access_id and access_key and object_key):
|
|
344
|
+
return False
|
|
345
|
+
ctype = _img_content_type(data)
|
|
346
|
+
oss_date = formatdate(usegmt=True)
|
|
347
|
+
oss_ua = "aliyun-sdk-js/6.8.0"
|
|
348
|
+
oss_headers = {
|
|
349
|
+
"x-oss-date": oss_date,
|
|
350
|
+
"x-oss-security-token": access_token,
|
|
351
|
+
"x-oss-user-agent": oss_ua,
|
|
352
|
+
}
|
|
353
|
+
canon = "\n".join(f"{k}:{oss_headers[k]}" for k in sorted(oss_headers))
|
|
354
|
+
string_to_sign = f"PUT\n\n{ctype}\n{oss_date}\n{canon}\n/{OSS_BUCKET}/{object_key}"
|
|
355
|
+
sig = base64.b64encode(
|
|
356
|
+
hmac.new(access_key.encode("utf-8"), string_to_sign.encode("utf-8"), hashlib.sha1).digest()
|
|
357
|
+
).decode("utf-8")
|
|
358
|
+
status, _ = _raw(
|
|
359
|
+
"PUT", f"{OSS_ENDPOINT}/{object_key}", [],
|
|
360
|
+
headers={
|
|
361
|
+
"Content-Type": ctype,
|
|
362
|
+
"Authorization": f"OSS {access_id}:{sig}",
|
|
363
|
+
"x-oss-date": oss_date,
|
|
364
|
+
"x-oss-security-token": access_token,
|
|
365
|
+
"x-oss-user-agent": oss_ua,
|
|
366
|
+
},
|
|
367
|
+
data=data,
|
|
368
|
+
)
|
|
369
|
+
return bool(status) and status < 400
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def _wait_image_ready(jar, image_id: str) -> str:
|
|
373
|
+
for _ in range(10):
|
|
374
|
+
status, raw = _raw("GET", f"https://api.zhihu.com/images/{image_id}", jar, headers=ZH_FETCH)
|
|
375
|
+
try:
|
|
376
|
+
d = json.loads(raw.decode("utf-8", "replace"))
|
|
377
|
+
except json.JSONDecodeError:
|
|
378
|
+
d = {}
|
|
379
|
+
if d.get("status") == "completed" or d.get("original_hash"):
|
|
380
|
+
return d.get("original_hash", "")
|
|
381
|
+
time.sleep(1.0)
|
|
382
|
+
return ""
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def _upload_image_binary(jar, data: bytes):
|
|
386
|
+
"""Fallback: MD5 -> request OSS token -> (dedup or) signed PUT to OSS."""
|
|
387
|
+
image_hash = hashlib.md5(data).hexdigest()
|
|
388
|
+
status, raw = _raw(
|
|
389
|
+
"POST", ZH_IMAGES, jar,
|
|
390
|
+
headers={"x-requested-with": "fetch", "Content-Type": "application/json"},
|
|
391
|
+
data=json.dumps({"image_hash": image_hash, "source": "article"}).encode("utf-8"),
|
|
392
|
+
)
|
|
393
|
+
if not status or status >= 400:
|
|
394
|
+
return None
|
|
395
|
+
try:
|
|
396
|
+
token_data = json.loads(raw.decode("utf-8", "replace"))
|
|
397
|
+
except json.JSONDecodeError:
|
|
398
|
+
return None
|
|
399
|
+
upload_file = token_data.get("upload_file") or {}
|
|
400
|
+
upload_token = token_data.get("upload_token") or {}
|
|
401
|
+
if upload_file.get("state") == 1: # already on Zhihu — just resolve its hash
|
|
402
|
+
object_key = _wait_image_ready(jar, upload_file.get("image_id", ""))
|
|
403
|
+
return _canonical_zhimg(f"https://pic4.zhimg.com/{object_key}") if object_key else None
|
|
404
|
+
object_key = upload_file.get("object_key", "")
|
|
405
|
+
if not object_key or not _oss_put(object_key, data, upload_token):
|
|
406
|
+
return None
|
|
407
|
+
if data[:6] in (b"GIF87a", b"GIF89a"):
|
|
408
|
+
object_key += ".gif"
|
|
409
|
+
return _canonical_zhimg(f"https://pic4.zhimg.com/{object_key}")
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
def _download_image(src: str):
|
|
413
|
+
# Route through _raw so a gzip-encoded response is decompressed (otherwise we
|
|
414
|
+
# would upload gzipped bytes and Zhihu would reject/garble the image). jar=[]
|
|
415
|
+
# so no cookie is ever sent to a third-party image host.
|
|
416
|
+
status, raw = _raw("GET", src, [], headers={"User-Agent": UA}, timeout=30)
|
|
417
|
+
return raw if status and status < 400 and raw else None
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
def count_images(content: str) -> int:
|
|
421
|
+
srcs = {m.group(2) for m in _IMG_SRC_RE.finditer(content or "")}
|
|
422
|
+
srcs |= {m.group(3) for m in _MD_IMG_RE.finditer(content or "")}
|
|
423
|
+
return sum(1 for s in srcs if not _on_zhihu_cdn(s))
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
def rehost_images(jar, content: str):
|
|
427
|
+
"""Replace every non-Zhihu image URL (HTML <img> + markdown ![]()) with a
|
|
428
|
+
Zhihu-CDN URL. Returns (new_content, stats)."""
|
|
429
|
+
cache: dict = {}
|
|
430
|
+
stats = {"found": 0, "rehosted": 0, "failed": 0}
|
|
431
|
+
|
|
432
|
+
def resolve(src: str) -> str:
|
|
433
|
+
if not src or _on_zhihu_cdn(src):
|
|
434
|
+
return src
|
|
435
|
+
if src in cache:
|
|
436
|
+
return cache[src]
|
|
437
|
+
stats["found"] += 1
|
|
438
|
+
new = None
|
|
439
|
+
if src.startswith("data:"):
|
|
440
|
+
try:
|
|
441
|
+
new = _upload_image_binary(jar, base64.b64decode(src.split(",", 1)[1]))
|
|
442
|
+
except Exception:
|
|
443
|
+
new = None
|
|
444
|
+
else:
|
|
445
|
+
new = _upload_image_url(jar, src)
|
|
446
|
+
if not new:
|
|
447
|
+
data = _download_image(src)
|
|
448
|
+
if data:
|
|
449
|
+
new = _upload_image_binary(jar, data)
|
|
450
|
+
if new:
|
|
451
|
+
stats["rehosted"] += 1
|
|
452
|
+
cache[src] = new
|
|
453
|
+
return new
|
|
454
|
+
stats["failed"] += 1
|
|
455
|
+
cache[src] = src # leave as-is; don't corrupt the doc if upload fails
|
|
456
|
+
return src
|
|
457
|
+
|
|
458
|
+
content = _IMG_SRC_RE.sub(lambda m: m.group(1) + resolve(m.group(2)) + m.group(3), content or "")
|
|
459
|
+
content = _MD_IMG_RE.sub(lambda m: m.group(1) + m.group(2) + resolve(m.group(3)) + m.group(4), content)
|
|
460
|
+
return content, stats
|
|
461
|
+
|
|
462
|
+
|
|
242
463
|
def cmd_publish(jar, args):
|
|
243
464
|
if not args.title:
|
|
244
465
|
die("--title is required")
|
|
@@ -260,6 +481,7 @@ def cmd_publish(jar, args):
|
|
|
260
481
|
"title": args.title,
|
|
261
482
|
"draft_only": args.draft_only,
|
|
262
483
|
"content_bytes": len(content or ""),
|
|
484
|
+
"images_found": count_images(content or ""),
|
|
263
485
|
"note": "re-run with --confirm as the LAST argument to actually write. "
|
|
264
486
|
"Without --draft-only this publishes a PUBLIC article on the user's real account.",
|
|
265
487
|
})
|
|
@@ -278,7 +500,12 @@ def cmd_publish(jar, args):
|
|
|
278
500
|
if not draft_id:
|
|
279
501
|
die(f"create-draft failed ({status}): {str(created)[:300]}")
|
|
280
502
|
|
|
281
|
-
# 2.
|
|
503
|
+
# 2. re-host external images to Zhihu's CDN (Zhihu strips foreign <img>)
|
|
504
|
+
image_stats = {"found": 0, "rehosted": 0, "failed": 0}
|
|
505
|
+
if not args.no_images:
|
|
506
|
+
content, image_stats = rehost_images(jar, content)
|
|
507
|
+
|
|
508
|
+
# 3. set draft content
|
|
282
509
|
status, text = request(
|
|
283
510
|
"PATCH", f"https://zhuanlan.zhihu.com/api/articles/{draft_id}/draft", jar,
|
|
284
511
|
headers=ZH_FETCH, body={"title": args.title, "content": content},
|
|
@@ -291,11 +518,12 @@ def cmd_publish(jar, args):
|
|
|
291
518
|
"ok": True,
|
|
292
519
|
"draft_only": True,
|
|
293
520
|
"draft_id": str(draft_id),
|
|
521
|
+
"images": image_stats,
|
|
294
522
|
"edit_url": f"https://zhuanlan.zhihu.com/write?draftId={draft_id}",
|
|
295
523
|
})
|
|
296
524
|
return
|
|
297
525
|
|
|
298
|
-
#
|
|
526
|
+
# 4. publish (go live)
|
|
299
527
|
status, text = request(
|
|
300
528
|
"PUT", f"https://zhuanlan.zhihu.com/api/articles/{draft_id}/publish", jar,
|
|
301
529
|
headers=ZH_FETCH, body={},
|
|
@@ -306,6 +534,7 @@ def cmd_publish(jar, args):
|
|
|
306
534
|
"ok": True,
|
|
307
535
|
"published": True,
|
|
308
536
|
"article_id": str(draft_id),
|
|
537
|
+
"images": image_stats,
|
|
309
538
|
"url": f"https://zhuanlan.zhihu.com/p/{draft_id}",
|
|
310
539
|
})
|
|
311
540
|
|
|
@@ -339,6 +568,8 @@ def main() -> None:
|
|
|
339
568
|
sp.add_argument("--content-file", help="path to an HTML file")
|
|
340
569
|
sp.add_argument("--draft-only", action="store_true",
|
|
341
570
|
help="create a draft only; do NOT go public")
|
|
571
|
+
sp.add_argument("--no-images", action="store_true",
|
|
572
|
+
help="skip re-hosting external images to Zhihu's CDN")
|
|
342
573
|
|
|
343
574
|
args = p.parse_args(ARGV)
|
|
344
575
|
jar = load_cookies(args.platform)
|