-
Notifications
You must be signed in to change notification settings - Fork 0
/
openstates_query.js
96 lines (87 loc) · 2.8 KB
/
openstates_query.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
const request = require('superagent');
const { google } = require('googleapis');
const openstatesApiKey = '82e9cac0-93e7-496c-9a76-565ed6233fdc';
const openstatesEndpoint = 'https://openstates.org/api/v1/bills';
const searchWindow = ''
const openstatesQuery = (subj) => {
// Query OpenStates API
const data = request.get(openstatesEndpoint)
.query(openstatesParams(subj))
.then((response) => {
const shapedBills = response.body.map(bill => shapeBill(bill));
return shapedBills;
});
return data;
}
const openstatesParams = (subj) => {
return {
apikey: openstatesApiKey,
state: 'tx',
search_window: 'session',
sort: 'updated_at',
fields: 'bill_id,created_at,updated_at,title,session,subjects,actions,versions',
subject: subj,
};
}
const shapeBill = (bill) => {
// Reduce raw data to just what we need
return {
session: bill.session,
bill_id: bill.bill_id,
title: bill.title,
bill_url: shapeBillUrl(bill),
filed_date: bill.created_at,
last_status: shapeAction(bill),
last_updated: bill.updated_at,
// subjects: bill.subjects.join(', '),
}
}
const shapeAction = (bill) => {
// Strip weird value of `Effective on . . . . . . . . . . . . . . .`
const action = bill.actions[bill.actions.length - 1].action;
if (action == 'Effective on . . . . . . . . . . . . . . .') {
return 'Completed'; // TK get better verbiage
}
return action;
}
const shapeBillUrl = (bill) => {
// Safely get URL without crashing, since some bills don't have URLs for some reason?
if (bill.versions.length > 0) {
return bill.versions[bill.versions.length - 1].url;
}
return null;
}
// HERE'S THE SHEETS PART
const appendToSheet = (valueSet) => {
// Authorize
const privatekey = require("/Users/colin/Downloads/Beater.json");
const jwtClient = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
['https://www.googleapis.com/auth/spreadsheets']);
jwtClient.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
} else {
console.log("Successfully connected!");
}
});
// Ship to the sheet
const sheets = google.sheets('v4');
const googleSheetId = '1fO-vfsIkNuUnfFFiKtnp0pydHmsaLTFt1VgSvguDiAs';
return sheets.spreadsheets.values.append({
spreadsheetId: googleSheetId,
range: 'Sheet1',
auth: jwtClient,
valueInputOption: 'RAW',
insertDataOption: 'INSERT_ROWS',
resource: {
values: valueSet.map(bill => [bill.session, bill.bill_id, bill.title, bill.bill_url, bill.filed_date, bill.last_status, bill.last_updated]),
},
});
}
['Reproductive Issues', 'Sexual Orientation and Gender Issues', 'Health', 'Family and Children Issues'].forEach((subj) => {
openstatesQuery(subj).then((set) => appendToSheet(set));
})