|
| 1 | +import fetch from 'node-fetch'; |
| 2 | +import { writeFileSync } from 'fs'; |
| 3 | +import { fileURLToPath } from 'url'; |
| 4 | +import { dirname, join } from 'path'; |
| 5 | +import 'dotenv/config'; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = dirname(__filename); |
| 9 | + |
| 10 | +// Verify required environment variables are present |
| 11 | +if (!process.env.SUPABASE_URL || !process.env.SUPABASE_SERVICE_ROLE_KEY) { |
| 12 | + console.error('Error: Required environment variables SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY must be set in .env file'); |
| 13 | + process.exit(1); |
| 14 | +} |
| 15 | + |
| 16 | +const SUPABASE_URL = process.env.SUPABASE_URL; |
| 17 | +const SUPABASE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY; |
| 18 | + |
| 19 | +async function fetchAllBallots() { |
| 20 | + console.log('Fetching all ballot records...'); |
| 21 | + |
| 22 | + const response = await fetch(`${SUPABASE_URL}/rest/v1/phila_ballots?select=id_number,name,ward,division,birth_year,zip,ballot_status_reason,added`, { |
| 23 | + headers: { |
| 24 | + 'apikey': SUPABASE_KEY, |
| 25 | + 'Authorization': `Bearer ${SUPABASE_KEY}` |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + if (!response.ok) { |
| 30 | + throw new Error(`Failed to fetch ballots: ${response.statusText}`); |
| 31 | + } |
| 32 | + |
| 33 | + const data = await response.json(); |
| 34 | + console.log(`Fetched ${data.length} ballot records`); |
| 35 | + return data; |
| 36 | +} |
| 37 | + |
| 38 | +async function fetchDivisionStats() { |
| 39 | + console.log('Fetching division statistics...'); |
| 40 | + |
| 41 | + const response = await fetch(`${SUPABASE_URL}/rest/v1/phila_ballots_stats?select=division,count`, { |
| 42 | + headers: { |
| 43 | + 'apikey': SUPABASE_KEY, |
| 44 | + 'Authorization': `Bearer ${SUPABASE_KEY}` |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + if (!response.ok) { |
| 49 | + throw new Error(`Failed to fetch stats: ${response.statusText}`); |
| 50 | + } |
| 51 | + |
| 52 | + const data = await response.json(); |
| 53 | + console.log(`Fetched stats for ${data.length} divisions`); |
| 54 | + return data; |
| 55 | +} |
| 56 | + |
| 57 | +async function main() { |
| 58 | + try { |
| 59 | + // Fetch data |
| 60 | + const ballots = await fetchAllBallots(); |
| 61 | + const stats = await fetchDivisionStats(); |
| 62 | + |
| 63 | + // Define output paths |
| 64 | + const publicDataDir = join(__dirname, '..', 'public', 'data'); |
| 65 | + const ballotsPath = join(publicDataDir, 'phila_ballots.json'); |
| 66 | + const statsPath = join(publicDataDir, 'division_stats.json'); |
| 67 | + |
| 68 | + // Save ballots data |
| 69 | + console.log(`Saving ballots to ${ballotsPath}...`); |
| 70 | + writeFileSync(ballotsPath, JSON.stringify(ballots, null, 2)); |
| 71 | + console.log(`✓ Saved ${ballots.length} ballot records`); |
| 72 | + |
| 73 | + // Save stats data |
| 74 | + console.log(`Saving stats to ${statsPath}...`); |
| 75 | + writeFileSync(statsPath, JSON.stringify(stats, null, 2)); |
| 76 | + console.log(`✓ Saved stats for ${stats.length} divisions`); |
| 77 | + |
| 78 | + // Print summary |
| 79 | + console.log('\n=== Export Summary ==='); |
| 80 | + console.log(`Total ballot records: ${ballots.length}`); |
| 81 | + console.log(`Total divisions: ${stats.length}`); |
| 82 | + console.log(`Ballots file size: ${(JSON.stringify(ballots).length / 1024).toFixed(2)} KB`); |
| 83 | + console.log(`Stats file size: ${(JSON.stringify(stats).length / 1024).toFixed(2)} KB`); |
| 84 | + console.log('\nExport completed successfully!'); |
| 85 | + |
| 86 | + } catch (error) { |
| 87 | + console.error('Fatal error:', error.message); |
| 88 | + process.exit(1); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +main(); |
0 commit comments