# leads.py
import csv
import os
import time
import requests
API = "https://api.spidra.io/api"
HEADERS = {"Authorization": f"Bearer {os.environ['SPIDRA_API_KEY']}"}
inputs = {"query": "dentist", "areas": ["Lekki, Lagos"], "findContacts": True}
# 1. Check the cost
estimate = requests.post(f"{API}/templates/google-maps-leads/estimate", json={"inputs": inputs}).json()
print("Estimate:", estimate["credits"], "credits +", estimate["note"])
# 2. Start the run
run = requests.post(f"{API}/templates/google-maps-leads/runs", headers=HEADERS, json={"inputs": inputs}).json()["run"]
# 3. Wait for it, including the website step
while run["status"] == "running":
time.sleep(5)
run = requests.get(f"{API}/template-runs/{run['runId']}", headers=HEADERS, params={"rows": "false"}).json()["run"]
run = requests.get(f"{API}/template-runs/{run['runId']}", headers=HEADERS).json()["run"]
print(run["status"], run["rowCount"], "places,", run["creditsUsed"], "credits")
# 4. Write the leads
columns = ["title", "phone", "website", "emails", "instagram", "facebook", "linkedin", "rating", "address"]
with open("leads.csv", "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=columns, extrasaction="ignore")
writer.writeheader()
for row in run["rows"]:
socials = row.get("socials") or {}
writer.writerow({
**row,
"emails": ", ".join(row.get("emails") or []),
"instagram": socials.get("instagram"),
"facebook": socials.get("facebook"),
"linkedin": socials.get("linkedin"),
})
with_email = sum(1 for r in run["rows"] if r.get("emails"))
print(f"{with_email} of {len(run['rows'])} places have an email")