-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOctoparse API.java
More file actions
273 lines (253 loc) · 8.96 KB
/
Octoparse API.java
File metadata and controls
273 lines (253 loc) · 8.96 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package APIDemo;
/**
* @Project: Skieer BI
* @Package: skieer.test
* @Description: TODO
* @author
* @version V1.0.0
* @date 3:06:33 afternoon on September 6, 2016
* @copyright Copyright 2014(c) Octopus Data Inc.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @ClassName: APIDemo
* @Description: TODO
* @author
*/
public class APIDemo {
public static void main(String args[]) {
String userName = ""; //Octoparse Username
String passWord = ""; //Octoparse Password
//Access token
String tokenUrl = "http://dataapi.octoparse.com/token";
String getToken = GetToken(userName, passWord, tokenUrl);
System.out.println("Token is:" + getToken);
System.out.println();
//Regular processing access token
String tokenRegex=":\"(.*)\",\"t";
Pattern p=Pattern.compile(tokenRegex);
Matcher m = p.matcher(getToken);
String token="";
while(m.find()){
//System.out.println("Token£º"+m.group(1));
//.out.println();
token="bearer "+m.group(1); //System.out.println(token);
}
//Get the task group list by task group interface demo
String taskGroupUrl="http://dataapi.octoparse.com/api/taskgroup";
String getTaskGroup=LoadTaskGroup(token, taskGroupUrl);
System.out.println("TaskGroup is :"+getTaskGroup);
System.out.println();
//Get the task list by task group id and token, taskGroupID task group id for the users themselves
String taskGroupId=""; //Task group id, Get set up
String taskIdUrl="http://dataapi.octoparse.com/api/task?taskgroupid="+taskGroupId;
String getTask=LoadTaskGroup(token, taskIdUrl);
System.out.println("TaskId is :"+getTask);
System.out.println();
//Get AllData, taskID tasks for the user ID, parameter set for themselves
String taskId=""; //task id,Get set up
int pageindex=1; //Your Settings
int pagesize=3; //Your Settings
String precedAllDataUrl="http://dataapi.octoparse.com/api/alldata";
String allDataparam="taskid="+taskId+"&pageindex="+pageindex+
"&pagesize="+pagesize;
String getAllData=sendGet(precedAllDataUrl, allDataparam, token);
System.out.println("All Data is :"+getAllData);
System.out.println();
//Access to notexportdata
int size=2; //Your Settings
String notExportDataUrl="http://dataapi.octoparse.com/api/notexportdata";
String notExportparam="taskid="+taskId+"&size="+size;
String getNotExportData=sendGet(notExportDataUrl, notExportparam, token);
System.out.println("Not Export Data is"+getNotExportData);
}
public static String LoadTaskGroup(String token,String taskgroupUrl) {
String result = "";
BufferedReader in = null;
try {
URL realUrl = new URL(taskgroupUrl);
// The connection between the opening and the URL
URLConnection connection = realUrl.openConnection();
// Set the request of the general properties
connection.setRequestProperty("accept", "application/json");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Authorization", token);
// To establish the actual connection
connection.connect();
// Get all the response header fields
Map<String, List<String>> map = connection.getHeaderFields();
// Iterate through all the response header fields
for (String key : map.keySet()) {
//System.out.println(key + "--->" + map.get(key));
}
// Define BufferedReader input stream to read the response of the URL
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"UTF-8"));
//connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
//return result;
} catch (Exception e) {
System.out.println("Send a GET request an exception!" + e);
e.printStackTrace();
}
// To use a finally block to close the input stream
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
return result;
}
}
public static String GetToken(String userName, String password,
String tokenUrl) {
String token = null;
if (null != userName && null != password && null != tokenUrl) {
String postdata = String.format(
"username=%s&password=%s&grant_type=password", userName,
password);
String responseText = sendPost(tokenUrl,postdata);
//System.out.println(responseText);
if (responseText.contains("access_token")) {
token = responseText;// JObject.Parse(responseText)["access_token"].ToString();
}
}
return token;
}
public static String sendGet(String url, String param,String token) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
//String bearerAccessToken="bearer "+token;
//System.out.println(bearerAccessToken);
URL realUrl = new URL(urlNameString);
// The connection between the opening and the URL
URLConnection connection = realUrl.openConnection();
// Set the request of the general properties
connection.setRequestProperty("accept", "application/json");
connection.setRequestProperty("Authorization", token);
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// To establish the actual connection
connection.connect();
//Get all the response header fields
Map<String, List<String>> map = connection.getHeaderFields();
// Iterate through all the response header fields
for (String key : map.keySet()) {
//System.out.println(key + "--->" + map.get(key));
}
// Define BufferedReader input stream to read the response of the URL
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("Send a GET request an exception!" + e);
e.printStackTrace();
}
// To use a finally block to close the input stream
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
/**
* To specify the URL to send the request of the POST method
*
* @param url
* Send the request URL
* @param param
* Request parameters, request parameters should be name1 = value1 & name2 = value2 form.
* @return Represents a remote resource response results
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// The connection between the opening and the URL
HttpURLConnection conn = (HttpURLConnection) realUrl
.openConnection();
// Set the request of the general properties
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestMethod("POST");
conn.setDoOutput(true);// Use the URL connection for output
conn.setDoInput(true);//Use the URL connection for input
byte[] requestStringBytes = param.getBytes("utf-8");
// To build the output stream and write data
OutputStream outputStream = conn.getOutputStream();
outputStream.write(requestStringBytes);
outputStream.close();
// Get the response status
int responseCode = conn.getResponseCode();
if (HttpURLConnection.HTTP_OK == responseCode) {// The connection is successful
// When the correct response to process the data
StringBuffer sb = new StringBuffer();
String readLine;
BufferedReader responseReader;
//Process the response flow, must be consistent with the server response stream output coding
responseReader = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "utf-8"));
while ((readLine = responseReader.readLine()) != null) {
sb.append(readLine).append("\n");
}
responseReader.close();
result = sb.toString();
}
} catch (Exception e) {
System.out.println("Send a POST request is abnormal!" + e);
e.printStackTrace();
}
// Use a finally block to close the output stream and the input stream
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}