-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
62 lines (52 loc) · 1.96 KB
/
Main.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Main {
String charset = "UTF-8";
public static void main(String[] args) throws IOException {
String urlString =
"https://api.nytimes.com/svc/search/v2/articlesearch.json";
urlString += "?q=wind+power"
+ "&api-key=61f3909960f048909642771cedab3b76"
+ "&response-format=jsonp"
+ "&callback=svc_search_v2_articlesearch";
HttpURLConnection connection = (HttpURLConnection) new URL(urlString).openConnection();
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer response = new StringBuffer();
String line = in.readLine();
while (line != null) {
response.append(line);
line = in.readLine();
}
JSONParser parser = new JSONParser();
try {
JSONObject obj = (JSONObject) parser.parse(response.toString());
JSONObject resp = (JSONObject) obj.get("response");
JSONArray docs = (JSONArray) resp.get("docs");
JSONObject doc = (JSONObject) docs.get(0);
// Loop through some arbitrary amount
for (int i = 1; i < 10; i++) {
doc = (JSONObject) docs.get(i);
String abs = (String) doc.get("abstract");
String url = (String) doc.get("web_url");
System.out.println("URL: " + url);
// Send GET for url contents here
// Find the HTML tag/class that contains the content
// Looks like it's <p class="story-body-text story-content"
// Use jtdiy to get the contents of this specific tag
// Strip out links
// Split contents on regex to get sentences e.g.: '. '
// Map from article URL to set of sentences
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}