-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrawl.java
More file actions
141 lines (125 loc) · 5.85 KB
/
Crawl.java
File metadata and controls
141 lines (125 loc) · 5.85 KB
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
import net.sf.json.JSON;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class Crawl {
private User user;
public Crawl(User user) {
this.user = user;
}
public void refreshUser() throws IOException, ParseException {
JSONObject user_info = user_info(this.user.url);
this.user.setAvatar_url((URL) user_info.get("avatar_url"));
this.user.setBio((String) user_info.get("bio"));
this.user.setEmail((String) user_info.get("email"));
this.user.setName((String) user_info.get("name"));
this.user.setLocation((String) user_info.get("location"));
this.user.setCompany((String) user_info.get("company"));
this.user.setCreated_at((String) user_info.get("created_at"));
this.user.setUpdated_at((String) user_info.get("updated_at"));
// user = new User(
// (String) user_info.get("login"),
// (Long) user_info.get("id"),
// ,
// (URL) user_info.get("follower_url"),
// (URL) user_info.get("following_url"),
// (URL) user_info.get("organization_url"),
// (URL) user_info.get("repo_url"),
// (URL) user_info.get("starred_url"),
// (String) user_info.get("name"),
// ,
// (URL) user_info.get("blog"),
// (String) user_info.get("location"),
// (String) user_info.get("email"),
// (String) user_info.get("hireable"),
// ,
// (Long) user_info.get("followers"),
// (Long) user_info.get("following"),
// (String) user_info.get("created_at"),
URL follower_url = (URL)user_info.get("follower_url");
URL following_url = (URL)user_info.get("following_url");
JSONArray followers_info = users_info(follower_url);
JSONArray followings_info = users_info(following_url);
for (int i = 0; i < followers_info.size(); i++) {
JSONObject follower = (JSONObject) followers_info.get(i);
user.followers_list.add(new User((String)follower.get("login")));
}
for (int i = 0; i < followings_info.size(); i++) {
JSONObject following = (JSONObject) followings_info.get(i);
user.followings_list.add(new User((String)following.get("login")));
}
}
public static JSONObject user_info(URL url) throws IOException, ParseException {
String inline = "";
JSONObject user_info;
try {
//Parse URL into HttpURLConnection in order to open the connection in order to get the JSON data
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//Set the request to GET or POST as per the requirements
conn.setRequestMethod("GET");
//Use the connect method to create the connection bridge
conn.connect();
//Get the response status of the Rest API
int responsecode = conn.getResponseCode();
System.out.println("Response code is: " + responsecode);
//Iterating condition to if response code is not 200 then throw a runtime exception
//else continue the actual process of getting the JSON data
if (responsecode != 200)
throw new RuntimeException("HttpResponseCode: " + responsecode);
else {
//Scanner functionality will read the JSON data from the stream
Scanner sc = new Scanner(url.openStream());
while (sc.hasNext()) {
inline += sc.nextLine();
}
sc.close();
}
JSONParser jsonParser = new JSONParser();
user_info = (JSONObject) jsonParser.parse(inline);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return user_info;
}
public static JSONArray users_info(URL url) throws IOException, ParseException {
String inline = "";
JSONArray user_info;
try {
//Parse URL into HttpURLConnection in order to open the connection in order to get the JSON data
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//Set the request to GET or POST as per the requirements
conn.setRequestMethod("GET");
//Use the connect method to create the connection bridge
conn.connect();
//Get the response status of the Rest API
int responsecode = conn.getResponseCode();
System.out.println("Response code is: " + responsecode);
//Iterating condition to if response code is not 200 then throw a runtime exception
//else continue the actual process of getting the JSON data
if (responsecode != 200)
throw new RuntimeException("HttpResponseCode: " + responsecode);
else {
//Scanner functionality will read the JSON data from the stream
Scanner sc = new Scanner(url.openStream());
while (sc.hasNext()) {
inline += sc.nextLine();
}
sc.close();
}
JSONParser jsonParser = new JSONParser();
user_info = (JSONArray) jsonParser.parse(inline);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return user_info;
}
}