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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# Civic Data API Integration Status

Status of major civic data APIs in the Open Navigator platform.

## βœ… Fully Integrated APIs

### 1. Open States API βœ…
**Status:** INTEGRATED  
**File:** `discovery/openstates_sources.py`  
**API Docs:** https://openstates.org/api/  
**What it provides:**
- 50+ state legislatures
- State-level officials
- Legislative bills and votes
- Committee information
- Video sources (YouTube, Vimeo, Granicus)

**Usage:**
```bash
# Set API key in .env
OPENSTATES_API_KEY=your-key-here

# Run ingestion
python -m discovery.openstates_sources
```

**API Key:** Free tier - 50,000 requests/month  
**Sign up:** https://openstates.org/accounts/signup/

---

### 2. NCES District Search βœ…
**Status:** INTEGRATED  
**File:** `discovery/nces_ingestion.py`  
**Data Source:** https://nces.ed.gov/ccd/  
**What it provides:**
- 13,000+ school districts nationwide
- School district boundaries
- Contact information
- Enrollment and demographic data
- Physical addresses

**Usage:**
```bash
# Run ingestion (downloads CSV from NCES)
python -m discovery.nces_ingestion
```

**API Key:** Not required (public CSV downloads)

---

### 3. Wikidata βœ… **NEW!**
**Status:** INTEGRATED  
**File:** `discovery/wikidata_integration.py`  
**API Docs:** https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service  
**What it provides:**
- Structured knowledge base (powers Wikipedia infoboxes)
- Best for connecting people β†’ organizations β†’ locations
- SPARQL queries for complex relationships
- Millions of interconnected entities

**Why it's amazing:**
- βœ… **Completely FREE** - no API key required
- βœ… **Highly interconnected** - find person β†’ see all linked organizations
- βœ… **Structured data** - triples (subject-predicate-object)
- βœ… **Real Wikipedia data** - millions of entities
- βœ… **Perfect for relationships** - "All school board members in Alabama"

**Usage:**
```python
from discovery.wikidata_integration import WikidataQuery

wikidata = WikidataQuery()

# Find school board members
members = await wikidata.find_school_board_members(state="Alabama")

# Find cities in a county
cities = await wikidata.find_cities_in_county("Tuscaloosa County", "Alabama")

# Find organizations a person is affiliated with
orgs = await wikidata.find_person_organizations("Walt Maddox")
```

**API Key:** Not required (completely free)

---

### 4. DBpedia βœ… **NEW!**
**Status:** INTEGRATED  
**File:** `discovery/dbpedia_integration.py`  
**API Docs:** http://lookup.dbpedia.org/api/doc/  
**What it provides:**
- Structured data from Wikipedia infoboxes
- Perfect for autocomplete/type-ahead search
- Every Wikipedia page as a structured "resource"
- Mayor, population, school district info

**Why it's perfect for search:**
- βœ… **Completely FREE** - no API key required
- βœ… **Designed for autocomplete** - Lookup API is type-ahead optimized
- βœ… **Instant context** - Get Mayor, population for "Tuscaloosa"
- βœ… **Rich data** - Structured triples from Wikipedia
- βœ… **Fast** - Optimized for search box suggestions

**Usage:**
```python
from discovery.dbpedia_integration import DBpediaLookup

dbpedia = DBpediaLookup()

# Autocomplete search
results = await dbpedia.search("Tuscaloosa", max_results=10)

# Get detailed info
info = await dbpedia.get_resource_info("Tuscaloosa,_Alabama")

# Search by type
cities = await dbpedia.find_cities(state="Alabama")
people = await dbpedia.find_people("Alabama mayor")
```

**API Key:** Not required (completely free)

---

## οΏ½ Reference Implementations (Paid Services)

These integrations are provided as reference code but require paid API access.

### Ballotpedia API v3.0 πŸ’°
**Status:** REFERENCE ONLY - Paid service  
**File:** `discovery/ballotpedia_integration.py` (reference implementation)  
**Website:** https://ballotpedia.org  
**API Docs:** https://ballotpedia.org/API_documentation  
**API Announcement:** https://ballotpedia.org/Just_launched:_Ballotpedia's_API_Version_3.0  
**Pricing:** Contact Ballotpedia for pricing (not free)  

**What it provides:**
- Elected officials (federal, state, local)
- Ballot measures and initiatives
- Election results
- Candidate information

**Current Implementation:**
- βœ… Official API v3.0 client (BallotpediaAPI class)
- βœ… Web scraping fallback (BallotpediaDiscovery class)
- βœ… Leader search by name
- βœ… City officials discovery
- βœ… Ballot measures by state/year
- βœ… Rate-limited web scraping (2s delays)

**API Key:** Contact Ballotpedia for access  
**Get access:** https://ballotpedia.org/API_documentation

**Usage (Official API - RECOMMENDED):**
```python
from discovery.ballotpedia_integration import BallotpediaAPI

# Set BALLOTPEDIA_API_KEY in .env
api = BallotpediaAPI()

# Get officials via official API
officials = await api.get_officials("Tuscaloosa", state="Alabama")

# Get ballot measures via official API
measures = await api.get_ballot_measures("Alabama", year=2024)
```

**Usage (Web Scraping Fallback):**
```python
from discovery.ballotpedia_integration import BallotpediaDiscovery

discovery = BallotpediaDiscovery()

# Search for a leader (web scraping)
leader = await discovery.search_leader("Walt Maddox", "Alabama")

# Get city officials (web scraping)
officials = await discovery.get_city_officials("Tuscaloosa", "Alabama")

# Get ballot measures (web scraping)
measures = await discovery.get_ballot_measures("Alabama", year=2024)
```

**Notes:**
- ⚠️ **Paid Service** - Ballotpedia API requires payment
- Not recommended for free/open-source projects
- Code provided as reference for those with API access
- Consider alternatives: Google Civic API (free) for officials, Open States (free) for state data
- Web scraping may violate terms of service - use at own risk

**Alternative Free APIs:**
- Google Civic Information API - Free, 25k requests/day
- Open States API - Free, 50k requests/month
- NCES - Free public data for school boards

---

## ❌ Not Yet Integrated

### 3. Google Civic Information API ❌
**Status:** NOT INTEGRATED  
**API Docs:** https://developers.google.com/civic-information  
**What it would provide:**
- Address-to-representative mapping
- Elected officials by address
- Election data
- Polling locations
- Voter information

**Why integrate:**
- Best API for "who represents this address?"
- Official election information
- Comprehensive official contact info
- Free tier: 25,000 requests/day

**API Key Required:** Yes (Google Cloud Console)  
**Free Tier:** 25,000 requests/day  
**Sign up:** https://console.cloud.google.com/

**Next Steps:**
1. Create `discovery/google_civic_integration.py`
2. Add API key to `.env`: `GOOGLE_CIVIC_API_KEY=your-key`
3. Implement endpoints:
   - `representativeInfoByAddress(address)`
   - `elections()`
   - `voterInfoQuery(address)`

---

### Cicero API πŸ’° (Reference Only)
**Status:** NOT INTEGRATED - Paid service  
**API Docs:** https://cicerodata.com  
**What it would provide:**
- Local district boundaries (very accurate)
- Contact info for local officials
- Non-legislative officials (school boards, water districts, etc.)
- Real-time updates

**Why NOT integrating:**
- ⚠️ **Paid Service** - Enterprise/professional pricing
- Not suitable for free/open-source projects
- Free alternatives available (Google Civic, Open States)

**Free Alternatives:**
- Google Civic Information API - Address-to-representative mapping
- Open States API - State-level officials and districts
- Census TIGER/Line - Free boundary shapefiles

---

## πŸ“Š Integration Summary

| API | Status | Free? | File | Key Required? |
|-----|--------|-------|------|---------------|
| **Wikidata** | βœ… Integrated | Yes | `wikidata_integration.py` | No |
| **DBpedia** | βœ… Integrated | Yes | `dbpedia_integration.py` | No |
| **Open States** | βœ… Integrated | Yes | `openstates_sources.py` | Yes (free) |
| **NCES** | βœ… Integrated | Yes | `nces_ingestion.py` | No |
| **Google Civic** | ❌ Not Yet | Yes | `google_civic_integration.py` | Yes (free) |

**Reference Only (Paid Services):**
- **Ballotpedia API v3.0** - Paid service, code available for reference in `ballotpedia_integration.py`
- **Cicero API** - Enterprise-grade district boundaries (paid)

---

## 🎯 The "Free Stack" for School Boards & Civic Data

Since school board data is the **hardest to find for free**, here's how to combine FREE sources:

| Source | Best Use Case | API Type | File |
|--------|---------------|----------|------|
| **Wikidata** | Relationships (People β†’ Boards) | SPARQL | `wikidata_integration.py` |
| **Google Civic** | Address β†’ Specific Board | REST | `google_civic_integration.py` |
| **NCES** | Official District IDs & Boundaries | CSV | `nces_ingestion.py` |
| **DBpedia** | Autocomplete & Context | Lookup | `dbpedia_integration.py` |
| **Open States** | State-Level Officials & Bills | REST | `openstates_sources.py` |

### How They Work Together:

**1. User enters address in search box:**
- **DBpedia Lookup** β†’ Autocomplete suggestions as they type
- **Google Civic API** β†’ Maps address to exact school board district
- **NCES Data** β†’ Official district ID, boundaries, demographics

**2. User wants to see school board members:**
- **Wikidata SPARQL** β†’ "Find all members of [School Board Name]"
- **Wikidata** β†’ Links each person to their organizations
- **DBpedia** β†’ Rich context from Wikipedia (photos, bio, etc.)

**3. User wants state-level info:**
- **Open States API** β†’ State legislators, bills, committees
- **Wikidata** β†’ State government structure, officials
- **DBpedia** β†’ State context and background

**Example Query Flow:**
```
User types: "Tuscaloosa schools"
  ↓
DBpedia: Autocomplete β†’ "Tuscaloosa City Schools"
  ↓
User enters address: "123 Main St, Tuscaloosa, AL"
  ↓
Google Civic: β†’ Maps to "Tuscaloosa City School District"
  ↓
NCES: β†’ Gets official district ID, enrollment, demographics
  ↓
Wikidata: β†’ Finds all school board members
  ↓
DBpedia: β†’ Gets rich Wikipedia context for each member
```

---

## 🎯 Recommended Integration Priority

### βœ… Already Integrated (Free + High Value)
1. βœ… **Wikidata** - BEST for relationships (people β†’ organizations) - **FREE, no key**
2. βœ… **DBpedia** - BEST for autocomplete/search - **FREE, no key**
3. βœ… **Open States** - State legislature data - **FREE, key required**
4. βœ… **NCES** - School district data - **FREE, no key**

### πŸ”΄ High Priority (Not Yet Integrated)
5. πŸ”΄ **Google Civic API** - Address β†’ officials mapping - **FREE, key required**
   - Code ready in `google_civic_integration.py`
   - Just need API key from Google Cloud Console
   - 25,000 requests/day free tier

### ❌ Not Recommended (Paid Services)
- ❌ **Ballotpedia API** - Paid service, use free alternatives
- ❌ **Cicero API** - Enterprise pricing, use Google Civic + Wikidata instead

---

## πŸ† Why Wikidata + DBpedia are Game-Changers

### **Wikidata = The Relationship Database**
- Find **all school board members** in a state
- See **every organization** a person belongs to
- Link **people β†’ positions β†’ locations**
- Example: "Walt Maddox" β†’ Mayor β†’ Tuscaloosa β†’ School Board connections

### **DBpedia = The Autocomplete Engine**
- **Perfect for search boxes** - Lookup API designed for type-ahead
- Type "Tusc" β†’ Get instant suggestions
- Every Wikipedia page = structured data
- Get Mayor, population, district info instantly

### **Together They're Unbeatable:**
1. **DBpedia** for autocomplete (fast, optimized for search)
2. **Wikidata** for relationships (deep, interconnected data)
3. **Google Civic** for address mapping (precise, official)
4. **NCES** for official IDs (authoritative, complete)
5. **Open States** for state-level (comprehensive, up-to-date)

**All FREE. No paid services needed!** πŸŽ‰

---

## πŸš€ Quick Start: Adding Google Civic API

The highest-value missing integration is **Google Civic Information API**.

### Step 1: Get API Key
```bash
# Visit Google Cloud Console
open https://console.cloud.google.com/

# Create project
# Enable "Google Civic Information API"
# Create API key
```

### Step 2: Add to Environment
```bash
# Add to .env
echo "GOOGLE_CIVIC_API_KEY=your-key-here" >> .env
```

### Step 3: Create Integration (stub provided below)
See `discovery/google_civic_integration.py` (to be created)

---

## πŸ“ Example: Google Civic Integration Stub

```python
"""
Google Civic Information API Integration

Best for address-to-representative mapping.

API: https://developers.google.com/civic-information
Free Tier: 25,000 requests/day
"""
import httpx
from typing import Dict, List, Optional
from loguru import logger
from config.settings import settings


class GoogleCivicAPI:
    BASE_URL = "https://www.googleapis.com/civicinfo/v2"
    
    def __init__(self, api_key: Optional[str] = None):
        self.api_key = api_key or settings.google_civic_api_key
    
    async def get_representatives(self, address: str) -> Dict:
        """Get elected officials for an address."""
        async with httpx.AsyncClient() as client:
            response = await client.get(
                f"{self.BASE_URL}/representatives",
                params={"address": address, "key": self.api_key}
            )
            return response.json()
    
    async def get_elections(self) -> Dict:
        """Get upcoming elections."""
        async with httpx.AsyncClient() as client:
            response = await client.get(
                f"{self.BASE_URL}/elections",
                params={"key": self.api_key}
            )
            return response.json()
```

---

## πŸ” What Each API is Best For

**Open States:** State legislature bills, votes, committees  
**NCES:** School district boundaries and demographics  
**Ballotpedia:** Elected officials, ballot measures, elections  
**Google Civic:** Address β†’ representatives (best for this!)  
**Cicero:** Local district boundaries (enterprise-grade)

---

## πŸ“š Additional Resources

- **Open States Documentation:** https://docs.openstates.org/
- **NCES Common Core of Data:** https://nces.ed.gov/ccd/files.asp
- **Ballotpedia Sample Pages:** https://ballotpedia.org/Main_Page
- **Google Civic API Guide:** https://developers.google.com/civic-information/docs/using_api
- **Cicero Use Cases:** https://cicerodata.com/use-cases

---

## βœ… Next Steps

1. **Test Ballotpedia integration:**
   ```bash
   cd /home/developer/projects/open-navigator
   source .venv/bin/activate
   python discovery/ballotpedia_integration.py
   ```

2. **Create Google Civic integration:**
   - Get API key from Google Cloud Console
   - Create `discovery/google_civic_integration.py`
   - Add to API routes in `api/main.py`

3. **Evaluate Cicero:**
   - Contact cicerodata.com for pricing
   - Decide if worth the cost for enterprise deployment

4. **Update frontend:**
   - Add "Find My Representatives" feature using Google Civic
   - Show ballot measures from Ballotpedia
   - Link to school board from NCES data