-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhouse-reports.js
56 lines (48 loc) · 1.86 KB
/
house-reports.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
const axios = require('axios');
const admZip = require('adm-zip');
// TODO: Create tests to verify transaction output
const fetchDisclosures = async year => {
const url = `https://disclosures-clerk.house.gov/public_disc/financial-pdfs/${year}FD.ZIP`;
const zipBuffer = await axios.get(url, {
responseType: 'arraybuffer'
});
const zip = new admZip(zipBuffer.data)
const zipEntries = zip.getEntries();
return zip.readAsText(zipEntries[0]);
}
// members is an array of objects each containing first & last names of a member
const parseDisclosures = (disclosures, members) => {
const lines = disclosures.split('\r\n')
const header = lines[0].split('\t')
const result = [];
for (let line of lines.slice(1)) {
const values = line.split('\t');
const row = values.reduce((row, field, index) => {
row[header[index]] = field
return row;
}, {})
let member = members.find(member =>
member.last.toLowerCase() === row?.Last?.toLowerCase() &&
member.first.toLowerCase() === row?.First?.toLowerCase())
if (member) result.push(row)
}
return result
}
const fetchDocuments = async (disclosures, year) => {
const documents = [];
for (disclosure of disclosures) {
const DocID = disclosure["DocID"]
const reportType = (disclosure["FilingType"] == "P") ? "ptr-pdfs" : "financial-pdfs"
const url = `https://disclosures-clerk.house.gov/public_disc/${reportType}/${year}/${DocID}.pdf`
documents.push({
first: disclosure.First,
last: disclosure.Last,
report: url,
date: disclosure.FilingDate,
state: disclosure.StateDst,
type: disclosure.FilingType
})
}
return documents;
}
module.exports = { fetchDisclosures, parseDisclosures, fetchDocuments };