#!/usr/bin/env python3 """Compare a business's own supplied listing details; no web requests.""" import json import re import sys from urllib.parse import urlsplit FIELDS = ("name", "address", "phone", "website") def normalize(field, value): text = " ".join(str(value or "").split()) if field == "phone": digits = re.sub(r"\D", "", text) return digits[1:] if len(digits) == 11 and digits.startswith("1") else digits if field == "website": host = (urlsplit(text).hostname or "").casefold() return host.removeprefix("www.") return text.casefold() def audit(records): if not isinstance(records, list) or not records: raise ValueError("Provide a non-empty JSON list of listing records.") if any(not isinstance(row, dict) for row in records): raise ValueError("Each listing record must be a JSON object.") # The first record is the reference you have checked yourself. reference = records[0] issues = [] for row in records: source = str(row.get("source") or "Unnamed source") for field in FIELDS: value = normalize(field, row.get(field)) expected = normalize(field, reference.get(field)) if not value: issues.append({"source": source, "field": field, "issue": "Missing or unusable value"}) elif expected and value != expected: issues.append({"source": source, "field": field, "issue": "Differs from checked reference"}) return issues if __name__ == "__main__": if len(sys.argv) != 2: raise SystemExit("Usage: python3 listing_audit.py business-listings.json") with open(sys.argv[1], encoding="utf-8") as file: records = json.load(file) print(json.dumps(audit(records), indent=2, ensure_ascii=False))