Current US Congresspeople
const got = require("got");
const R = require("ramda");
const SyncRequest = require('sync-request');
const states = {};
function getStates()
{
if (states["CA"]) return states;
const res = SyncRequest('GET', 'https://www.govtrack.us/api/v2/role?current=true&limit=6000');
const data = JSON.parse(res.getBody("utf-8"));
R.forEach(rep => {
if (!rep.state) return;
let state = states[rep.state];
if (!state)
state = states[rep.state] = {senators:[], representatives:[]};
if (rep.role_type === "senator")
state.senators.push(rep);
else if (rep.role_type === "representative")
state.representatives[rep.district] = rep;
}, data.objects);
return states;
}
function getStateReps(state)
{
return getStates()[state.toUpperCase(1)];
}
function getDistrictReps(state, district)
{
const stateReps = getStateReps(state);
let districtReps = null;
if (stateReps)
districtRep = stateReps.representatives[district];
if (stateReps && districtRep)
return R.insert(0, districtRep, stateReps.senators);
else
return [];
}
module.exports.getStateReps = getStateReps;
module.exports.getDistrictReps = getDistrictReps;
no comments