#!/usr/bin/env python
"""content-c6281-000: how often the Federal Register's full text carries the phrase, by year. The FR API's
conditions[term] is a full-text search; a quoted term is a phrase. One GET per (term, year); prints the count field
the API returns and writes fr_ai_agents_c6281.json beside itself. Control: 'artificial intelligence' per year, which
must be large and rising, and a nonsense phrase, which must be 0.

    python fr_ai_agents_c6281.py > fr_ai_agents_c6281.out.txt
"""
import json
import time
import urllib.parse
import urllib.request
from pathlib import Path

HERE = Path(__file__).resolve().parent
API = "https://www.federalregister.gov/api/v1/documents.json"
TERMS = ['"AI agents"', '"AI agent"', '"agentic"', '"autonomous agents"', '"artificial intelligence"',
         '"zxqv agentoid"']   # the last is the zero control
YEARS = list(range(2019, 2027))


def count(term, year):
    q = urllib.parse.urlencode({"conditions[term]": term, "conditions[publication_date][year]": year, "per_page": 1})
    req = urllib.request.Request(API + "?" + q, headers={"User-Agent": "fleet-c6281 census (contact via site)"})
    with urllib.request.urlopen(req, timeout=60) as r:
        return json.load(r)["count"]


def main():
    out = {}
    print("pulled", time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()))
    print("%-26s" % "term" + "".join("%7d" % y for y in YEARS))
    for t in TERMS:
        row = {}
        for y in YEARS:
            try:
                row[y] = count(t, y)
            except Exception as e:                                       # noqa: BLE001
                row[y] = "ERR:%s" % e.__class__.__name__
            time.sleep(0.6)
        out[t] = row
        print("%-26s" % t + "".join("%7s" % row[y] for y in YEARS))
    (HERE / "fr_ai_agents_c6281.json").write_text(json.dumps(out, indent=1), encoding="utf-8")
    print("2026 is through the pull date above, not a full year")


if __name__ == "__main__":
    main()
