-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcivic_info.js
More file actions
144 lines (117 loc) · 3.74 KB
/
civic_info.js
File metadata and controls
144 lines (117 loc) · 3.74 KB
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
// For civic info
const API_KEY = "AIzaSyDJDVbMVYKXs15lWL77RI-xdSj4mSPofJA";
const BASE_URL = "https://www.googleapis.com/civicinfo/v2/voterinfo?key=";
const POLLING_LOCATIONS = "pollingLocations";
const ELECTIONS = "elections";
const CANDIDATE_URL = "candidateUrl";
const WEBSITE = "Website";
const NAME = "Name";
const STRING = "string";
const https = require('https');
// ---------------------------------- CIVIC INFO -------------------------------
function constructUrl(address) {
const address_key = "&address="
return BASE_URL + API_KEY + address_key + address
}
function formatAddress(address) {
return address.replace(" ", "%20")
}
function title(str) {
return str
.toLowerCase()
.trim()
.split(' ')
.map(function(word) {
if (word[0] != '(') {
return word[0].toUpperCase() + word.substr(1);
} else {
return word[0] + word[1].toUpperCase() + word.substr(2);
}
})
.join(' ');
}
function formatInfoType(info_type) {
switch(info_type) {
case CANDIDATE_URL:
return WEBSITE;
default:
return title(info_type);
}
}
function formatInfo(info_type, info) {
switch(info_type) {
case NAME:
return title(info);
default:
return info;
}
}
function getElectionsData(resp) {
var offices = {};
contests = resp.contests
for (var i in contests) {
if (contests[i].office === undefined) {
continue;
}
var str = "";
for (var j in contests[i].candidates) {
candidate = contests[i].candidates[j]
for (var key in candidate) {
if (typeof candidate[key] === STRING) {
var info_type = formatInfoType(key)
var info = formatInfo(info_type, candidate[key])
str += info_type + ": " + info + "\n";
}
}
for (var k in candidate.channels) {
var channel = candidate.channels[k]
var info_type = formatInfoType(channel.type)
str += info_type + ": "+ channel.id + "\n";
}
str += "\n";
}
offices[contests[i].office] = str;
}
return offices;
}
function getLocationsData(resp) {
var str = ""
locations = resp.pollingLocations
if (locations.length == 0) {
return "Sorry, there are no polling locations near you.";
}
str += "Polling Locations Near You:\n\n";
for (var i in locations) {
var address = locations[i].address
str += "Name: " + address.locationName + "\n\n";
str += "Address: " + [address.line1, address.city, address.state].join(',') + " " + address.zip + "\n\n";
str += "Polling Hours: " + locations[i].pollingHours + "\n\n";
str += "\n\n";
}
return str;
}
async function getRequest(url, fetch_data) {
return new Promise((resolve, reject) => {
https.get(url, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
jsonResp = JSON.parse(data);
resolve(fetch_data(jsonResp))
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
});
}
async function getInfo(raw_address, func) {
var address = formatAddress(raw_address)
var url = constructUrl(address)
var response = await getRequest(url, func);
return response;
}
module.exports = {getInfo, getLocationsData, getElectionsData}