house-gov-page-scraper
Version:
a node module for scraping info from the US House of Representatives website: house.gov
95 lines (76 loc) • 3.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.endpoint = exports.INVALID_DISTRICT_CODE_STRING = exports.NO_RESULTS_ZIP = exports.INVALID_ZIP = undefined;
exports.parseDistrictCode = parseDistrictCode;
exports.scrapePage = scrapePage;
exports.default = getDistrictsInZip;
var _requestPromiseNative = require('request-promise-native');
var _requestPromiseNative2 = _interopRequireDefault(_requestPromiseNative);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var INVALID_ZIP = exports.INVALID_ZIP = function INVALID_ZIP(zip) {
return 'Invalid zip code: "' + zip + '".';
};
var NO_RESULTS_ZIP = exports.NO_RESULTS_ZIP = function NO_RESULTS_ZIP(zip) {
return 'No results for zip code: "' + zip + '".';
};
var INVALID_DISTRICT_CODE_STRING = exports.INVALID_DISTRICT_CODE_STRING = function INVALID_DISTRICT_CODE_STRING(val) {
return 'District code substring: "' + val + '" is an invalid format.';
};
var endpoint = exports.endpoint = function endpoint(zip) {
return 'http://ziplook.house.gov/htbin/findrep?ZIP=' + zip;
};
function parseDistrictCode(val) {
if (!val) {
// eslint-disable-next-line babel/new-cap
throw new Error(INVALID_DISTRICT_CODE_STRING(val));
}
var num = val.match(/\d+/g);
var st = val.substring(1, 3);
if (!num || !st || st.length !== 2 || num.length !== 1 || isNaN(num[0]) || num[0].length > 2) {
// eslint-disable-next-line babel/new-cap
throw new Error(INVALID_DISTRICT_CODE_STRING(val));
}
return st + '-' + Number(num);
}
function scrapePage(html, zip) {
var expected404Msg = 'The ZIP code ' + zip + ' was not found.';
if (html.indexOf(expected404Msg) > -1) {
return [];
}
if (html.indexOf('invalid Zip Code') > -1) {
// eslint-disable-next-line babel/new-cap
throw new Error(INVALID_ZIP(zip));
}
var startStr = 'districts=[';
var start = html.indexOf(startStr) + startStr.length;
var end = html.indexOf('];', start);
var data = html.substring(start, end).split(',');
return data.map(parseDistrictCode);
}
/**
* Retrieves an array of Congressional districts based on zip
* by scraping house.gov's district finder.
* @see http://ziplook.house.gov/htbin/findrep
* @param {string} zip - A 5 digit zip code.
* Prefix 4 digit zip codes w/ "0".
* @returns {Promise}
* Rejects with a 404 if no district codes are found.
* Resolves with an array of hyphen delimited district codes.
* ex: ['AL-1','AL-2']
*/
function getDistrictsInZip(zip) {
return _requestPromiseNative2.default.get({ uri: endpoint(zip) }).then(function (html) {
return scrapePage(html, zip);
}).then(function (result) {
if (result.length) {
return result;
}
return Promise.reject({
statusCode: 404,
// eslint-disable-next-line babel/new-cap
message: NO_RESULTS_ZIP(zip)
});
});
}