1   package eu.fbk.dkm.pikes.tintop;
2   
3   import com.google.gson.JsonElement;
4   import com.google.gson.JsonObject;
5   import com.google.gson.JsonParser;
6   
7   import javax.net.ssl.HttpsURLConnection;
8   import java.io.BufferedReader;
9   import java.io.DataOutputStream;
10  import java.io.IOException;
11  import java.io.InputStreamReader;
12  import java.net.HttpURLConnection;
13  import java.net.URL;
14  import java.net.URLEncoder;
15  import java.nio.file.Files;
16  import java.nio.file.Paths;
17  import java.util.ArrayList;
18  import java.util.List;
19  import java.util.Map;
20  import java.util.stream.Collectors;
21  
22  /**
23   * Created by alessio on 14/06/17.
24   */
25  
26  public class PikesTest {
27  
28      public static void main(String[] args) {
29          final String USER_AGENT = "Mozilla/5.0";
30  
31          String fileName = "/Users/alessio/Downloads/lista_persone_demo.txt";
32          List<String> list = new ArrayList<>();
33  
34          try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) {
35  
36              //br returns as stream and convert it into a List
37              list = br.lines().collect(Collectors.toList());
38  
39          } catch (IOException e) {
40              e.printStackTrace();
41          }
42  
43          for (String line : list) {
44              try {
45                  String url = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&explaintext=&exlimit=1&titles=" +
46                          URLEncoder.encode(line, "UTF-8");
47  
48                  URL obj = new URL(url);
49                  HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
50  
51                  // optional default is GET
52                  con.setRequestMethod("GET");
53  
54                  //add request header
55                  con.setRequestProperty("User-Agent", USER_AGENT);
56  
57                  int responseCode = con.getResponseCode();
58                  System.out.println("\nSending 'GET' request from  : " + line);
59                  System.out.println("Response Code : " + responseCode);
60  
61                  BufferedReader in = new BufferedReader(
62                          new InputStreamReader(con.getInputStream()));
63                  String inputLine;
64                  StringBuffer response = new StringBuffer();
65  
66                  while ((inputLine = in.readLine()) != null) {
67                      response.append(inputLine);
68                  }
69                  in.close();
70  
71                  JsonParser parser = new JsonParser();
72  
73                  JsonObject o = parser.parse(response.toString()).getAsJsonObject();
74                  //print result
75  
76                  String id_page = "";
77                  String page_content = "";
78                  for (Map.Entry<String, JsonElement> stringJsonElementEntry : o.getAsJsonObject("query").getAsJsonObject("pages").entrySet()) {
79                      id_page = stringJsonElementEntry.getKey();
80                      break;
81                  }
82                  page_content = o.getAsJsonObject("query").getAsJsonObject("pages").getAsJsonObject(id_page).get("extract").getAsString();
83  //                page_content = page_content.substring(0, 1000);
84  
85                  url = "http://rhodes.fbk.eu:50010/text2json";
86                  URL url_base = new URL(url);
87  
88                  HttpURLConnection conn = (HttpURLConnection) url_base.openConnection();
89  
90                  //add request header
91                  conn.setRequestMethod("POST");
92                  conn.setRequestProperty("User-Agent", USER_AGENT);
93                  conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
94  
95                  String urlParameters = "text=" + URLEncoder.encode(page_content, "UTF-8");
96  
97                  // Send post request
98                  conn.setDoOutput(true);
99                  DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
100                 wr.writeBytes(urlParameters);
101                 wr.flush();
102                 wr.close();
103 
104                 System.out.println("\nSending 'Pikes' request for : " + line);
105 
106                 in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
107 
108                 response = new StringBuffer();
109                 while ((inputLine = in.readLine()) != null) {
110                     response.append(inputLine);
111                 }
112                 in.close();
113 
114                 //print result
115 //                System.out.println(response.toString());
116 
117             } catch (Exception e) {
118                 e.printStackTrace();
119             }
120         }
121 
122     }
123 }