/
ApiReaderEdited.java
159 lines (140 loc) · 4.59 KB
/
ApiReaderEdited.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* This class is used as a static method class to interface with the GovTrack
* API in JSON format. Dependencies are Google's JSON Parser and Tokenizer
* JAR.
*
* @author William Archer
*
*
*/
public class ApiReaderEdited {
private static LinkedList<String> congressmen = new LinkedList();
/**
*
* read is a private helper function used to parse all the way through
* a specified reader. It is not meant to be used in main functions
* and only helps create the JSONObject used in getJSON.
*
* @param reader - the reader to read through
* @return String - string of the entire JSON response
* @author William Archer
*
*/
private static String read(Reader reader) {
StringBuilder retVal = new StringBuilder();
int character;
try {
//read returns char unless EOF, which is -1
character = reader.read();
while(character != -1) { //not done
retVal.append((char) character);
character = reader.read();
}
return retVal.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* This method produces a JSONObject from an HTTP call. It's genericized
* for any type of API, however it assumes the url is correctly formatted
*
* @see org.json
* @param url - url call to make
* @return JSONObject from response by API URL
* @author William Archer
*/
public static JSONObject getJSON(String url) {
try {
//open up the url and get the bytes
InputStream is = new URL(url).openStream();
InputStreamReader input = new InputStreamReader(is);
String json = read(new BufferedReader(input));
is.close(); //don't forget to close!
return new JSONObject(json);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* getMap creates a HashMap of all bills in the json parameter of all
* sponsors and their various co-sponsors. It utilizes the govTrack.us
* API
*
*
* @param json - the json response previously gotten
* @return - HashMap of all sponsors and co-sponsors in all lowercase
* we use a List because we want to see if a sponsor and
* co-sponsor have worked together multiple times
* @author William Archer
*/
public static HashMap<String,List<String>> getMap(JSONObject json) {
HashMap<String,List<String>> retVal = new HashMap<String, List<String>>();
Set<String> ids = new HashSet<String>(); //capture all bill ids
try {
JSONArray test = json.getJSONArray("objects");
//parse response to get bill ids
for(int i=0; i < test.length(); i++) {
JSONObject object = (JSONObject) test.get(i);
ids.add(object.get("id").toString());
}
String baseUrl = "https://www.govtrack.us/api/v2/bill/";
//API doesn't include co-sponsor information if querying so
//you have to request each individual bill...
for(String id : ids) {
JSONObject object = getJSON(baseUrl+id);
JSONObject congress = object.getJSONObject("sponsor");
//building congressperson's name
String congressperson = (congress.getString("firstname")
+ " " + congress.getString("lastname")).toLowerCase();
if (!congressmen.contains(congressperson)) {
congressmen.add(congressperson);
}
JSONArray cosponsors = object.getJSONArray("cosponsors");
//co-sponsors are stored in a JSONArray which
//we iterate through to get everything and store
for(int i = 0; i < cosponsors.length(); i++) {
JSONObject cosponsor = cosponsors.getJSONObject(i);
String cosponsorName = (cosponsor.getString("firstname")
+ " " + cosponsor.getString("lastname")).toLowerCase();
if (!congressmen.contains(cosponsorName)) {
congressmen.add(cosponsorName);
}
//if we haven't encountered the sponsor before
//we need to create a new list
if(retVal.get(congressperson) == null) {
List<String> newSet = new LinkedList<String>();
newSet.add(cosponsorName);
retVal.put(congressperson,newSet);
} else {
retVal.get(congressperson).add(cosponsorName);
}
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retVal;
}
public static List<String> getCongressmen() {
return congressmen;
}
}