import csv, hashlib, json, pathlib, sys
from datetime import datetime
from urllib.parse import urlsplit, parse_qs
manifest, target = map(pathlib.Path, sys.argv[1:3])
entries = json.loads(manifest.read_text(encoding="utf-8"))
if not isinstance(entries, list) or not 1 <= len(entries) <= 100:
raise ValueError("Supply 1–100 saved requests; no network calls are made")
STANDARD = "wechat_mp.v2.fetch_article_stats"
H5 = "wechat_mp.v2.fetch_article_stats_h5"
metrics = {"read_num": "show_read", "like_count": "show_like", "old_like_count": "show_old_like",
"share_count": "show_share", "collect_count": "show_collect", "comment_count": "comment_enabled", "star_num": "show_star"}
rows, sources, request_ids = [], [], set()
def cell(v):
if v is None:
return ""
value = str(v)
return "'" + value if value.startswith(("=", "+", "-", "@", "\t", "\r", "\n")) else value
def classify(value, present):
if not present:
return "missing", None
if value is None:
return "null", None
if isinstance(value, bool):
return "invalid_boolean", None
if type(value) is int:
if value > 9007199254740991:
return "unsafe_numeric", None
return ("reported_integer", str(value)) if value >= 0 else ("invalid_negative", None)
if isinstance(value, str):
return ("reported_integer", value) if value.isascii() and value.isdigit() else ("reported_text", None)
raise ValueError("Unexpected structured or floating metric")
for index, entry in enumerate(entries):
action, params = entry["action"], entry["params"]
if action not in (STANDARD, H5) or type(params.get("raw")) is not bool:
raise ValueError("Use a stats action with explicit raw mode")
allowed = {"url", "raw"} | ({"comment_id"} if action == H5 else set())
if set(params) - allowed:
raise ValueError("Unexpected action parameters")
article_url = params["url"]
if not isinstance(article_url, str) or any(c.isspace() or ord(c) < 32 for c in article_url):
raise ValueError("Invalid public article URL")
u = urlsplit(article_url)
if any(k.lower() in ("key", "uin", "pass_ticket", "ticket", "access_token") for k in parse_qs(u.query, keep_blank_values=True)):
raise ValueError("Use a public share URL without session credentials")
if u.scheme not in ("http", "https") or u.hostname != "mp.weixin.qq.com" or u.username is not None or u.password is not None or u.port not in (None, 80, 443):
raise ValueError("Expected original WeChat article URL")
if u.path == "/s":
query = parse_qs(u.query, keep_blank_values=True)
if any(len(query.get(k, [])) != 1 or not query[k][0] for k in ("__biz", "mid", "idx")):
raise ValueError("Long article URL needs unambiguous __biz/mid/idx")
if not query["mid"][0].isdigit() or not query["idx"][0].isdigit() or int(query["idx"][0]) < 1:
raise ValueError("Invalid article position")
elif not u.path.startswith("/s/") or not u.path[3:] or "/" in u.path[3:]:
raise ValueError("Short article URL needs a nonempty article token")
collected = datetime.fromisoformat(entry["collected_at"].replace("Z", "+00:00"))
if collected.tzinfo is None:
raise ValueError("Collection time requires timezone")
http = entry["http_status"]
if type(http) is not int or not 100 <= http <= 599:
raise ValueError("Record actual integer HTTP status")
source = manifest.parent / entry["response_file"]
raw = source.read_bytes(); result = json.loads(raw)
evidence = {"source": str(source), "sha256": hashlib.sha256(raw).hexdigest(), "params": params,
"action": action, "http_status": http, "collected_at": entry["collected_at"]}
data, failure = {}, None
if http != 200:
error = result.get("error", {})
if not isinstance(error, dict) or not isinstance(error.get("code"), str) or not error["code"].strip():
raise ValueError("Expected gateway error envelope")
request, failure = error.get("request_id"), error["code"]
else:
if result.get("action") != action or result.get("status") != "succeeded" or result.get("error") is not None or not isinstance(result.get("data"), dict):
raise ValueError("Wrong action, failure or missing data object")
request, data = result.get("request_id"), result["data"]
if not isinstance(request, str) or not request.strip() or request in request_ids:
raise ValueError("Missing/repeated successful request ID")
request_ids.add(request)
evidence.update(request_id=request, error_code=failure)
sources.append((evidence, raw))
unavailable = data.get("unavailable", [])
if not isinstance(unavailable, list) or any(not isinstance(x, str) for x in unavailable):
raise ValueError("Invalid unavailable list")
fields = dict(metrics)
if action == H5:
fields["elected_comment_count"] = None
for metric, flag in fields.items():
raw_value = data.get(metric)
state, value = classify(raw_value, metric in data)
if failure:
state, value = "gateway_failed", None
elif metric in unavailable:
state, value = ("unavailable" if raw_value is None else "inconsistent_unavailable"), None
elif action == H5 and metric not in ("comment_count", "elected_comment_count"):
state, value = "unsupported_by_action", None
rows.append({"article_url": article_url, "action": action, "request_id": request,
"collected_at": entry["collected_at"], "metric": metric, "state": state, "value": value,
"raw_value": json.dumps(raw_value, ensure_ascii=False), "visibility_flag": flag,
"visibility_raw": json.dumps(data.get(flag), ensure_ascii=False) if flag else None,
"get_data_succ_raw": json.dumps(data.get("get_data_succ")),
"source_index": index + 1, "error_code": failure})
target.mkdir(exist_ok=False)
with (target / "metrics.csv").open("x", encoding="utf-8-sig", newline="") as f:
writer = csv.DictWriter(f, fieldnames=list(rows[0])); writer.writeheader()
writer.writerows({k: cell(v) for k, v in row.items()} for row in rows)
for index, (_, raw) in enumerate(sources):
(target / ("raw-%03d.json" % (index + 1))).write_bytes(raw)
(target / "evidence.json").write_text(json.dumps({"requests": [x[0] for x in sources],
"exact_audience_claim": False, "automatic_ranking": False,
"note": "reported_integer describes syntax only; inspect source flags and platform thresholds before comparison"}, ensure_ascii=False, indent=2), encoding="utf-8")
print(json.dumps({"requests": len(sources), "metric_rows": len(rows)}))