File size: 4,390 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
-- MA pilot verification queries (DuckDB).
--
-- Run from the repo root:
--     duckdb -c ".read scripts/datasources/ma_pilot/verify.sql"
-- Or interactively:
--     duckdb
--     .read scripts/datasources/ma_pilot/verify.sql
--
-- Reads the local parquet files written by
-- ``scripts/datasources/ma_pilot/scrape_ma_jurisdictions.py``.

.print
.print === MA pilot verification ===
.print

-- 1. Row counts by jurisdiction.
.print Contacts per jurisdiction:
SELECT
    jurisdiction_id,
    jurisdiction_name,
    jurisdiction_type,
    COUNT(*)                                    AS contact_rows,
    COUNT(DISTINCT source_page_url)             AS distinct_pages,
    SUM(CASE WHEN is_mayor THEN 1 ELSE 0 END)   AS mayor_rows,
    SUM(CASE WHEN email IS NOT NULL THEN 1 ELSE 0 END) AS rows_with_email,
    SUM(CASE WHEN phone IS NOT NULL THEN 1 ELSE 0 END) AS rows_with_phone
FROM 'data/bronze/contacts_scraped/ma_pilot_contacts.parquet'
GROUP BY 1, 2, 3
ORDER BY jurisdiction_name;

.print
.print Jurisdictions with NO contact rows (red flag):
SELECT
    j.name                  AS jurisdiction_name,
    j.jurisdiction_id,
    j.homepage
FROM (VALUES
    ('Boston',          'boston_2507000', 'https://www.boston.gov/'),
    ('Cambridge',       'cambridge_2511000', 'https://www.cambridgema.gov/'),
    ('Worcester',       'worcester_2582000', 'https://www.worcesterma.gov/'),
    ('Springfield',     'springfield_2567000', 'https://www.springfield-ma.gov/'),
    ('Lowell',          'lowell_2537000', 'https://www.lowellma.gov/'),
    ('Somerville',      'somerville_2562535', 'https://www.somervillema.gov/'),
    ('Newton',          'newton_2545560', 'https://www.newtonma.gov/'),
    ('Quincy',          'quincy_2555745', 'https://www.quincyma.gov/'),
    ('Plymouth County', 'plymouth_25023',         'https://www.plymouthcountyma.gov/'),
    ('Norfolk County',  'norfolk_25021',         'http://www.norfolkcounty.org/')
) AS j(name, jurisdiction_id, homepage)
LEFT JOIN (
    SELECT DISTINCT jurisdiction_id
    FROM 'data/bronze/contacts_scraped/ma_pilot_contacts.parquet'
) c USING (jurisdiction_id)
WHERE c.jurisdiction_id IS NULL;

.print
.print Mayor-flagged contacts (these came from mayor-style URLs or had 'Mayor' in the title):
SELECT
    jurisdiction_name,
    person_name,
    title_or_role,
    email,
    phone,
    source_page_url
FROM 'data/bronze/contacts_scraped/ma_pilot_contacts.parquet'
WHERE is_mayor = TRUE
ORDER BY jurisdiction_name, person_name;

.print
.print Contacts WITHOUT person_name (cleanup candidates):
SELECT
    jurisdiction_name,
    source_page_url,
    title_or_role,
    email,
    phone
FROM 'data/bronze/contacts_scraped/ma_pilot_contacts.parquet'
WHERE person_name IS NULL OR TRIM(person_name) = ''
LIMIT 25;

.print
.print Duplicate emails across jurisdictions (often shared portals or generic mailboxes):
SELECT
    email,
    COUNT(DISTINCT jurisdiction_id) AS jurisdiction_count,
    LIST(DISTINCT jurisdiction_name) AS jurisdictions
FROM 'data/bronze/contacts_scraped/ma_pilot_contacts.parquet'
WHERE email IS NOT NULL
GROUP BY email
HAVING COUNT(DISTINCT jurisdiction_id) > 1
ORDER BY jurisdiction_count DESC;

.print
.print === YouTube channels ===
.print

-- The YouTube file may not exist if --skip-youtube was used. Guard with a check.
.print YouTube channels by jurisdiction:
SELECT
    jurisdiction_name,
    channel_url,
    channel_title,
    subscriber_count,
    video_count,
    discovery_method,
    confidence
FROM 'data/bronze/youtube_channels/ma_pilot_youtube_channels.parquet'
ORDER BY jurisdiction_name, confidence DESC;

.print
.print Jurisdictions with NO YouTube channel discovered:
SELECT
    j.name AS jurisdiction_name,
    j.jurisdiction_id
FROM (VALUES
    ('Boston',          'boston_2507000'),
    ('Cambridge',       'cambridge_2511000'),
    ('Worcester',       'worcester_2582000'),
    ('Springfield',     'springfield_2567000'),
    ('Lowell',          'lowell_2537000'),
    ('Somerville',      'somerville_2562535'),
    ('Newton',          'newton_2545560'),
    ('Quincy',          'quincy_2555745'),
    ('Plymouth County', 'plymouth_25023'),
    ('Norfolk County',  'norfolk_25021')
) AS j(name, jurisdiction_id)
LEFT JOIN (
    SELECT DISTINCT jurisdiction_id
    FROM 'data/bronze/youtube_channels/ma_pilot_youtube_channels.parquet'
) y USING (jurisdiction_id)
WHERE y.jurisdiction_id IS NULL;