import json, os, sys, urllib.request, urllib.parse, urllib.error SECRETS = json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) INPUTS = json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) BASE = SECRETS.get("base_url", "https://api.ipinfo.io").rstrip("/") # IPinfo caps a single Lite batch call at 1000 addresses. CHUNK = 1000 def post_chunk(url, ips): # POST /batch/lite expects a raw JSON array of IPs, not an object. data = json.dumps(ips).encode("utf-8") headers = {"Content-Type": "application/json", "Accept": "application/json"} req = urllib.request.Request(url, data=data, headers=headers, method="POST") with urllib.request.urlopen(req, timeout=30) as r: raw = r.read() return json.loads(raw) if raw else {} def main(): token = SECRETS.get("token", "") seen = set() ips = [] for x in str(INPUTS.get("ips", "")).split(","): x = x.strip() if x and x not in seen: seen.add(x) ips.append(x) if not ips: print(json.dumps({"error": "ips is required"})) sys.exit(1) url = BASE + "/batch/lite?" + urllib.parse.urlencode({"token": token}) result = {} for i in range(0, len(ips), CHUNK): result.update(post_chunk(url, ips[i:i + CHUNK])) print(json.dumps(result)) try: main() except urllib.error.HTTPError as e: print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) sys.exit(1) except Exception as e: print(json.dumps({"error": str(e)})) sys.exit(1)