"""Fast volume check - fetch page data only, no popup API calls (huge savings)."""
import json, re, urllib.request, sys
API = "http://localhost:8000/api/pages"
def get_json(url, timeout=8):
try:
resp = urllib.request.urlopen(url, timeout=timeout)
return json.loads(resp.read())
except Exception as e:
return {"_error": str(e)}
def lookup(vol, page, fid):
"""Quick popup test."""
import urllib.parse
url = f"http://localhost:8000/api/reference/lookup?vol={vol}&page={page}&type=footnote&id={urllib.parse.quote(fid)}"
try:
resp = urllib.request.urlopen(url, timeout=5)
d = json.loads(resp.read())
return "OK" if d.get("found") else "NF"
except:
return "ERR"
print(f"{'Vol':>3} {'Pg':>3} {'FN':>3} {'Sup':>3} {'First FN':<50} {'Status'}")
print("=" * 80)
all_data = {} # vol -> (page, fn_count, sup_count, first_fn, all_fns)
errors = []
for vol in range(1, 46):
d = None
used = None
for pn in range(1, 4):
d = get_json(f"{API}/{vol}/{pn}")
if "_error" in d:
print(f"{vol:>3} --- --- --- ERROR: {d['_error']:<50} ā")
errors.append(vol)
d = None
break
fns = d.get("footnotes", [])
blank = d.get("is_blank", False)
if fns or not blank or pn == 3:
used = pn
break
# blank page with no footnotes -> try next
if d is None or d.get("_error"):
continue
fns = d.get("footnotes", [])
html = d.get("content_html_formatted", "")
sups = re.findall(r']*>.*?', html)
fn_count = len(fns)
sup_count = len(sups)
first_fn = (fns[0]["content"][:50].replace("\n", " ")) if fns else "(none)"
# Check for anomalies
issues = []
if fn_count > 0 and sup_count == 0:
issues.append("NOSUP")
if fn_count > 0 and sup_count > 0 and sup_count < fn_count:
# Some markers might be different type - note for detail
issues.append(f"Sup3} {used:>3} {fn_count:>3} {sup_count:>3} {first_fn:<50} ā ļø {iss}")
all_data[vol] = (used, fn_count, sup_count, fns, html)
else:
print(f"{vol:>3} {used:>3} {fn_count:>3} {sup_count:>3} {first_fn:<50} ā
")
all_data[vol] = (used, fn_count, sup_count, fns, html)
print("=" * 80)
good = 45 - len(errors) - len([v for v in all_data if v in all_data and v not in errors])
issues_list = [v for v in all_data if v not in errors]
issue_vols = [v for v in issues_list if all_data[v][1] > 0 and all_data[v][2] == 0] # FN>0 but Sup=0
print(f"\nChecked: 45 volumes")
print(f"Full OK: {45 - len(errors) - len(issues_list)}")
print(f"Has footnotes: {sum(1 for v in all_data if v not in errors and all_data[v][1] > 0)}")
print(f"Has NO footnotes: {sum(1 for v in all_data if v not in errors and all_data[v][1] == 0)}")
if issue_vols:
print(f"\nš“ Volumes with FN>0 but Sup=0 (NO sup tags!): {len(issue_vols)}")
for v in issue_vols:
pg, fnc, spc, fns, html = all_data[v]
print(f" Vol {v} Page {pg}: {fnc} footnotes, {spc} sup tags")
for fn in fns:
print(f" [{fn['id']}] {fn['content'][:80]}")
# Test popup
for fn in fns[:1]:
r = lookup(v, pg, fn["id"])
print(f" popup test: {r}")
# Check volumes with Sup 0 and all_data[v][2] > 0 and all_data[v][2] < all_data[v][1]]
if partial:
print(f"\nš” Volumes with Sup