-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhartford-address-lookup.js
221 lines (207 loc) · 6.29 KB
/
hartford-address-lookup.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
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
import getViewersByOffice from '../server/util/get-viewers-by-office'
import _ from 'lodash'
/*
* function takes an address in Hartford CT
* returns a list of offices that are relevant to
* assembly districts and senatorial districts
* pertinent to the the given address
**/
const https = require('https')
const geocodeRootURL = 'https://maps.googleapis.com/maps/api/geocode/json?address=' // must be followed by a '+' separated string where + is space
const censusGeocoder = address =>
`https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address=${address}&benchmark=9&format=json`
const key = `&key=${process.env.GOOGLE_MAPS_API_KEY}`
const getOfficials = (long, lat, doOnSuccess, address_found) => {
try {
https
.get(`${process.env.ELECTED_OFFICIALS_URL}?long=${long}&lat=${lat}`, resp => {
let data = ''
resp.on('data', chunk => {
data += chunk
})
resp.on('end', async () => {
const success = JSON.parse(data).success
if (success) {
const officials = JSON.parse(data).data[0].elected_officials
const districts = officials.districts
/*
* districts is an array of objects
* format:
* name: String -> containing district name
* id: Number
* type: String -> containing type of district e.g. School District
* state: 'CT'
* offices: [{id, name:???,office_holders}]
* */
let officeNames = []
districts.forEach(district => {
// the upper legislative body is being used for testing
if (district.offices && district.type === 'State Legislative (Upper)') {
district.offices.forEach(office => {
officeNames.push({
id: office.id,
name: office.name,
district_number: parseInt(office.name[office.name.length - 1]),
type: district.type,
})
})
}
})
//officeNames.forEach(office => {
//console.log(parseInt(office.name[office.name.length - 1]))
//})
//TODO remove this later as the tabs should be filled
let viewers = []
for (let office of officeNames) {
let viewer = await getViewersByOffice(String(office.id))
viewers.push(viewer)
}
//send info after success
doOnSuccess({ ok: true, address_found, officeNames, viewers })
} else {
doOnSuccess({ ok: false, error: JSON.parse(data) })
}
})
})
.on('error', err => {
console.log('Error: ' + err)
})
} catch (error) {
console.log(error)
}
}
export default async function listOffices(address, doOnSuccess) {
const query = address.trim().replace(/\s+/g, '+') // removes trailing whitespace and replaces inner spaces with '+'
try {
https
.get(censusGeocoder(query), resp => {
let data = ''
resp.on('data', chunk => {
data += chunk
})
resp.on('end', () => {
const jsonData = JSON.parse(data)
const { result } = jsonData
console.log(jsonData)
if (result && result.addressMatches.length > 0) {
const matches = result.addressMatches[0]
const address_found = matches.matchedAddress
const lng = matches.coordinates.x
const lat = matches.coordinates.y
getOfficials(lng, lat, doOnSuccess, address_found)
} else {
doOnSuccess({ ok: false, jsonData })
}
})
})
.on('error', err => {
console.log('Error: ' + err)
})
} catch (error) {
console.log(error)
}
}
//export default async function listOffices(address, doOnSuccess) {
//const query = address.trim().replace(/\s+/g, '+') // removes trailing whitespace and replaces inner spaces with '+'
//try {
//https
//.get(geocodeRootURL + query + key, resp => {
//let data = ''
//resp.on('data', chunk => {
//data += chunk
//})
//resp.on('end', () => {
//const jsonData = JSON.parse(data)
//const { status } = jsonData
//console.log(jsonData)
//if (status === 'OK') {
//const geo = jsonData.results[0].geometry
//const address_found = jsonData.results[0].formatted_address
//const lng = geo.location.lng
//const lat = geo.location.lat
//getOfficials(lng, lat, doOnSuccess, address_found)
//} else {
//doOnSuccess({ ok: false, jsonData })
//}
//})
//})
//.on('error', err => {
//console.log('Error: ' + err)
//})
//} catch (error) {
//console.log(error)
//}
// validate the address to check if it meets all the requirements
// check google maps to get a long + lat coordinate for the address
// send coordinates in a ballotpedia url get request format ?lat=xx.xxxx&long=xx.xxxx
// take list of offices and filter for relevant offices.
//}
//let hartford_voting_centers = [
//{
//lat: 41.792,
//long: -72.708,
//},
//{
//lat: 41.769,
//long: -72.707,
//},
//{
//lat: 41.757,
//long: -72.707,
//},
//{
//lat: 41.735,
//long: -72.694,
//},
//{
//lat: 41.75,
//long: -72.685,
//},
//{
//lat: 41.789,
//long: -72.671,
//},
//]
//for (let { long, lat } of hartford_voting_centers) {
//console.log(long, lat)
//https
//.get(`${process.env.ELECTED_OFFICIALS_URL}?long=${long}&lat=${lat}`, resp => {
//let data = ''
//resp.on('data', chunk => {
//data += chunk
//})
//resp.on('end', async () => {
//const success = JSON.parse(data).success
//if (success) {
//const officials = JSON.parse(data).data[0].elected_officials
//const districts = officials.districts
//let officeNames = []
//districts.forEach(district => {
//// the upper legislative body is being used for testing
//if (district.offices && district.type === 'State Legislative (Lower)') {
//district.offices.forEach(office => {
//officeNames.push({
//id: office.id,
//name: office.name,
//district_number: parseInt(office.name[office.name.length - 1]),
//type: district.type,
//})
//})
//}
//})
//let viewers = []
//for (let office of officeNames) {
//let viewer = await getViewersByOffice(String(office.id))
//viewers.push(viewer)
//}
////send info after success
//console.log(officeNames)
//} else {
//console.log(JSON.parse(data))
//}
//})
//})
//.on('error', err => {
//console.log('Error: ' + err)
//})
//}