File size: 12,408 Bytes
d15f07c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
"""
Extract election calendar rows, candidacies, and ballot measures from jurisdiction HTML.

Best-effort heuristics for city/county sites — not a substitute for official election files,
but suitable for bronze + c1 promotion when no external API is used.
"""

from __future__ import annotations

import json
import re
from datetime import date, datetime
from typing import Any
from urllib.parse import urljoin, urlparse

from bs4 import BeautifulSoup

ELECTION_LINK_RE = re.compile(
    r"\b(?:election|elections|ballot|ballots|candidate|candidates|voting|vote|voter|"
    r"poll(?:ing)?|sample[- ]ballot|election[- ]results?|runoff|primary|general)\b",
    re.I,
)

ELECTION_HEADING_RE = re.compile(
    r"\b(?:election|ballot|candidate|voting|vote|primary|general|runoff|special)\b",
    re.I,
)

DATE_PATTERNS: tuple[re.Pattern[str], ...] = (
    re.compile(
        r"\b(\d{4})-(\d{1,2})-(\d{1,2})\b",
    ),
    re.compile(
        r"\b(\d{1,2})[/.-](\d{1,2})[/.-](\d{2,4})\b",
    ),
    re.compile(
        r"\b(January|February|March|April|May|June|July|August|September|October|November|December)"
        r"\s+(\d{1,2})(?:st|nd|rd|th)?,?\s+(\d{4})\b",
        re.I,
    ),
)

_MONTH = {
    "january": 1, "february": 2, "march": 3, "april": 4, "may": 5, "june": 6,
    "july": 7, "august": 8, "september": 9, "october": 10, "november": 11, "december": 12,
}

ELECTION_PROBE_PATHS: tuple[str, ...] = (
    "/elections",
    "/elections/",
    "/election-information",
    "/election-info",
    "/vote",
    "/voting",
    "/voter-information",
    "/ballot",
    "/candidates",
    "/election-results",
    "/government/elections",
    "/departments/city-clerk/elections",
    "/departments/city-clerk/election-information",
    "/county-clerk/elections",
)


def _same_site(url: str, base: str) -> bool:
    try:
        return urlparse(url).netloc.lower() == urlparse(base).netloc.lower()
    except Exception:
        return False


def discover_election_page_urls(homepage_url: str, html: str, *, max_urls: int = 12) -> list[str]:
    """Collect same-site links that look election-related."""
    if not homepage_url or not html:
        return []
    out: list[str] = []
    seen: set[str] = set()
    soup = BeautifulSoup(html, "html.parser")
    for a in soup.find_all("a", href=True):
        href = (a.get("href") or "").strip()
        if not href or href.startswith(("#", "mailto:", "tel:")):
            continue
        text = a.get_text(" ", strip=True)
        if not ELECTION_LINK_RE.search(href) and not ELECTION_LINK_RE.search(text):
            continue
        abs_url = urljoin(homepage_url, href)
        if not _same_site(abs_url, homepage_url):
            continue
        norm = abs_url.split("#", 1)[0].rstrip("/")
        if norm in seen:
            continue
        seen.add(norm)
        out.append(abs_url)
        if len(out) >= max_urls:
            break
    return out


def candidate_election_urls(homepage_url: str) -> list[str]:
    """Build ordered ``/elections``-style URLs relative to the jurisdiction homepage."""
    if not homepage_url:
        return []
    out: list[str] = []
    seen: set[str] = set()
    for path in ELECTION_PROBE_PATHS:
        url = urljoin(homepage_url, path)
        if url in seen:
            continue
        seen.add(url)
        out.append(url)
    return out


def probe_election_path_urls(homepage_url: str, session) -> list[str]:
    """HEAD/GET common ``/elections``-style paths on the site host."""
    from scrapers.discovery.mayor_url_discovery import probe_urls

    return probe_urls(candidate_election_urls(homepage_url), session=session)


def _parse_date_from_text(text: str) -> date | None:
    blob = (text or "").strip()
    if not blob:
        return None
    m = DATE_PATTERNS[0].search(blob)
    if m:
        try:
            return date(int(m.group(1)), int(m.group(2)), int(m.group(3)))
        except ValueError:
            pass
    m = DATE_PATTERNS[1].search(blob)
    if m:
        mo, da, yr = int(m.group(1)), int(m.group(2)), int(m.group(3))
        if yr < 100:
            yr += 2000
        try:
            return date(yr, mo, da)
        except ValueError:
            pass
    m = DATE_PATTERNS[2].search(blob)
    if m:
        mo = _MONTH.get(m.group(1).lower())
        if mo:
            try:
                return date(int(m.group(3)), mo, int(m.group(2)))
            except ValueError:
                pass
    return None


def _infer_election_type(text: str) -> str:
    low = (text or "").lower()
    for label in ("primary", "general", "runoff", "special", "municipal"):
        if label in low:
            return label
    return "unknown"


def _json_ld_events(soup: BeautifulSoup, page_url: str) -> list[dict[str, Any]]:
    rows: list[dict[str, Any]] = []
    for script in soup.find_all("script", attrs={"type": re.compile(r"ld\+json", re.I)}):
        raw = (script.string or script.get_text() or "").strip()
        if not raw:
            continue
        try:
            data = json.loads(raw)
        except (json.JSONDecodeError, TypeError, ValueError):
            continue

        def walk(node: Any) -> None:
            if isinstance(node, list):
                for item in node:
                    walk(item)
            elif isinstance(node, dict):
                t = node.get("@type") or ""
                types = t if isinstance(t, list) else [t]
                if any(str(x).lower() == "event" for x in types):
                    name = (node.get("name") or "").strip()
                    start = node.get("startDate") or node.get("startdate") or ""
                    if name and ELECTION_HEADING_RE.search(name):
                        rows.append({
                            "name": name,
                            "election_date": _parse_date_from_text(str(start)) or _parse_date_from_text(name),
                            "election_type": _infer_election_type(name),
                            "source_url": page_url,
                            "extraction_method": "json_ld_event",
                            "raw_snippet": name,
                        })
                for v in node.values():
                    walk(v)

        walk(data)
    return rows


def _heading_blocks(soup: BeautifulSoup, page_url: str) -> list[dict[str, Any]]:
    rows: list[dict[str, Any]] = []
    for tag in soup.find_all(re.compile(r"^h[1-3]$", re.I)):
        title = tag.get_text(" ", strip=True)
        if not title or not ELECTION_HEADING_RE.search(title):
            continue
        sibling_text = []
        for sib in tag.find_next_siblings(limit=4):
            if getattr(sib, "name", None) and re.match(r"^h[1-3]$", sib.name, re.I):
                break
            sibling_text.append(sib.get_text(" ", strip=True))
        block = " ".join([title, *sibling_text])
        rows.append({
            "name": title[:500],
            "election_date": _parse_date_from_text(block),
            "election_type": _infer_election_type(title),
            "source_url": page_url,
            "extraction_method": "heading_block",
            "raw_snippet": block[:2000],
        })
    return rows


def _table_candidacies(soup: BeautifulSoup, page_url: str) -> list[dict[str, Any]]:
    rows: list[dict[str, Any]] = []
    for table in soup.find_all("table"):
        headers = [
            th.get_text(" ", strip=True).lower()
            for th in table.find_all("th")
        ]
        if not headers:
            first = table.find("tr")
            if first:
                headers = [c.get_text(" ", strip=True).lower() for c in first.find_all(["td", "th"])]
        header_blob = " ".join(headers)
        if not re.search(r"candidate|name|office|position|party|seat", header_blob, re.I):
            continue
        name_idx = party_idx = office_idx = -1
        for i, h in enumerate(headers):
            if "name" in h or h == "candidate":
                name_idx = i
            if "party" in h:
                party_idx = i
            if "office" in h or "position" in h or "seat" in h:
                office_idx = i
        for tr in table.find_all("tr"):
            cells = [td.get_text(" ", strip=True) for td in tr.find_all(["td", "th"])]
            if len(cells) < 2:
                continue
            if name_idx >= 0 and name_idx < len(cells):
                person = cells[name_idx]
            else:
                person = cells[0]
            if not person or len(person) < 2 or person.lower() in headers:
                continue
            if not re.search(r"[A-Za-z]{2,}", person):
                continue
            party = cells[party_idx] if 0 <= party_idx < len(cells) else None
            office = cells[office_idx] if 0 <= office_idx < len(cells) else None
            rows.append({
                "person_name": person[:300],
                "party": (party or "")[:120] or None,
                "office": (office or "")[:300] or None,
                "status": "candidate",
                "source_url": page_url,
                "extraction_method": "html_table",
            })
    return rows


def _list_ballot_measures(soup: BeautifulSoup, page_url: str) -> list[dict[str, Any]]:
    rows: list[dict[str, Any]] = []
    for li in soup.find_all("li"):
        text = li.get_text(" ", strip=True)
        if not text or len(text) < 12:
            continue
        if not re.search(r"\b(?:proposition|measure|question|referendum|amendment)\b", text, re.I):
            continue
        if not re.search(r"\b\d+\b", text) and "?" not in text:
            continue
        rows.append({
            "title": text[:500],
            "summary": text[:2000],
            "classification": "referendum",
            "source_url": page_url,
            "extraction_method": "list_item",
        })
    return rows


def extract_election_bundle_from_html(html: str, page_url: str) -> dict[str, Any]:
    """
    Parse one HTML page into elections, candidacies, and ballot-measure candidates.
    """
    soup = BeautifulSoup(html or "", "html.parser")
    elections: list[dict[str, Any]] = []
    elections.extend(_json_ld_events(soup, page_url))
    elections.extend(_heading_blocks(soup, page_url))

    # Page title as weak election row when election-themed
    title = (soup.title.string or "").strip() if soup.title else ""
    if title and ELECTION_HEADING_RE.search(title):
        elections.append({
            "name": title[:500],
            "election_date": _parse_date_from_text(title),
            "election_type": _infer_election_type(title),
            "source_url": page_url,
            "extraction_method": "document_title",
            "raw_snippet": title,
        })

    candidacies = _table_candidacies(soup, page_url)
    measures = _list_ballot_measures(soup, page_url)

    return {
        "page_url": page_url,
        "elections": elections,
        "candidacies": candidacies,
        "ballot_measures": measures,
    }


def merge_election_bundles(bundles: list[dict[str, Any]]) -> dict[str, Any]:
    """Deduplicate merged bundles from multiple pages."""
    elections: list[dict[str, Any]] = []
    candidacies: list[dict[str, Any]] = []
    measures: list[dict[str, Any]] = []
    seen_e: set[tuple[str, str]] = set()
    seen_c: set[tuple[str, str, str]] = set()
    seen_m: set[str] = set()

    for bundle in bundles:
        for e in bundle.get("elections") or []:
            key = ((e.get("name") or "").lower(), str(e.get("election_date") or ""))
            if key in seen_e:
                continue
            seen_e.add(key)
            elections.append(e)
        for c in bundle.get("candidacies") or []:
            key = (
                (c.get("person_name") or "").lower(),
                (c.get("office") or "").lower(),
                c.get("source_url") or "",
            )
            if key in seen_c:
                continue
            seen_c.add(key)
            candidacies.append(c)
        for m in bundle.get("ballot_measures") or []:
            key = (m.get("title") or "").lower()
            if key in seen_m:
                continue
            seen_m.add(key)
            measures.append(m)

    return {
        "elections": elections,
        "candidacies": candidacies,
        "ballot_measures": measures,
        "pages_scraped": [b.get("page_url") for b in bundles if b.get("page_url")],
    }