"""How much of the site: route's items do plain topic queries return?

Usage: python3 topic_coverage.py items.csv "query one" "query two" ...
Each query gets after:/before: appended for the collection window.
Matches by normalised title (outlet suffix kept, since the same headline
appears on several outlets).
"""
import csv, sys, time, urllib.parse, urllib.request
import xml.etree.ElementTree as ET

AFTER, BEFORE = "2026-10-09", "2026-10-17"
KEPT = {"verticalfarmdaily.com", "igrownews.com", "hortidaily.com",
        "openpr.com", "bioengineer.org", "hortibiz.com"}

def norm(t):
    return " ".join(t.lower().split())

want = {}
for r in csv.DictReader(open(sys.argv[1])):
    if r["domain"] in KEPT:
        want[norm(r["title"])] = r["domain"]
print(f"{len(want)} items from {len(KEPT)} kept outlets via site:")

got = set()
for q in sys.argv[2:]:
    url = "https://news.google.com/rss/search?q=" + urllib.parse.quote_plus(
        f"{q} after:{AFTER} before:{BEFORE}") + "&hl=en-US&gl=US&ceid=US:en"
    req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
    root = ET.fromstring(urllib.request.urlopen(req, timeout=30).read())
    titles = {norm(i.findtext("title") or "") for i in root.iter("item")}
    hit = titles & set(want)
    got |= hit
    print(f"{q!r}: {len(titles)} items returned, {len(hit)} of the site: items")
    time.sleep(3)
print(f"union: {len(got)} of {len(want)}")
by = {}
for t in want:
    by.setdefault(want[t], [0, 0])[0] += 1
    if t in got:
        by[want[t]][1] += 1
for d, (n, g) in by.items():
    print(f"  {d}: {g}/{n}")
