import json
import re
import os
from pathlib import Path

jsons_dir = Path(__file__).parent / "jsons"
output_file = Path(__file__).parent / "mismatched_codes.json"

mismatches = []

for json_file in sorted(jsons_dir.glob("*.json")):
    with open(json_file, encoding="utf-8") as f:
        data = json.load(f)

    for item in data.get("items", []):
        url = item.get("url", "")
        xkw_code = item.get("xkw_code", "")

        if "www.zxxk.com" not in url:
            continue

        # Extract numeric ID from URL (e.g. /soft/38079968.html or ?id=38079968)
        match = re.search(r"[\\/](\d+)(?:\.\w+)?(?:\?|$)", url)
        if not match:
            match = re.search(r"[?&](?:id|soft)=(\d+)", url)

        if not match:
            continue

        url_id = match.group(1)

        if str(xkw_code) == "0":
            continue

        if url_id != str(xkw_code):
            mismatches.append({
                "source_file": json_file.name,
                "filename": item.get("filename", []),
                "title": item.get("title", ""),
                "url": url,
                "url_id": url_id,
                "xkw_code": xkw_code,
            })

with open(output_file, "w", encoding="utf-8") as f:
    json.dump(mismatches, f, ensure_ascii=False, indent=2)

print(f"检查完成：共发现 {len(mismatches)} 条不一致记录，已输出到 {output_file}")
