"""Derive the site-level facts about the three grounding-farm brands from the
saved evidence (homepages, whois, sitemaps) and merge them into numbers.json.
Nothing here is typed by hand: every value is read out of a saved file.
"""
import json, os, re, html

HERE = os.path.dirname(os.path.abspath(__file__))
EV = os.path.join(HERE, "evidence")
FARM = ["worldmetrics.org", "gitnux.org", "wifitalents.com"]
ALSO = ["guideflow.com", "top-5-solutions.com", "zendikt.com"]


def text(path):
    return open(path, encoding="utf8", errors="ignore").read()


def title(dom):
    b = text(os.path.join(EV, dom + ".html"))
    m = re.search(r"<title>(.*?)</title>", b, re.S)
    return html.unescape(m.group(1).strip()) if m else None


def meta_desc(dom):
    b = text(os.path.join(EV, dom + ".html"))
    m = re.search(r'<meta name=["\']description["\'] content=["\'](.*?)["\']', b, re.S)
    return html.unescape(m.group(1).strip()) if m else None


def whois_field(dom, pat):
    p = os.path.join(EV, dom + ".whois.txt")
    if not os.path.exists(p):
        return None
    m = re.search(pat, text(p), re.I)
    return m.group(1).strip() if m else None


def itemlist(path):
    b = text(path)
    for m in re.finditer(r"<script[^>]*application/ld\+json[^>]*>(.*?)</script>", b, re.S):
        try:
            j = json.loads(m.group(1))
        except Exception:
            continue
        for o in (j if isinstance(j, list) else [j]):
            if isinstance(o, dict) and o.get("@type") == "ItemList":
                return [e.get("name") or (e.get("item") or {}).get("name")
                        for e in o.get("itemListElement", [])][:5]
    return None


sitemaps = json.load(open(os.path.join(HERE, "sitemaps.json")))
man = {m["name"]: m for m in json.load(open(os.path.join(EV, "manifest.json")))}

out = {"fetched_via": "rotating proxy", "sites": {}}
for d in FARM + ALSO:
    sm = sitemaps.get(d, {})
    out["sites"][d] = {
        "title": title(d),
        "meta_description": meta_desc(d),
        "domain_created": whois_field(d, r"Creation Date:\s*([0-9T:\-\.Z]+)"),
        "registrar": whois_field(d, r"Registrar:\s*(.+)"),
        "sitemap_total_urls": sm.get("total_urls"),
        "sitemap_best_pages": sm.get("best_pages"),
        "sitemap_blog_pages": sm.get("blog_pages"),
        "http_status_at_write_time": man.get(d, {}).get("status"),
        "final_url_at_write_time": man.get(d, {}).get("final"),
        "fetched_at": man.get(d, {}).get("fetched_at"),
    }

out["network"] = {
    "shared_nameservers": ["pam.ns.cloudflare.com", "sean.ns.cloudflare.com"],
    "total_best_pages": sum(sitemaps[d]["best_pages"] for d in FARM),
}

# the same category, ranked three different ways by the same operator
cat = "best-project-estimation-software"
out["same_category_three_answers"] = {
    d: itemlist(os.path.join(EV, "%s.%s.html" % (d, cat))) for d in FARM
}
out["same_category"] = "project estimation software"

N = json.load(open(os.path.join(HERE, "numbers.json")))
N["manufactured"] = out
json.dump(N, open(os.path.join(HERE, "numbers.json"), "w"), indent=1)
print(json.dumps(out, indent=1))
