-
Notifications
You must be signed in to change notification settings - Fork 0
/
createBigData.java
102 lines (84 loc) · 3.08 KB
/
createBigData.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
j/*
* Get each day's page views for topics mentioned in main method
*/
package wiki.wiki;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
public class createBigData {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
prepURL();
}
// HTTP GET request
private String sendGet(String url) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
//System.out.println("\nSending 'GET' request to URL : " + url);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
public static void parseFromJSONResponse(String respo)
{
JSONObject myjson;
try
{
myjson = new JSONObject(respo);
for(int i = 0 ; i< 30 ; i++){
JSONObject res = myjson.getJSONArray("items").getJSONObject(i);
String result = res.getString("article") + "," + res.getString("timestamp").substring(0, 8) + "," + res.getLong("views");
try(FileWriter fw = new FileWriter("wikicounts", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw))
{
out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
public static void prepURL() throws Exception{
String[] articles = { "Isaac_Newton","Albert_Einstein" , "Thomas_Edison" , "Galileo_Galilei" , "Archimedes"} ;
String[] startDates = {"2015100100" , "2015090100" , "2015080100" };
String[] endDates = {"2015103100" , "2015093000" , "2015083100"};
createBigData http = new createBigData();
for(int i = 0 ; i < articles.length ; i++){
for(int j = 0 ; j < startDates.length ; j++){
String url1 = "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia/all-access/all-agents/";
String url2 = articles[i]; //Article name
String url3 = "/daily/" ;
String url4 = startDates[j]; //Start Date
String url5 = "/";
String url6 = endDates[j]; //End Date
StringBuilder sb = new StringBuilder();
sb.append(url1).append(url2).append(url3).append(url4).append(url5).append(url6);
String res = sb.toString();
String response = http.sendGet(res);
parseFromJSONResponse(response);
}
}
}
}