-
Notifications
You must be signed in to change notification settings - Fork 0
/
importcounty.js
119 lines (105 loc) · 3.64 KB
/
importcounty.js
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
const cheerio = require('cheerio');
const { google } = require('googleapis');
const sheets = google.sheets('v4');
const parseXML = require('util').promisify(require('xml2js').parseString);
const readline = require('readline');
const request = require('request-promise');
const { readFile, writeFile } = require('fs').promises;
const getAuthClient = () => {
return readFile('./credentials.json')
.then((contents) => {
const credentials = JSON.parse(contents.toString());
const { client_id, client_secret, redirect_uris } = credentials;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
// Check if there is a previously stored token
return readFile('./token.json')
.then((token) => {
oAuth2Client.setCredentials(JSON.parse(token));
return Promise.resolve(oAuth2Client);
})
.catch((e) => {
return new Promise((resolve, reject) => {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: ['https://www.googleapis.com/auth/spreadsheets'],
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (_, token) => {
oAuth2Client.setCredentials(token);
writeFile('./token.json', JSON.stringify(token))
.then(() => resolve(oAuth2Client));
});
});
});
});
});
};
const getCountyData = () => {
return request({
url: 'https://results.enr.clarityelections.com//PA/Allegheny/106267/269519/json/ALL_district.json',
json: true
})
.then((countyData) => {
const add = (total, num) => total + num;
const nameMap = {
'MT OLIVER': 'MOUNT OLIVER',
'MT LEBANON': 'MOUNT LEBANON',
'UPPER SAINT CLAIR': 'UPPER ST. CLAIR',
"OHARA": "O'HARA"
};
return countyData.Districts.map((municipality) => {
const municipalityResults = municipality.Contests.find((c) => c.A === "-1").V;
let bidenVotes = 0;
let trumpVotes = 0;
let totalVotes = 0;
if (municipalityResults[0]) {
bidenVotes = municipalityResults[0][0];
trumpVotes = municipalityResults[0][1];
totalVotes = municipalityResults[0].reduce(add);
}
if (municipality.Name === 'ALEPPO') {
bidenVotes = 517;
trumpVotes - 499;
totalVotes = 1029;
}
return Promise.resolve({
name: nameMap[municipality.Name] || municipality.Name,
pctTotal: municipality.TotalNumberPrecincts,
pctReporting: municipality.PrecinctReporting,
bidenPct: totalVotes > 0 ? Math.ceil((bidenVotes / totalVotes) * 10000) / 100 : 0,
bidenVotes,
trumpPct: totalVotes > 0 ? Math.ceil((trumpVotes / totalVotes) * 10000) / 100 : 0,
trumpVotes
});
});
})
.then((p) => Promise.all(p));
};
Promise.all([getAuthClient(), getCountyData()])
.then(([ authClient, countyData ]) => {
const sheetData = [
["name", "pctTotal", "pctReporting", "bidenPct", "bidenVotes", "trumpPct", "trumpVotes"],
...countyData.map(Object.values)
];
return sheets.spreadsheets.values.update({
auth: authClient,
range: 'County!A1:G132',
resource: {
values: sheetData,
},
spreadsheetId: process.env.SPREADSHEET_ID,
valueInputOption: 'USER_ENTERED',
});
})
.then((response) => {
if (response.status === 200) {
console.log(`***DONE: ${(new Date()).toLocaleString()}`);
}
})
.catch(console.log);