Add CTU catalogue parser and research contract

parse_catalog.py turns a saved copy of the CTU Mobility catalogue (FEL, Master,
2027/28) into data/catalog.json: 186 agreements merged into 170 schools.
research/BRIEF.md is the contract for per-school research (JSON shape, scoring
anchors, source rules); validate_research.py enforces its structure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gXkMmmRRKLSnY6vQN3
This commit is contained in:
agent 2026-11-12 20:55:35 +00:00
parent fcfb2bcd87
commit 3980fbd62b
16 changed files with 16805 additions and 0 deletions

118
scripts/parse_catalog.py Normal file
View file

@ -0,0 +1,118 @@
#!/usr/bin/env python3
"""Parse a SingleFile save of the CTU Mobility "Catalog of agreements" page.
Usage: python3 -I scripts/parse_catalog.py <saved-catalog.html> data/catalog.json
The saved page is a rendered Vue table. Every agreement is one
<tr class="row-headline"> with cells identified by data-testid. School names
are truncated in the table ("Instituto Tecnológico de B..."), so the research
step resolves full names from the web/QS links kept here.
Output: {"source": {...}, "agreements": [...], "schools": [...]}
agreements - one entry per table row (186 for FEL / Master / 2027)
schools - agreements merged by school code (one card per school)
"""
import html
import json
import re
import sys
from collections import OrderedDict
ISCED = {
"00": "Generic programmes (any field)", "0000": "Generic programmes (any field)",
"0533": "Physics", "0541": "Mathematics",
"061": "ICT", "0610": "ICT (general)", "0611": "Computer use", "0619": "ICT (other)",
"071": "Engineering", "0710": "Engineering (general)", "0713": "Electricity & energy",
"0714": "Electronics & automation", "0715": "Mechanics & metal trades",
"0716": "Motor vehicles, ships & aircraft", "0730": "Architecture & construction",
}
def text(fragment: str) -> str:
t = re.sub(r"<[^>]+>", " ", fragment)
return re.sub(r"\s+", " ", html.unescape(t)).strip()
def cell(row: str, testid: str) -> str:
m = re.search(r"data-testid=%s>(.*?)(?=<td|$)" % re.escape(testid), row, flags=re.S)
return text(m.group(1)) if m else ""
def parse_languages(raw: str):
"""'de (B2) en (B2)' -> [{'lang': 'de', 'level': 'B2'}, ...] (deduplicated)."""
seen, out = set(), []
for lang, level in re.findall(r"([a-z]{2})\s*\(([ABC][12])\)", raw):
if (lang, level) not in seen:
seen.add((lang, level))
out.append({"lang": lang, "level": level})
return out
def main(src_path: str, out_path: str) -> None:
src = open(src_path, encoding="utf-8").read()
meta_url = re.search(r"url:\s*(\S+)", src)
meta_date = re.search(r"saved date:\s*([^\n]+)", src)
src = re.sub(r"<style[^>]*>.*?</style>", " ", src, flags=re.S | re.I)
src = re.sub(r"<svg[^>]*>.*?</svg>", "", src, flags=re.S | re.I)
agreements = []
for chunk in re.split(r'<tr class="row-headline', src)[1:]:
row = chunk.split('<tr class="row-details')[0]
links = {label.strip(): html.unescape(href.strip('"'))
for href, label in re.findall(r'<a[^>]*href=("[^"]*"|\S+)[^>]*>([^<]*)</a>', row)}
lat = lon = None
m = re.search(r"x=([-\d.]+)&y=([-\d.]+)", links.get("Maps", ""))
if m:
lon, lat = float(m.group(1)), float(m.group(2))
places_raw = cell(row, "places")
isced = cell(row, "specialization").split()
agreements.append({
"code": cell(row, "code"),
"catalogName": cell(row, "name"),
"country": cell(row, "country"),
# "FEE" = Faculty of Electrical Engineering (FEL) agreement; blank = CTU-wide
"scope": "FEL" if cell(row, "faculty") == "FEE" else "CTU-wide",
"places": int(places_raw) if places_raw.isdigit() else None,
"isced": isced,
"iscedLabels": [ISCED.get(c, c) for c in isced],
"levels": cell(row, "studyType").split(),
"languages": parse_languages(cell(row, "language")),
"web": links.get("Web"),
"factsheet": links.get("Factsheet"),
"qs": links.get("QS"),
"lat": lat, "lon": lon,
})
schools = OrderedDict()
for a in agreements:
s = schools.setdefault(a["code"], {
"code": a["code"],
"slug": re.sub(r"[^A-Za-z0-9]+", "_", a["code"]).strip("_"),
"catalogName": a["catalogName"], "country": a["country"],
"web": a["web"], "qs": a["qs"], "lat": a["lat"], "lon": a["lon"],
"agreements": [],
})
s["agreements"].append({k: a[k] for k in
("scope", "places", "isced", "iscedLabels", "levels", "languages", "factsheet")})
for k in ("web", "qs", "lat", "lon"):
s[k] = s[k] or a[k]
out = {
"source": {
"url": meta_url.group(1) if meta_url else None,
"savedAt": meta_date.group(1).strip() if meta_date else None,
"academicYear": "2027/2028", "faculty": "FEL (13000)", "studyLevel": "Master (N)",
},
"agreements": agreements,
"schools": list(schools.values()),
}
with open(out_path, "w", encoding="utf-8") as f:
json.dump(out, f, ensure_ascii=False, indent=1)
f.write("\n")
print(f"{len(agreements)} agreements, {len(schools)} schools -> {out_path}")
if __name__ == "__main__":
if len(sys.argv) != 3:
sys.exit(__doc__)
main(sys.argv[1], sys.argv[2])