"""After the data, not sealed: the first link of the loop's pages at earlier dates.

For each title, the revision current at each date (rvstart, rvdir=older, limit 1),
parsed by the API and run through rule.first_link. Output: then.txt.
"""
import os
import fetch, rule

TITLES = ["Social science", "Branches of science", "Formal science", "Mathematics", "Society", "Awareness"]
DATES = ["2024-09-15T00:00:00Z", "2025-09-15T00:00:00Z", "2026-06-30T00:00:00Z", "2026-09-15T00:00:00Z"]
lines = []
for t in TITLES:
    for d in DATES:
        j = fetch.api({"action": "query", "prop": "revisions", "titles": t, "rvstart": d,
                       "rvdir": "older", "rvlimit": "1", "rvprop": "ids|timestamp"})
        rev = j["query"]["pages"][0]["revisions"][0]
        p = fetch.api({"action": "parse", "oldid": rev["revid"], "prop": "text"})["parse"]
        lines.append(f"{t} | as of {d[:10]} (rev {rev['revid']}, {rev['timestamp'][:10]}) -> {rule.first_link(p['text'], t)}")
        print(lines[-1], flush=True)
open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "then.txt"), "w").write("\n".join(lines) + "\n")

# The edit before and after "removed link to common word" (2026-09-08).
for rev in (1372482236, 1373835092):
    p = fetch.api({"action": "parse", "oldid": rev, "prop": "text"})["parse"]
    lines.append(f"Branches of science | rev {rev} -> {rule.first_link(p['text'], 'Branches of science')}")
    print(lines[-1])
open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "then.txt"), "w").write("\n".join(lines) + "\n")
