import requests, json, time, csv

API_KEY = "apify_api_P3r95DNB3QjDySjpUWrFPQzVKvKNOO09xnav"
ACTOR_ID = "compass~crawler-google-places"

# Correcting payload based on API error and previous success
payload = {
    "searchStringsArray": ["janitorial services Denver CO"],
    "maxCrawledPlaces": 100,
    "language": "en"
}

print("Starting Apify scrape for Denver, CO (retry)...")
print(f"Payload: {json.dumps(payload, indent=2)}")

run_resp = requests.post(
    f"https://api.apify.com/v2/acts/{ACTOR_ID}/runs?token={API_KEY}",
    json=payload
).json()

print("Run response:", json.dumps(run_resp, indent=2))

if "data" not in run_resp or "id" not in run_resp["data"]:
    print("Error starting run: Missing run ID.")
    exit(1)

run_id = run_resp["data"]["id"]
dataset_id = run_resp["data"]["defaultDatasetId"]
print(f"Run ID: {run_id}")

# Wait for completion (with more robust error checking)
status = "STARTING"
for i in range(60):  # max 10 minutes
    time.sleep(10)
    try:
        r = requests.get(f"https://api.apify.com/v2/actor-runs/{run_id}?token={API_KEY}").json()
        status = r["data"]["status"]
        print(f"Status: {status}")
        if status in ["SUCCEEDED", "FAILED", "ABORTED"]:
            break
    except Exception as e:
        print(f"Error checking status: {e}")
        status = "FAILED"
        break

if status == "SUCCEEDED":
    link = f"https://api.apify.com/v2/datasets/{dataset_id}/items?token={API_KEY}&format=json&clean=true"
    items = requests.get(link).json()
    print(f"Got {len(items)} places")
    
    with open("/data/openclaw/leads/raw/denver-2026-03-12.csv", "w", newline="") as f:
        w = csv.DictWriter(f, fieldnames=["business_name", "phone", "website", "address", "rating", "review_count", "category", "scrape_date"])
        w.writeheader()
        for item in items:
            w.writerow({
                "business_name": item.get("title", ""),
                "phone": item.get("phone", ""),
                "website": item.get("website", ""),
                "address": item.get("address", ""),
                "rating": item.get("totalScore", ""),
                "review_count": item.get("reviewsCount", ""),
                "category": item.get("categoryName", ""),
                "scrape_date": "2026-03-12"
            })
    print("Saved!")
else:
    print(f"Run failed with status: {status}")
