mirror of
https://github.com/bitbaum/substrata.git
synced 2026-12-04 05:47:59 +01:00
* fix(data): correct what the quality checks and a 36-row source sample found - listings: 8 pinned home lines stored a composite FIGI that OpenFIGI cannot resolve (openfigi.com/id 404, ID_BB_GLOBAL mapping empty); pinnedLine now keeps the exchange line's own FIGI (TSMC, UMC, GlobalWafers, ASE, SMIC, Rio Tinto, NKT, TSMC Advanced Packaging) - sanctions: all 49 EU amendment dates were a day early (Brussels-midnight stamps read in UTC); 4 OFAC programmes had empty titles (menu link parsed first) - usgs-producers: wrapped workbook cells were split into bogus facilities (64 fragments -> 5); Chile's copper table had lost Escondida, Spence, Sierra Gorda and 12 more rows behind a 'Continued' commodity cell - events/dependencies: 7 paraphrased quotes replaced with the source's verbatim text; SK Siltron event names its directory company - series: 18 Cameco points get the date their MD&A states - directory: Shin-Etsu Handotai source 404 replaced; Fangyuan careers URL (domain does not resolve) set to null as its evidence already said Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * feat(data): written quality criteria per dataset, automated checks, /data/quality - config/substrata-quality.ts: 15 datasets x 6 criteria (completeness, correctness, provenance, freshness, link health, consistency); each a rule or 'na' with a reason; freshness reuses config/substrata-freshness.ts - lib/quality: pure checks (USGS world sums, quote states the value, names resolve, cross-dataset agreement with USGS) run in verify with a failure-count ratchet (test/quality.test.ts); network checks (links with 3 tries and a blocked/broken distinction, quotes on page, SEC ticker file, OpenFIGI mapping, USGS values on the chapter PDF) run on a rotation via /api/cron/quality - migration 014: research_quality_checks + research_quality_runs (trend) - /data/quality: scores, counts, trend and every failing row with its source and a prefilled correction issue; linked from /data, footer, palette - check-series.ts shares the reader (CDATA and ' ,' fixes found by the sample) - pnpm run quality [-- --network | --all | --json out.json]; no AI anywhere Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
146 lines
5.6 KiB
Python
146 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Country-level sanctions regimes, from primary sources, dated and linked.
|
|
|
|
Writes research/sanctions.json:
|
|
- EU: every regime on the EU Sanctions Map (sanctionsmap.eu, the Council's
|
|
own register), with who adopted it (EU, UN, both), when it was last
|
|
amended, its legal acts (EUR-Lex / UN links), and each measure's type and
|
|
text. Measures are what is sanctioned (an import ban on listed goods, an
|
|
asset freeze on listed persons), which is never "the country" as a whole.
|
|
- US: OFAC's program list, with the program page for each country program.
|
|
|
|
python3 scripts/research/sanctions.py
|
|
|
|
No key needed. The UK list is a gap: gov.uk publishes regimes as prose pages
|
|
without a machine-readable index of measures per regime.
|
|
"""
|
|
import html
|
|
import json
|
|
from zoneinfo import ZoneInfo
|
|
import re
|
|
import sys
|
|
import time
|
|
import urllib.request
|
|
from datetime import date, datetime, timezone
|
|
from pathlib import Path
|
|
|
|
sys.dont_write_bytecode = True
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
OUT = ROOT / "research" / "sanctions.json"
|
|
EU_API = "https://www.sanctionsmap.eu/api/v1/regime"
|
|
EU_MAP = "https://www.sanctionsmap.eu/#/main/details/{id}/"
|
|
BRUSSELS = ZoneInfo("Europe/Brussels")
|
|
OFAC_LIST = "https://ofac.treasury.gov/sanctions-programs-and-country-information"
|
|
UA = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) substrata-research"}
|
|
|
|
# OFAC program slug -> ISO2 of the country it is about. Thematic programs
|
|
# (counter-terrorism, cyber, Magnitsky…) are not country programs and are left out.
|
|
OFAC_COUNTRY = {
|
|
"afghanistan-related-sanctions": "af",
|
|
"belarus-sanctions": "by",
|
|
"burma": "mm",
|
|
"central-african-republic-sanctions": "cf",
|
|
"chinese-military-companies-sanctions": "cn",
|
|
"cuba-sanctions": "cu",
|
|
"democratic-republic-of-the-congo-related-sanctions": "cd",
|
|
"hong-kong-related-sanctions": "cn",
|
|
"iran-sanctions": "ir",
|
|
"iraq-related-sanctions": "iq",
|
|
"lebanon-related-sanctions": "lb",
|
|
"libya-sanctions": "ly",
|
|
"mali-related-sanctions": "ml",
|
|
"nicaragua-related-sanctions": "ni",
|
|
"north-korea-sanctions": "kp",
|
|
"paarss": "sy",
|
|
"russian-harmful-foreign-activities-sanctions": "ru",
|
|
"somalia-sanctions": "so",
|
|
"south-sudan-related-sanctions": "ss",
|
|
"sudan-and-darfur-sanctions": "sd",
|
|
"ukraine-russia-related-sanctions": "ru",
|
|
"venezuela-related-sanctions": "ve",
|
|
"yemen-related-sanctions": "ye",
|
|
}
|
|
|
|
|
|
def get(url: str) -> bytes:
|
|
req = urllib.request.Request(url, headers=UA)
|
|
with urllib.request.urlopen(req, timeout=60) as res:
|
|
return res.read()
|
|
|
|
|
|
def text(value: str | None) -> str:
|
|
return re.sub(r"\s+", " ", html.unescape(re.sub(r"<[^>]+>", " ", value or ""))).strip()
|
|
|
|
|
|
def eu_regimes() -> list[dict]:
|
|
index = json.loads(get(EU_API))["data"]
|
|
out = []
|
|
for row in index:
|
|
time.sleep(0.2)
|
|
detail = json.loads(get(f"{EU_API}/{row['id']}"))["data"]
|
|
country = detail.get("country")
|
|
country = country.get("data") if isinstance(country, dict) else None
|
|
if not country or not country.get("code"):
|
|
continue # thematic regimes (terrorism, cyber, human rights) have no country
|
|
amended = detail.get("amendment")
|
|
out.append(
|
|
{
|
|
"id": detail["id"],
|
|
"iso2": country["code"].lower(),
|
|
"adoptedBy": detail["adopted_by"]["data"]["title"],
|
|
"title": detail["specification"].strip(),
|
|
# The map stamps an amendment at midnight Brussels time; read in UTC it lands a day early.
|
|
"amended": datetime.fromtimestamp(amended, BRUSSELS).date().isoformat() if amended else None,
|
|
"url": EU_MAP.format(id=detail["id"]),
|
|
"legalActs": [
|
|
{"title": a["title"], "number": a.get("number"), "url": a.get("url")}
|
|
for a in detail.get("legal_acts", {}).get("data", [])
|
|
],
|
|
"measures": [
|
|
{"type": m["type"]["data"]["title"], "text": text(m.get("description"))}
|
|
for m in detail.get("measures", {}).get("data", [])
|
|
if not m.get("suspend")
|
|
],
|
|
}
|
|
)
|
|
return out
|
|
|
|
|
|
def ofac_programs() -> list[dict]:
|
|
page = get(OFAC_LIST).decode("utf-8")
|
|
found = re.findall(r'href="/sanctions-programs-and-country-information/([a-z0-9-]+)"[^>]*>([^<]+)<', page)
|
|
out, seen = [], set()
|
|
for slug, title in found:
|
|
# The page links each programme twice; the first is a menu link with no text.
|
|
if slug in OFAC_COUNTRY and slug not in seen and title.strip():
|
|
seen.add(slug)
|
|
out.append(
|
|
{
|
|
"iso2": OFAC_COUNTRY[slug],
|
|
"title": html.unescape(title).strip(),
|
|
"url": f"{OFAC_LIST}/{slug}",
|
|
}
|
|
)
|
|
missing = set(OFAC_COUNTRY) - seen
|
|
if missing:
|
|
raise SystemExit(f"OFAC list no longer has {sorted(missing)} — re-read {OFAC_LIST}")
|
|
return out
|
|
|
|
|
|
def main() -> None:
|
|
doc = {
|
|
"retrieved": date.today().isoformat(),
|
|
"eu": {"source": "EU Sanctions Map", "api": EU_API, "regimes": eu_regimes()},
|
|
"us": {"source": "OFAC, Sanctions Programs and Country Information", "url": OFAC_LIST, "programs": ofac_programs()},
|
|
"gaps": [
|
|
"UK: gov.uk publishes each regime as prose, with no machine-readable list of measures per regime; not read.",
|
|
],
|
|
}
|
|
OUT.write_text(json.dumps(doc, indent=1, ensure_ascii=False) + "\n")
|
|
print(f"wrote {OUT.relative_to(ROOT)}: {len(doc['eu']['regimes'])} EU regimes, {len(doc['us']['programs'])} OFAC programs")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|