This repository has been archived by the owner on Dec 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfec.js
136 lines (113 loc) · 4.14 KB
/
fec.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
import redis from 'redis';
import fetch from 'node-fetch';
import pifall from 'pifall';
import sha1 from 'sha1';
const ovi_config = {
redis_host: ( process.env.REDIS_HOST ? process.env.REDIS_HOST : 'localhost' ),
redis_port: ( process.env.REDIS_PORT ? process.env.REDIS_PORT : 6379 ),
election_year: ( process.env.ELECTION_YEAR ? process.env.ELECTION_YEAR : missingConfig("ELECTION_YEAR") ),
api_key_fec: ( process.env.API_KEY_FEC ? process.env.API_KEY_FEC : missingConfig("API_KEY_FEC") ),
DEBUG: ( process.env.DEBUG ? true : false ),
};
// async'ify redis
pifall(redis.RedisClient.prototype);
pifall(redis.Multi.prototype);
async function dbwrap() {
var params = Array.prototype.slice.call(arguments);
var func = params.shift();
if (ovi_config.DEBUG) {
let funcName = func.replace('Async', '');
console.log('DEBUG: '+funcName+' '+params.join(' '));
}
return rc[func](params);
}
function cleanobj(obj) {
for (var propName in obj) {
if (obj[propName] == '' || obj[propName] == null)
delete obj[propName];
}
}
function ucfirst(string) {
return string.charAt(0).toUpperCase()+string.slice(1);
}
// redis connection
var rc = redis.createClient(ovi_config.redis_port, ovi_config.redis_host,
{
// endlessly retry the database connection
retry_strategy: function (options) {
console.log('redis connection failed to "'+ovi_config.redis_host+'", retrying: this is attempt # '+options.attempt);
return Math.min(options.attempt * 100, 3000);
}
}
);
rc.on('connect', async function() {
console.log('Connected to redis at host "'+ovi_config.redis_host+'"');
let page = 1;
let pages = 0;
try {
do {
const response = await fetch(
"https://api.open.fec.gov/v1/candidates/search/?sort=name&per_page=100"+
"&api_key="+ovi_config.api_key_fec+
"&election_year="+ovi_config.election_year+
"&page="+page,
{compress: true}
);
const json = await response.json();
if (!pages) pages = json.pagination.pages;
page++;
for (let i in json.results) {
let fec = json.results[i];
try {
if (fec.office == 'P') continue; // not handling this yet
let div = 'ocd-division/country:us/state:'+fec.state.toLowerCase()+(fec.office == 'H'?'/cd':'')+(fec.district_number?":"+fec.district_number:"");
let dname = await rc.hgetAsync('division:'+div, 'name');
if (!dname) throw "Incorrect division "+div;
// FEC "name" is in format: LAST, FIRST MIDDLE TITLE -- we only need first and last name
fec.last_name = ucfirst(fec.name.split(",")[0].toLowerCase().trim());
fec.first_name = ucfirst(fec.name.split(", ")[1].split(" ")[0].toLowerCase().trim());
fec.politician_id = sha1(div+":"+fec.last_name.toLowerCase()+":"+fec.first_name.toLowerCase());
fec.divisionId = div;
// convert party data
switch (fec.party) {
case 'DEM': fec.party = 'D'; break;
case 'REP': fec.party = 'R'; break;
case 'GRE': fec.party = 'G'; break;
case 'LIB': fec.party = 'L'; break;
case 'DFL': fec.party = 'DFL'; break;
case 'OTH': fec.party = 'O'; break;
case 'W':
case 'IND':
case 'UN':
case 'NNE':
case 'NPA':
case 'NON':
fec.party = 'I';
break;
default: fec.party = 'U';
}
// delete arrays that redis won't like
delete fec.principal_committees;
delete fec.election_districts;
delete fec.election_years;
delete fec.cycles;
// remove null keys
cleanobj(fec);
rc.hmset('fec:'+fec.candidate_id, fec);
rc.sadd('politician:'+fec.politician_id, 'fec:'+fec.candidate_id);
} catch (e) {
console.log("Unable to import FEC record: %j", fec);
console.log(e);
}
}
} while (page <= pages);
} catch (e) {
console.log(e);
}
process.exit(0);
});
function missingConfig(item) {
let msg = "Missing config: "+item;
console.log(msg);
process.exit(1);
}