185 lines
6.3 KiB
Python
185 lines
6.3 KiB
Python
import json
|
|||
|
|
import re
|
||
|
|
|
||
|
|
def clean_val(v):
|
||
|
|
if not v:
|
||
|
|
return ""
|
||
|
|
v = v.strip()
|
||
|
|
# remove backticks
|
||
|
|
if v.startswith("`") and v.endswith("`"):
|
||
|
|
v = v[1:-1].strip()
|
||
|
|
# remove bold
|
||
|
|
if v.startswith("**") and v.endswith("**"):
|
||
|
|
v = v[2:-2].strip()
|
||
|
|
# remove quotes
|
||
|
|
if (v.startswith('"') and v.endswith('"')) or (v.startswith("'") and v.endswith("'")):
|
||
|
|
v = v[1:-1].strip()
|
||
|
|
return v
|
||
|
|
|
||
|
|
def is_req(req_str):
|
||
|
|
req_str = req_str.lower()
|
||
|
|
return "req" in req_str and "opt" not in req_str
|
||
|
|
|
||
|
|
def parse_md():
|
||
|
|
with open("web/docs/FASTAPI_BACKEND_API_DOCUMENTATION.md", "r") as f:
|
||
|
|
md_text = f.read()
|
||
|
|
|
||
|
|
sections = re.split(r"\n(?=###\s+)", md_text)
|
||
|
|
md_items = []
|
||
|
|
for s in sections[1:]:
|
||
|
|
lines = s.strip().split("\n")
|
||
|
|
header = lines[0].replace("###", "").strip()
|
||
|
|
|
||
|
|
url_m = re.search(r"\*\*URL:\*\*\s*`([^`]+)`", s)
|
||
|
|
url = url_m.group(1).strip() if url_m else ""
|
||
|
|
|
||
|
|
subs_m = re.search(r"\*\(Sub-endpoints:\s*([^\)]+)\)\*", s)
|
||
|
|
subs = subs_m.group(1).strip() if subs_m else ""
|
||
|
|
|
||
|
|
meth_m = re.search(r"\*\*Method:\*\*\s*`?([A-Za-z0-9_\/ ]+)`?", s)
|
||
|
|
method = meth_m.group(1).strip() if meth_m else ""
|
||
|
|
|
||
|
|
# parse table
|
||
|
|
table_lines = []
|
||
|
|
in_table = False
|
||
|
|
for l in lines:
|
||
|
|
ls = l.strip()
|
||
|
|
if ls.startswith("|") and ls.endswith("|"):
|
||
|
|
in_table = True
|
||
|
|
table_lines.append(ls)
|
||
|
|
elif in_table:
|
||
|
|
break
|
||
|
|
|
||
|
|
params = []
|
||
|
|
if len(table_lines) >= 3:
|
||
|
|
raw_headers = [c.strip() for c in table_lines[0].strip("|").split("|")]
|
||
|
|
for l in table_lines[2:]:
|
||
|
|
cols = [c.strip() for c in l.strip("|").split("|")]
|
||
|
|
if len(cols) == len(raw_headers):
|
||
|
|
row = dict(zip(raw_headers, cols))
|
||
|
|
field = clean_val(row.get("Field", ""))
|
||
|
|
ty = clean_val(row.get("Type", ""))
|
||
|
|
req_raw = row.get("Required", "")
|
||
|
|
req = is_req(req_raw)
|
||
|
|
df = clean_val(row.get("Default", ""))
|
||
|
|
ds = row.get("Description", "")
|
||
|
|
params.append({
|
||
|
|
"field": field,
|
||
|
|
"type": ty,
|
||
|
|
"required": req,
|
||
|
|
"required_raw": req_raw,
|
||
|
|
"default": df,
|
||
|
|
"description": ds
|
||
|
|
})
|
||
|
|
|
||
|
|
md_items.append({
|
||
|
|
"header": header,
|
||
|
|
"url": url,
|
||
|
|
"subs": subs,
|
||
|
|
"method": method,
|
||
|
|
"params": params,
|
||
|
|
"raw_text": s
|
||
|
|
})
|
||
|
|
return md_items
|
||
|
|
|
||
|
|
def parse_nexus():
|
||
|
|
with open("web/docs/data.json", "r") as f:
|
||
|
|
nexus_data = json.load(f)
|
||
|
|
|
||
|
|
nexus_apis = []
|
||
|
|
for s_idx, sec in enumerate(nexus_data["sections"]):
|
||
|
|
for api in sec.get("eps", []):
|
||
|
|
nexus_apis.append({
|
||
|
|
"sec_title": sec.get("title"),
|
||
|
|
"api_id": api.get("id"),
|
||
|
|
"api_key": api.get("api_key"),
|
||
|
|
"title": api.get("title"),
|
||
|
|
"api_ip_link": api.get("api_ip_link"),
|
||
|
|
"full_url": api.get("full_url"),
|
||
|
|
"path": api.get("path"),
|
||
|
|
"method": api.get("method"),
|
||
|
|
"params": api.get("params", []),
|
||
|
|
"endpoints": api.get("endpoints", [])
|
||
|
|
})
|
||
|
|
return nexus_apis
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
md_items = parse_md()
|
||
|
|
nexus_apis = parse_nexus()
|
||
|
|
print(f"MD sections: {len(md_items)}, Nexus APIs: {len(nexus_apis)}")
|
||
|
|
|
||
|
|
import re
|
||
|
|
|
||
|
|
def run_comparison():
|
||
|
|
md_items = parse_md()
|
||
|
|
nexus_apis = parse_nexus()
|
||
|
|
|
||
|
|
# Map by direct full_url, endpoint full_url, api_ip_link, or path
|
||
|
|
url_to_nexus = {}
|
||
|
|
path_to_nexus = {}
|
||
|
|
for a in nexus_apis:
|
||
|
|
if a.get("full_url"):
|
||
|
|
url_to_nexus[a["full_url"].rstrip("/")] = a
|
||
|
|
if a.get("api_ip_link"):
|
||
|
|
url_to_nexus[a["api_ip_link"].rstrip("/")] = a
|
||
|
|
if a.get("path"):
|
||
|
|
path_to_nexus[a["path"].rstrip("/")] = a
|
||
|
|
for se in a.get("endpoints", []):
|
||
|
|
if se.get("full_url"):
|
||
|
|
url_to_nexus[se["full_url"].rstrip("/")] = a
|
||
|
|
if se.get("path"):
|
||
|
|
path_to_nexus[se["path"].rstrip("/")] = a
|
||
|
|
|
||
|
|
results = []
|
||
|
|
|
||
|
|
for idx, md in enumerate(md_items):
|
||
|
|
m_url = md["url"].rstrip("/")
|
||
|
|
matched = None
|
||
|
|
|
||
|
|
# 1. Direct url match
|
||
|
|
if m_url in url_to_nexus:
|
||
|
|
matched = url_to_nexus[m_url]
|
||
|
|
else:
|
||
|
|
# 2. Check if m_url ends with any known path
|
||
|
|
for p, a in path_to_nexus.items():
|
||
|
|
if m_url.endswith(p):
|
||
|
|
matched = a
|
||
|
|
break
|
||
|
|
# 3. Path suffix match or partial url
|
||
|
|
if not matched:
|
||
|
|
for u, a in url_to_nexus.items():
|
||
|
|
if u.endswith(m_url) or m_url.endswith(u):
|
||
|
|
matched = a
|
||
|
|
break
|
||
|
|
# 4. Check special cases
|
||
|
|
if not matched:
|
||
|
|
# check header keyword match
|
||
|
|
hdr_lower = md["header"].lower()
|
||
|
|
for a in nexus_apis:
|
||
|
|
key_lower = a.get("api_key", "").lower()
|
||
|
|
title_lower = a.get("title", "").lower()
|
||
|
|
if "health" in hdr_lower and "health" in title_lower:
|
||
|
|
matched = a
|
||
|
|
break
|
||
|
|
elif "trendy" in hdr_lower and "trendy" in key_lower:
|
||
|
|
matched = a
|
||
|
|
break
|
||
|
|
elif "hair_segmentation_yolo_sam" in m_url and "hair_segmentation" in key_lower:
|
||
|
|
matched = a
|
||
|
|
break
|
||
|
|
elif "caricature_effects" in m_url and "caricature_effects" in key_lower:
|
||
|
|
matched = a
|
||
|
|
break
|
||
|
|
|
||
|
|
results.append((md, matched))
|
||
|
|
|
||
|
|
return results
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
results = run_comparison()
|
||
|
|
matched_cnt = sum(1 for m, n in results if n is not None)
|
||
|
|
print(f"Total MD items: {len(results)}, Matched: {matched_cnt}")
|
||
|
|
for idx, (m, n) in enumerate(results):
|
||
|
|
t_str = n["title"] if n else "NO MATCH"
|
||
|
|
print(f"[{idx+1:2d}] {m['header'][:40]:40s} -> {t_str}")
|