extract_qiwei_api.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import re
  2. import json
  3. from pathlib import Path
  4. ROOT = Path(__file__).resolve().parents[1]
  5. MD_DIR = ROOT / "doc" / "文档" / "QiWe开放平台文档" / "md"
  6. README = ROOT / "doc" / "文档" / "QiWe开放平台文档" / "README.md"
  7. API_FILES = {
  8. "API-01": [],
  9. "API-02": [
  10. "创建设备(步骤1).md",
  11. "二维码-获取(步骤2).md",
  12. "二维码状态-检测(步骤3).md",
  13. "二维码-code验证(步骤4).md",
  14. ],
  15. "API-03": ["设置回调地址.md"],
  16. "API-04": ["回调结构说明.md"],
  17. "API-05": ["用户状态.md"],
  18. "API-06": ["同步历史消息分页.md"],
  19. "API-07": ["群分页.md"],
  20. "API-08": ["群详情-批量.md"],
  21. "API-09": ["群成员变动查询.md"],
  22. "API-10": ["创建群.md"],
  23. "API-11": ["修改群公告.md"],
  24. "API-12": ["群消息置顶-列表.md"],
  25. "API-13": ["群消息置顶-添加.md"],
  26. "API-14": ["发送纯文本消息.md"],
  27. "API-15": ["群发消息.md"],
  28. "API-16": ["群发消息-状态查询.md"],
  29. "API-17": ["外部联系人分页.md"],
  30. "API-18": ["联系人详情-批量.md"],
  31. "API-19": ["客户标签-增删.md"],
  32. "API-20": ["添加群成员好友.md"],
  33. "API-21": ["企微文件下载.md"],
  34. }
  35. def extract(path: Path) -> dict:
  36. if not path.exists():
  37. return {}
  38. text = path.read_text(encoding="utf-8")
  39. out: dict = {}
  40. mm = re.search(r'"method"\s*:\s*"([^"]+)"', text)
  41. if mm:
  42. out["method"] = mm.group(1)
  43. blocks = re.findall(r"```\s*\n\s*(\{[\s\S]*?\})\s*\n\s*```", text)
  44. for b in blocks:
  45. if '"method"' in b and "params" in b:
  46. out["request"] = b.strip()
  47. break
  48. for b in blocks:
  49. if '"code"' in b and "data" in b and "method" not in b:
  50. out["response"] = b.strip()
  51. break
  52. # notes from bullet lines
  53. notes = [
  54. ln.strip("•").strip()
  55. for ln in text.splitlines()
  56. if ln.strip().startswith("•") or ln.strip().startswith("-")
  57. ]
  58. if notes:
  59. out["notes"] = notes[:5]
  60. return out
  61. def main():
  62. index = {}
  63. for line in README.read_text(encoding="utf-8").splitlines():
  64. m = re.match(r"- \[(.+?)\]\(md/(.+?)\) - (https://.+)", line)
  65. if m:
  66. index[m.group(2)] = {"title": m.group(1), "url": m.group(3)}
  67. result = {}
  68. for api, files in API_FILES.items():
  69. result[api] = {"files": []}
  70. for f in files:
  71. p = MD_DIR / f
  72. info = extract(p)
  73. info["file"] = f
  74. info["local"] = f"./文档/QiWe开放平台文档/md/{f}"
  75. if f in index:
  76. info["title"] = index[f]["title"]
  77. info["url"] = index[f]["url"]
  78. result[api]["files"].append(info)
  79. if info.get("method") and "method" not in result[api]:
  80. result[api]["method"] = info["method"]
  81. out = ROOT / "doc" / "文档" / "qiwei_api_extracted.json"
  82. out.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
  83. print("wrote", out)
  84. for api, data in result.items():
  85. print(api, data.get("method", "-"), len(data["files"]))
  86. if __name__ == "__main__":
  87. main()