#!/usr/bin/env python3
"""CitedBy study, November 2026: do mid-ranked German, French, Spanish and Dutch websites let AI crawlers in?
Prints every number on https://citedby.eu/data/ai-crawler-access-midtail from the CSV published next to it:

    python3 ai-crawler-access-midtail-2026-11.py ai-crawler-access-midtail-2026-11.csv

One CSV row = one website. robots_<bot> = what the site's robots.txt says for that crawler (allowed / blocked), filled only when the
robots.txt was read (robots_status 200, or 404/410 = no file = allowed). request_<bot> = what our own test request got on the home
page when it sent that crawler's user-agent, as the check judges it: allowed; refused (robots.txt allows the crawler but our request did not get the
normal page: blocked, challenged, rate-limited or empty); blocked (robots.txt disallows it); unverified (no usable answer). Filled only when the
home page opened for a normal browser request (home_status 200). template_robots = yes when the site's robots.txt is the same
boilerplate as 86 others (it starts "Global defaults - block all except the whitelisted bots"); those sites are counted apart.
A request from our server is not the real crawler (which comes from the operator's published IP ranges), so a refusal is what our
request got, not proof that the real crawler is refused. Licence CC BY 4.0, CitedBy (https://citedby.eu/data/ai-crawler-access-midtail).
Standard library only."""
import csv, sys
from collections import Counter

BOTS = ["OAI-SearchBot", "GPTBot", "PerplexityBot", "ClaudeBot", "Google-Extended", "Bingbot", "Googlebot"]
AI4 = ["OAI-SearchBot", "GPTBot", "PerplexityBot", "ClaudeBot"]


def compute(path):
    rows = list(csv.DictReader(open(path, encoding="utf-8")))
    ind = [r for r in rows if r["template_robots"] == "no"]
    tpl = [r for r in rows if r["template_robots"] == "yes"]
    rb = [r for r in ind if r["robots_status"] in ("200", "404", "410")]
    hp = [r for r in ind if r["home_status"] == "200"]  # sites whose home page opened for a browser request
    k = lambda base, f: sum(1 for r in base if f(r))
    out = {"sampled": len(rows), "template": len(tpl), "template_by_tld": dict(Counter(r["tld"] for r in tpl)),
           "template_oai_blocked": k(tpl, lambda r: r["robots_OAI-SearchBot"] == "blocked"),
           "oai_unverified": k(hp, lambda r: r["request_OAI-SearchBot"] == "unverified"),
           "template_googlebot_allowed": k(tpl, lambda r: r["robots_Googlebot"] == "allowed"),
           "independent": len(ind), "by_tld": dict(Counter(r["tld"] for r in rows)),
           "ranks": (min(int(r["tranco_rank"]) for r in rows), max(int(r["tranco_rank"]) for r in rows)),
           "read_from": min(r["read_utc"] for r in rows), "read_to": max(r["read_utc"] for r in rows),
           "robots_read": len(rb), "robots_file": k(rb, lambda r: r["robots_status"] == "200"), "robots_none": k(rb, lambda r: r["robots_status"] != "200"),
           "robots_unread": len(ind) - len(rb), "blocked": {b: k(rb, lambda r, b=b: r["robots_" + b] == "blocked") for b in BOTS},
           "blocked_any4": k(rb, lambda r: any(r["robots_" + b] == "blocked" for b in AI4)),
           "gptbot_only": k(rb, lambda r: r["robots_GPTBot"] == "blocked" and r["robots_OAI-SearchBot"] == "allowed"),
           "oai_not_gpt": k(rb, lambda r: r["robots_OAI-SearchBot"] == "blocked" and r["robots_GPTBot"] == "allowed"),
           "home_ok": len(hp), "home_status": dict(Counter(r["home_status"] or "no answer" for r in ind).most_common()),
           "refused": {b: k(hp, lambda r, b=b: r["request_" + b] == "refused") for b in BOTS if b != "Google-Extended"},
           "oai_refused_google_ok": k(hp, lambda r: r["request_OAI-SearchBot"] == "refused" and r["request_Googlebot"] == "allowed"),
           "oai_refused_google_ok_bing_ok": k(hp, lambda r: r["request_OAI-SearchBot"] == "refused" and r["request_Googlebot"] == "allowed" and r["request_Bingbot"] == "allowed"),
           "oai_blocked_or_refused": k(hp, lambda r: r["robots_OAI-SearchBot"] == "blocked" or r["request_OAI-SearchBot"] == "refused"),
           "oai_blocked_by_tld": {t: (k([r for r in rb if r["tld"] == t], lambda r: r["robots_OAI-SearchBot"] == "blocked"), len([r for r in rb if r["tld"] == t])) for t in ("de", "fr", "es", "nl")}}
    return out


if __name__ == "__main__":
    d = compute(sys.argv[1])
    for key, v in d.items():
        print(key, v)
