import sys, re, time, json
sys.path.insert(0, sys.argv[1])
from peek import get
brands = ["acura","honda","toyota","lexus","scion","nissan","infiniti","mazda","subaru","mitsubishi","hyundai","kia","genesis",
          "ford","lincoln","gm","chevrolet","gmc","buick","cadillac","mopar","chrysler","dodge","jeep","ram",
          "vw","volkswagen","audi","bmw","mini","mercedes","mercedesbenz","volvo","porsche","landrover","jaguar","tesla","fiat","alfaromeo"]
pats = ["{b}partsdeal.com","{b}partsgiant.com","{b}partsnow.com"]
out = []
for b in brands:
    for p in pats:
        dom = p.format(b=b)
        s, body = get("https://www." + dom + "/")
        t = body.decode("utf8", "ignore") if body else ""
        row = {"brand": b, "domain": dom, "home": s}
        if s == 200 and "/Page_Customer/" not in t and len(t) < 2000:
            row["note"] = "small page"
        if s == 200:
            row["family"] = bool(re.search(r"©\s*2006-20\d\d", t))
            links = re.findall(r'href="(/oem-[^"]+\.html)"', t)
            row["oem_links"] = len(links)
            if links:
                time.sleep(1)
                s2, b2 = get("https://www." + dom + links[0])
                t2 = b2.decode("utf8", "ignore")
                prices = re.findall(r'"price"\s*:\s*"?([\d.]+)', t2)
                row["test_page"] = links[0]; row["test_status"] = s2; row["prices"] = prices[:2]
            row["vin"] = "vin" in t.lower()
            row["cf"] = "just a moment" in t.lower()
        print(json.dumps(row), flush=True)
        time.sleep(0.7)
