-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWRVoteData.pde
291 lines (235 loc) · 8.08 KB
/
WRVoteData.pde
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
public class WRVoteData {
public static final int SENATE = 0;
public static final int HOUSE = 1;
public static final int DEMOCRAT = 2;
public static final int REPUBLICAN = 3;
public static final int TIE = 4;
private String api_key;
public ArrayList senate_votes;
public ArrayList house_votes;
public ArrayList votes;
public Date start_date;
public Date end_date;
public HashMap bill_cache;
public WRVoteData(XMLElement house_xml, XMLElement senate_xml, String api_key ) {
this.bill_cache = new HashMap();
this.api_key = api_key;
this.senate_votes = getWRVoteArrayList(senate_xml, WRVoteData.SENATE);
this.house_votes = getWRVoteArrayList(house_xml, WRVoteData.HOUSE);
this.votes = new ArrayList();
this.votes.addAll(senate_votes);
this.votes.addAll(house_votes);
this.sortByDate(votes);
}
private void sortByDate(ArrayList votes) {
Collections.sort(votes, new Comparator() {
public int compare(Object o1, Object o2) {
WRVote v1 = (WRVote) o1;
WRVote v2 = (WRVote) o2;
return v1.date.compareTo(v2.date);
}
});
}
private ArrayList getWRVoteArrayList(XMLElement xml, int chamber) {
XMLElement[] vote_elements = xml.getChildren("results/votes/vote");
ArrayList al = new ArrayList();
println(vote_elements.length);
for(int i=0; i<vote_elements.length; i++) {
if(filterVote(vote_elements[i])) {
WRVote vote = new WRVote(vote_elements[i], chamber, this.api_key);
println(vote.bill_number);
al.add(vote);
// if(!bill_cache.containsKey(vote.getCleanBillNumber())) {
// bill_cache.put(vote.getCleanBillNumber(), vote.bill);
// }
// try {
// Thread.sleep(200);
// } catch(InterruptedException e) {
// println("ERROR! Throw that shit.");
// }
}
}
return al;
}
private boolean filterVote(XMLElement vote) {
// check to see if there is a value for bill_number
if(vote.getChild("bill_number").getContent() == null) {
return false;
}
// check to make sure it's a house/senate bill (not a nomination)
String bill_number_first_char = vote.getChild("bill_number").getContent().substring(0,1);
if(!(bill_number_first_char.equals("H") || bill_number_first_char.equals("S"))) {
return false;
}
// check to make sure it's not a motion
if(match(vote.getChild("result").getContent(),"Motion") != null) { return false; }
// guess it's ok?
return true;
}
}
public class WRVoteTimeline {}
/**
* WRApiObject is a generic object to wrap API calls. Meant to be extended
* and overridden.
*
* @author Andrew Dayton
* @since 06.08.2011
*/
public class WRApiObject {
public String obj_uri; // the api endpoint for this obj
public XMLElement obj_uri_response; // xml for entire response
public XMLElement obj_xml_element; // xml for individual object
private String api_key; // api_key
public WRApiObject(String obj_uri, String api_key) {
this.obj_uri = obj_uri;
this.api_key = api_key;
this.obj_uri_response = this.apiCall();
this.obj_xml_element = isolateObjectXml(this.obj_uri_response);
}
public String get(String element_name) {
return this.getChildElement(element_name);
}
public String getChildElement(String element_name) {
return obj_xml_element.getChild(element_name).getContent();
}
// this will need to be overridden
private XMLElement isolateObjectXml(XMLElement xmlResponse) {
return xmlResponse.getChild("results");
}
private XMLElement apiCall() {
String path = this.obj_uri + "?api-key=" + this.api_key;
XMLElement xmlResponse = new XMLElement(loadUri(path));
return xmlResponse;
}
private String loadUri(String path) {
println("Load url: "+path);
String lines[] = loadStrings(path);
/*if(lines == null) { println("API read error: "+path); return null; }*/
String response = join(lines, "\n");
return response;
}
}
/**
* WRMember is a wrapper for the NYT Congress API member method
* http://developer.nytimes.com/docs/read/congress_api#h3-member-roles
*
* @author Andrew Dayton
* @since 06.08.2011
*/
public class WRMember extends WRApiObject {
public WRMember(String member_uri, String api_key) {
super(member_uri, api_key);
}
private XMLElement isolateObjectXml(XMLElement xmlResponse) {
return xmlResponse.getChild("results/member");
}
}
/**
* WRBill is a wrapper for the NYT Congress API Bill Details method
* http://developer.nytimes.com/docs/read/congress_api#h3-bill-details
*
* @author Andrew Dayton
* @since 06.08.2011
*/
public class WRBill extends WRApiObject {
public WRBill(String member_uri, String api_key) {
super(member_uri, api_key);
}
}
/**
* WRVote is (sort of) a wrapper for a NYT Congress API vote
* http://developer.nytimes.com/docs/read/congress_api#h2-votes
*
* @author Andrew Dayton
* @since 06.08.2011
*/
public class WRVote {
private String api_key;
// public WRBill bill;
public int chamber;
public Date date;
public int congress;
public int session;
public String bill_number;
public String result;
public String description;
public int party_affiliation;
public int winner;
public WRVote(XMLElement vote_xml, int chamber, String api_key) {
this.api_key = api_key;
this.date = this.formatDate(vote_xml);
this.chamber = chamber;
this.congress = Integer.parseInt(vote_xml.getChild("congress").getContent());
this.session = Integer.parseInt(vote_xml.getChild("session").getContent());
this.bill_number = vote_xml.getChild("bill_number").getContent();
this.result = vote_xml.getChild("result").getContent();
this.description = vote_xml.getChild("description").getContent();
this.winner = this.getWinner(vote_xml);
// get the bill
// // check the bill cache
// if(bill_cache.containsKey(this.cleanBillNumber(this.bill_number))) {
// this.bill = (WRBill)bill_cache.get(this.cleanBillNumber(this.bill_number));
// } else {
// String bill_uri = this.buildBillUri(this.congress, this.bill_number, api_key);
// this.bill = new WRBill(bill_uri, this.api_key);
// }
}
private int getWinner(XMLElement vote_xml) {
// @TODO check if bill passed (regex pending from Andy)
int billPassed = 1;
// check dem majority pos
String demPosStr = vote_xml.getChild("democratic").getChild("majority_position").getContent();
println("Dem Position: "+demPosStr);
int demPosInt = 0;
if(demPosStr.equals("Yes")) {
demPosInt = 1;
}
// check dem majority pos
String repPosStr = vote_xml.getChild("republican").getChild("majority_position").getContent();
println("Rep Position: "+repPosStr);
int repPosInt = 0;
if(repPosStr.equals("Yes")) {
repPosInt = 1;
}
// TIE
if(repPosInt == demPosInt) {
return 4;
}
// DEM WIN
if(demPosInt == billPassed && repPosInt != billPassed) {
return 2;
}
// REP WIN
if(repPosInt == billPassed && demPosInt != billPassed) {
return 3;
}
return 4;
}
public String getCleanBillNumber() {
return this.cleanBillNumber(this.bill_number);
}
public String cleanBillNumber(String bill_number) {
String clean_bill_number = bill_number;
char[] rm_chars = { ' ','.' };
for(int i=0; i<rm_chars.length; i++) {
String[] splitted = split(clean_bill_number, rm_chars[i]);
clean_bill_number = join(splitted, "");
}
return clean_bill_number;
}
// grab date & time from the XMLElement, return a date object
private Date formatDate(XMLElement e) {
String date = e.getChild("date").getContent();
String time = e.getChild("time").getContent();
String date_time = date + " " + time;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsed = new Date();
// do it
try{
parsed = format.parse(date_time);
} catch(ParseException pe) {
println("ERROR: Cannot parse \"" + date_time + "\"");
}
return parsed;
}
}