#!/usr/bin/env python3
import json

# Bot ISP keywords
bots_keywords = ['line corp', 'digitalocean', 'amazon', 'logicweb', 'constant company', 'akamai',
                 'facebook', 'microsoft', 'google', 'cloudflare', 'm247', 'egihosting',
                 'ovh', 'hetzner', 'linode', 'vultr', 'contabo', 'meta platform']

inserts = []
seen = set()

with open('/tmp/ip_results.json') as f:
    for line in f:
        line = line.strip()
        if not line:
            continue
        try:
            d = json.loads(line)
            ip = d.get('query', '')
            if ip in seen:
                continue
            seen.add(ip)

            country = d.get('country', '').replace("'", "\\'")
            region = d.get('regionName', '').replace("'", "\\'")
            city = d.get('city', '').replace("'", "\\'")
            isp = d.get('isp', '').replace("'", "\\'")

            is_bot = 1 if any(b in isp.lower() for b in bots_keywords) else 0

            inserts.append(f"('{ip}', '{country}', '{region}', '{city}', '{isp}', {is_bot})")
        except:
            pass

sql = f"""INSERT IGNORE INTO visitor_ips (ip, country, region, city, isp, is_bot) VALUES
{','.join(inserts)};
SELECT COUNT(*) as imported FROM visitor_ips;
"""

print(sql)
